Message Boards Message Boards

0
|
4772 Views
|
4 Replies
|
2 Total Likes
View groups...
Share
Share this post:

[?] Reset initial conditions at a specific event with NDSolve ?

Anonymous User
Anonymous User
Posted 6 years ago

Hello there, I have a simple single degree of freedom spring mass damper system. I aiming to reset my initial condition ( coordinates ) at a specific event when met. For example when my displacement x(t) = -4 I would like to reset the system or my initial conditions ( coordinates ) at that specific time and replace the new initial conditions at that time when the event happens. I tried to us the Switch function but I seem to not properly accommodate it in my code. Thank you

m = 1;
k = 2000;
c = 5;
eqn = m x''[t] + c x'[t] + k x[t] == 50;
initialconditions = {x[0] == 5, x'[0] == 1};
switch = WhenEvent[x[t] == -4, 
   Print["t=", t, "  x(0)=", x[t], "    x'(0)=", x'[t]]];
solution = 
  NDSolve[{eqn, initialconditions, switch}, {x[t], x'[t]}, {t, 0, 5}];
Plot[Evaluate[x[t]] /. solution, {t, 0, 5}, PlotRange -> All, 
 PlotLabel -> "x(t)"]
Plot[Evaluate[x'[t]] /. solution, {t, 0, 5}, PlotRange -> All, 
 PlotLabel -> "v(t)"]
POSTED BY: Anonymous User
4 Replies

Joseph,

This will do what you want:

switch = WhenEvent[
   x[t] == -4, {Print["t=", t, "  x(0)=", x[t], "    x'(0)=", x'[t]], 
    x'[t] -> 2*x'[t], x[t] -> -2, 
    Print["After bump: t=", t, "  x(0)=", x[t], "    x'(0)=", x'[t]]}];
solution = 
  NDSolve[{eqn, initialconditions, switch}, {x[t], x'[t]}, {t, 0, 
    5}];
Plot[Evaluate[x[t]] /. solution, {t, 0, .4}, PlotRange -> All, 
 PlotLabel -> "x(t)"]
Plot[Evaluate[x'[t]] /. solution, {t, 0, .4}, PlotRange -> All, 
 PlotLabel -> "v(t)"]

Regards,

Neil

POSTED BY: Neil Singer
Anonymous User
Anonymous User
Posted 6 years ago

Dear Neil can you please explain to me what you did exactly I don't seem to understand the following part

x'[t] -> 2*x'[t], x[t] -> -2, 
    Print["After bump: t=", t, "  x(0)=", x[t], "    x'(0)=", x'[t]]

Thanks

POSTED BY: Anonymous User

Joseph,

The Whenevent has several parts:

WhenEvent[
   x[t] == -4, {Print["t=", t, "  x(0)=", x[t], "    x'(0)=", x'[t]], 
    x'[t] -> 2*x'[t], x[t] -> -2, 
    Print["After bump: t=", t, "  x(0)=", x[t], "    x'(0)=", x'[t]]}];

the first part

x[t] == -4

is a condition so the event happens when the position hits -4. you can trigger on velocit (x'[t]) or anything else by changing this constraint. The next part is a list. The list contains a series of actions you are going to do when you hit the event (in this case when x[t] == -4.) First, I printed a message so I can see what is happening at the beginning of the event:

Print["t=", t, "  x(0)=", x[t], "    x'(0)=", x'[t]]

I get the time of the event and the x[t] and x'[t] values. The next thing I do is alter the states of the system (as I believe that you wanted to do). First I changed the position instantaneously and then I changed the velocity instantaneously.The changes make no physical sense but they showed what you can do:

x'[t] -> 2*x'[t]

instantaneously changes the velocity to be 2 times the current velocity at the when event (x[t]==-4). If you look at the graph you will see the velocity double at the instant the position hits -4. Next

x[t] -> -2

sets the position to jump from the current value of -4 to the new value of -2.

The last step prints out a message to show the new, updated values that I just changed. If you look at the plots, you can see the simulation pick up at the event with the new X and X' values. Obviously you can put in more meaningful changes to your states.

I hope this helps.

Regards,

Neil

POSTED BY: Neil Singer
Anonymous User
Anonymous User
Posted 6 years ago

Great and very helpful thank you . Joe

POSTED BY: Anonymous User
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