# Logistic differential equation with intermittent exploit rate

I'm working with the differential equation:

$$\frac{dP}{dt}=rP(1-\frac{P}K)-H(t)$$ $$P(0)=P_0$$

where $$r$$ and $$K$$ are known values. The idea is that $$H(t)$$ is a piecewise function such that from $$t=0, H(t)=H_0$$ (also a known value) but when $$P(t)$$ is below a certain value $$p_1$$ then $$H(t)=0$$ until $$P(t)$$ is greater than a certain value $$p_2>p_1$$.

After this, $$H(t)=H_0$$ again until $$P(t)$$ is below $$p1$$, and so on. I think the graph of the $$P(t)$$ function should look like a zig-zagging line.

I've been trying to numerically solve such equation but I don't know how to code such function. The only way I can think of describing such function $$H(t)$$ is by using a boolean variable that keeps track of the growth of $$P$$, but I don't think I can do that inside the NDSolve function.

Any ideas on how H would be so NDSolve[{P'[t] == r (1 - P[t]/K)*P[t] - H[t], P[0] == P0}, P, {t, 0, 20}] doesn't outputs errors?

You can solve this using NDSolve with two WhenEvents, with H[t] as a DiscreteVariable.

Variables:

ClearAll[p, t, H];
p0 = 1/2;
p1 = 1;
p2 = 2;
H0 = 5;
k = 10;
r = 1/2;


Differential equation and initial conditions:

ode = {p'[t] == r p[t] (1 - p[t]/k) - H[t],
WhenEvent[p[t] == p2(*&&p'[t]>0*), H[t] -> H0],
WhenEvent[p[t] == p1(*&&p'[t]<0*), H[t] -> 0]};
ic = {p[0] == p0, H[0] == 0};


Solving the equation:

sols = NDSolve[Join[ode, ic], {p[t], H[t]}, {t, 0, 30}, DiscreteVariables -> H];


Then, we can plot the solution:

GraphicsRow@{Plot[p[t] /. sols, {t, 0, 30}], Plot[H[t] /. sols, {t, 0, 30}]}


Note that if H0 is too small, the derivative won't change sign, and so it won't go back and forth:

H0 = 0.5;
sols = NDSolve[Join[ode, ic], {p[t], H[t]}, {t, 0, 30}, DiscreteVariables -> H];
GraphicsRow@{Plot[p[t] /. sols, {t, 0, 30}], Plot[H[t] /. sols, {t, 0, 30}]}


• I didn't know the existence of WhenEvent. That makes life so much easier. Probably there's no way to do such equation without it. Thank you so much for taking the time to help me! – Shay Mar 11 at 18:25

it always helps to provide values for these parameters so one does not have to guess. So I made up some values, which might not be right ones.

You can try WhenEvent

ClearAll[p, t, H];
p0 = 1/2;
p1 = 1;
p2 = 2;
H0 = 5;
k = 10;
r = 1/2;
H = 0;
ode = p'[t] == r p[t] (1 - p[t]/k) - H;
ic = p[0] == p0;
sol = NDSolve[{ode, ic,
WhenEvent[p[t] < p1, H = 0], WhenEvent[p[t] > p2, H = H0]}, p, {t, 0, 20}]


  Plot[Evaluate[p[t] /. sol], {t, 0, 20}]


• Sorry for not adding values. The ones you chose are perfectly fine for the example, though. However, I don't think the expression of $H(t)$ is correct. I may have expressed myself incorrectly. When $P(t)$ is below of $p_1$ then $H(t)=0$ until $P(t)$ is greater than $p_2$, and then $H(t)=H0$ until $P(t)$ is less than $p_1$. – Shay Mar 11 at 16:02
• Thank you for help! When Event is such a lifechanger. I hope I didn't bother you too much last time. It's true I wrote my problem in a really ambiguous way. – Shay Mar 11 at 18:28