I have a 'WhenEvent' where I want two actions to be performed. I thought the actions should be in a list but this does not seem to work. Here is an example there are two actions in the 'WhenEvent'.
(* Preliminary definition *)
ClearAll[ip, v1, v2];
ip[t_] :=
Piecewise[{{Sin[2 \[Pi] 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`,
WhenEvent[{z[t] - y[t] <= -0.125, z[t] - y[t] >= 0.125},
v1 = 0.9 (0.95 y'[t] + 0.15 z'[t]);
v2 = 0.9 (1.5 y'[t] - 0.4 z'[t]);
{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}]}]
By putting in 'Print[]' I can see that the ''WhenEvent' has been identified but no actions have been implemented.
If I modify my code with two 'WhenEvent's each with one action then I get what I want.
(* Preliminary definition *)
ClearAll[ip, v1, v2];
ip[t_] :=
Piecewise[{{Sin[2 \[Pi] 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`,
WhenEvent[{z[t] - y[t] <= -0.125, z[t] - y[t] >= 0.125},
v1 = 0.9 (0.95 y'[t] + 0.15 z'[t]);
v2 = 0.9 (1.5 y'[t] - 0.4 z'[t]);
Derivative[1][y][t] -> v1],
WhenEvent[{z[t] - y[t] <= -0.125, z[t] - y[t] >= 0.125},
v1 = 0.9 (0.95 y'[t] + 0.15 z'[t]);
v2 = 0.9 (1.5 y'[t] - 0.4 z'[t]);
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}]}]
How can I have several actions associated with one 'WhenEvent'? Am I doing something silly here or is this deeper?
Edit - problem fixed -simple error
I have found the error. It seems that the events must be in one list and the actions in another list. Further, and what eluded me, any associated calculations must also be within the list.
Here is the corrected code.
(* Preliminary definition *)
ClearAll[ip, v1, v2];
ip[t_] :=
Piecewise[{{Sin[2 \[Pi] t/(2 0.1)], t < 0.1}, {0, 0.1 <= t}}];
(* The differential equations *)
eqn = {986. y[t] + 6. Derivative[1][y][t] + (y^\[Prime]\[Prime])[t] ==
986 ip[t] + 6. ip'[t], (z^\[Prime]\[Prime])[t] == -9.81`,
WhenEvent[{z[t] - y[t] <= -0.125, z[t] - y[t] >= 0.125}, {
v1 = 0.9 (0.95 y'[t] + 0.15 z'[t]);
v2 = 0.9 (1.5 y'[t] - 0.4 z'[t]);,
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}]}]
So problem solved. But I still have to work out why the last two pictures are different. Sorry if I waisted your time.
(y^\[Prime]\[Prime])
as y'' $\endgroup$