# How can I use WhenEvent with multiple conditions inside NDSolve?

I would like to use WhenEvent but I'm not getting what I want: I set the initial condition var\epsilon=0; I want var\epsilon to change continuously over time, from to 0.1 if s'[t]>=0 or to -0.1 if s'[t]<0 whenever theta is greater than 2pi. I also need it to change to zero again when H[t] reaches the value Hf I define. Initially, H[t] is constant and equal to H0. When theta>=2pi, H[t] stops being constant. When Hf is reached, H[t] is constant and equal to Hf. Can someone help me? Thank you in advance.

 Hf=-0.4;
sol = NDSolve[{s''[t] == -(1/s[t]) + 1^2/s[t]^3 + var\[Epsilon][t],
var\[Epsilon]'[t] == 0, \[Theta]'[t] == 1/s[t]^2,
H'[t] == s'[t] var\[Epsilon][t], s[0] == 1,
s'[0] == 0, \[Theta][0] == 0, var\[Epsilon][0] == 0,
H[0] == -(1/2),
WhenEvent[s'[t] > 0,
If[\[Theta][t] >= 2 \[Pi], var\[Epsilon][t] -> 0.1]],
WhenEvent[s'[t] < 0,
If[\[Theta][t] >= 2 \[Pi], var\[Epsilon][t] -> -0.1]],
WhenEvent[H[t] == Hf, var\[Epsilon][t] -> 0]}, {s, s', \[Theta],
var\[Epsilon], H},
{t, 0, 100},
Method -> {"ExplicitRungeKutta", "DifferenceOrder" -> 4}];

• Your code checks for s'[t]>0, but your question indicated that the check should be for s'[t]>=0; that's a coding error, but I don't believe that it's the source of the problem. If I understand WhenEvent correctly, it's looking for the event to BECOME true, but your condition (s'[t]>=0) started as true, so WhenEvent is never triggered. As long as none of the events is triggered, all of your derivates (with the exception of [Theta]'[t]) remain zero so the system never changes. Apr 24, 2020 at 16:15

I don't know if this really solves your problem, but I added a WhenEvent to get things started.

Hf = -0.4;
sol = NDSolve[{Derivative[2][s][t] == -(1/s[t]) + 1^2/s[t]^3 +
var\[Epsilon][t],
Derivative[1][var\[Epsilon]][t] == 0,
Derivative[1][\[Theta]][t] == 1/s[t]^2,
Derivative[1][H][t] == Derivative[1][s][t]*var\[Epsilon][t],
s[0] == 1,
Derivative[1][s][0] == 0, \[Theta][0] == 0,
var\[Epsilon][0] == 0, H[0] == -(1/2),
WhenEvent[\[Theta][t] >= 2*Pi,
If[Derivative[1][s][t] >= 0, var\[Epsilon][t] -> 0.1]],
WhenEvent[Derivative[1][s][t] > 0,
If[\[Theta][t] >= 2*Pi, var\[Epsilon][t] -> 0.1]],
WhenEvent[Derivative[1][s][t] < 0,
If[\[Theta][t] >= 2*Pi, var\[Epsilon][t] -> -0.1]],
WhenEvent[H[t] == Hf, var\[Epsilon][t] -> 0]}, {s,
Derivative[1][s], \[Theta], var\[Epsilon], H},
{t, 0, 100},
Method -> {"ExplicitRungeKutta", "DifferenceOrder" -> 4}];