Edit
If we want to use v1
and v2
,we can use With
to enclose WhenEvent
Clear["Global`*"];
(*Preliminary definition*)ClearAll[ip, v1, v2];
ip[t_] :=
Piecewise[{{Sin[2 π t/(2 0.1)], t < 0.1}, {0, 0.1 <= t}}];
(*The differential equations*)
eqn = {986. y[t] + 6. Derivative[1][y][t] + y''[t] ==
986 ip[t] + 6. ip'[t], z''[t] == -9.81`,
With[{v1 = 0.9 (0.95 y'[t] + 0.15 z'[t]),
v2 = 0.9 (1.5 y'[t] - 0.4 z'[t])},
WhenEvent[{z[t] - y[t] <= -0.125,
z[t] - y[t] >= 0.125}, {Derivative[1][y][t] -> v1,
Derivative[1][z][t] -> v2}]], y[0] == 0,
Derivative[1][y][0] == 0, z[0] == 0, Derivative[1][z][0] == 0};
(*Sove the equation*)
sol = First@NDSolve[eqn, {y, z}, {t, 0, 0.04}];
(*Plot the result*)
Plot[Evaluate[z[t] - y[t] /. sol], {t, 0, 0.04},
Epilog -> {Pink, InfiniteLine[{0, -0.125}, {1, 0}]}]
Original
When we remove v1,v2
, all of them work fine.