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

Posted 11 months ago
1078 Views
|
4 Replies
|
2 Total Likes
|
 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)"]
4 Replies
Sort By:
Posted 11 months ago
 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 11 months 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