0
$\begingroup$

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?

$\endgroup$

1 Answer 1

3
$\begingroup$

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}]}

enter image description here

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}]}

enter image description here

$\endgroup$
1
  • $\begingroup$ 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! $\endgroup$
    – Shay
    Commented Mar 11, 2020 at 18:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.