Message Boards Message Boards

0
|
3327 Views
|
6 Replies
|
0 Total Likes
View groups...
Share
Share this post:

[?] Solve a differential equation using WhenEvent?

Posted 7 years ago

Hi, I have to solve this equation but I don't know how to use "WhenEvent" command:

P'[t]= r (1-P[t]/K) P[t] - H[t]

P[0]=P0

I have:

r = 1;

K = 10^6;

H0 = 4 x 10^5;

p1 = 5 x 10^5;

p2 = 8 x 10 ^ 5;

P0 = 3 x 10^5;

0 <= t <= 20

The question is: H[t] = 0 if 0 <=P[t] <= p2

But, when P[t]= p2, then H[t] = H0, until P[t]= p1, when (again) H[t] = 0

If P[t] = p2, again H[t] = H0 and so on

I know to use NDSolve to solve a differential equation, but I don't know how to use "WhenEvent"

Thanks.

POSTED BY: Jose Luis Lara
6 Replies
Posted 7 years ago

Thank you very much for your help

POSTED BY: Jose Luis Lara

In the error I see It says to set the options because your event includes the highest derivative:

Method -> {"EquationSimplification" -> "Residual"}

then the first one executes just fine...

POSTED BY: Sander Huisman
Posted 7 years ago

Where exactly do i put that?

I've never used this command before

POSTED BY: Jose Luis Lara

after your time specification, as an option.

 NDSolve[{p'[t] == r (1 - p[t]/k) p[t], p[0] ==3 10^5, WhenEvent[p[t] == p2, p'[t] -> p'[t] - 4 10^5], WhenEvent[p[t] == p1, p'[t] -> p'[t]]}, p[t], {t, 0, 20},Method -> {"EquationSimplification" -> "Residual"}]

If you don't know what options are, please read the elementary introduction to Wolfram language book, free online, no signing up, just go.

POSTED BY: Sander Huisman
Posted 7 years ago

Sorry, its the first time that i comment here:

p1 = 5 10^5;
p2 = 8 10^5;
r = 1;
k = 10^6 
 NDSolve[{p'[t] == r (1 - p[t]/k) p[t], p[0] ==3 10^5, WhenEvent[p[t] == p2, p'[t] -> p'[t] - 4 10^5], WhenEvent[p[t] == p1, p'[t] -> p'[t]]}, p[t], {t, 0, 20}] 

It didnt work, i have divided the problem and focused in the first part of the curve, i intend at least that when P[t]==p2, H[t]=H0:

i've tried doing that:

p1 = 5 10^5;
p2 = 8 10^5;
r = 1;
k = 10^6;
 NDSolve[{p'[t] == r (1 - p[t]/k) p[t], p[0] ==3 10^5, WhenEvent[p[t] == p2, p'[t] -> p'[t] - 4 10^5]}, p[t], {t, 0, 20}]

Or that:

NDSolve[{p'[t] == r (1 - p[t]/k) p[t]-H[t], p[0] == 10^5, WhenEvent[p[t] == p2,H[t]=H0]}, p[t], {t, 0, 20}]

But it gives me error, i'm making a mistake but in the "help" of mathmatica i only see examples about product of this type:

NDSolve[{y''[t]==-9.81,y[0]==5,y'[0]==0, WheEvent[y[t]==0, y[t]->-0.95 y'[t]]}, y, {t,0,10}]

Or with "Mod"

NDSolve[{y''[t] == -9.8, y[0] == 13.5, y'[0] == 5, a[0] == 13, 
   WhenEvent[y[t] - a[t] == 0, y'[t] -> -c y'[t]], 
   WhenEvent[Mod[t, 1], a[t] -> a[t] - 1]}, {y, a}, {t, 0, 8}, 
  DiscreteVariables -> {a}] ;
POSTED BY: Jose Luis Lara

Here are at least 50 examples of WhenEvent:

https://reference.wolfram.com/language/ref/WhenEvent.html

What have you tried? I don't see any code?

POSTED BY: Sander Huisman
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract