Group Abstract Group Abstract

Message Boards Message Boards

0
|
5.3K 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 8 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
POSTED BY: Neil Singer
Anonymous User
Anonymous User
Posted 8 years ago

Great and very helpful thank you . Joe

POSTED BY: Anonymous User

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 8 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
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard