Message Boards Message Boards

Update initial conditions using WhenEvent in NDSolve?

GROUPS:

Hello there, I would like to know how can I update my initial conditions when an event happen. Technically, when a specific event happen I want to reset my coordinates by updating my initial conditions at that point like restarting my system again but with different initial conditions every time the event happens and those new initial conditions are the values at that event. I have a simple second order ODE with Whenevent. Here is the code

In[203]:= IC = {x[0] == 0.01, x'[0] == 1};
con = WhenEvent[x[t] == 0, Print["t=", t, "    x(t)=", x[t]]];
sol = NDSolve[{x''[t] + x'[t] + 10 x[t] == 0, IC, con}, {x[t], 
    x'[t]}, {t, 0, 3}];
Plot[x[t] /. sol, {t, 0, 3}]

During evaluation of In[203]:= t=0.996168    x(t)=-2.60209*10^-17

During evaluation of In[203]:= t=2.00228    x(t)=3.29597*10^-17

Out[206]= 

Regards, Joe

POSTED BY: Joeseph A
Answer
16 days ago

Joe,

You can add the changes you want to the WhenEvent as a list and using -> to set the values. You do not need the print (only if you want it). so for example, I changed the sign of the first derivative when it hit the origin.

IC = {x[0] == 0.01, x'[0] == 1};
con = WhenEvent[x[t] == 0, {Print["t=", t, "    x(t)=", x[t]],
   x'[t] -> -x'[t]}];
sol = NDSolve[{x''[t] + x'[t] + 10 x[t] == 0, IC, con}, {x[t], 
    x'[t]}, {t, 0, 3}];
Plot[x[t] /. sol, {t, 0, 3}]

Regards,

Neil

POSTED BY: Neil Singer
Answer
16 days ago

Joseph,

You asked this same question a month ago. your old post. What is the story???

POSTED BY: Neil Singer
Answer
16 days ago

Group Abstract Group Abstract