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)"]