# WhenEvent stops working after first instance

I'm modelling a very simple physical situation is which an object is rested on two moving fingers. Sometimes the object moves with one finger while sliding over the other, while at other critical times the roles are reversed. I've tried to use two instances of WhenEvent to capture this switching but each of them are only executing once each.

Mass := 20(*Mass of object*)
Gravity := 9.8(*gravity*)
Muk[x_] := 1(*kinetic friction*)
Mus[x_] := 3 (*static friction*)
Phi[x_] := Mus[x]/Muk[x]
F1[t_] := -1 + t(*position of finger 1*)
F2[t_] := 1 - t (*positon of finger 2*)
N1[x_, y_, z_] :=
Mass Gravity (z - y)/(z - x) (*normal force @ finger 1*)
N2[x_, y_, z_] :=
Mass Gravity (y - x)/(z - x) (*normal force @ finger 2*)
X0 := 0.5 (*initial position of centroid*)

Sol = NDSolve[{x'[t] == a[t] F1'[t] + b[t] F2'[t], x == 0.2, a == 0,
b == 1,
WhenEvent[
Muk[F1[t]] N1[F1[t], x[t], F2[t]] ==
Mus[F2[t]] N2[F1[t], x[t], F2[t]], {a[t], b[t]} -> {1, 0}],
WhenEvent[
Muk[F2[t]] N2[F1[t], x[t], F2[t]] ==
Mus[F1[t]] N1[F1[t], x[t], F2[t]], {a[t], b[t]} -> {0, 1}]},
x, {t, 0, 1}, DiscreteVariables -> {a, b}];

Plot[{x[t] /. Sol, F1[t],
F2[t], (Phi[F1[t]] - 1)/(Phi[F1[t]] + 1) (1 -
t), (1 - Phi[F1[t]])/(Phi[F1[t]] + 1) (1 - t)}, {t, 0, 1},
PlotStyle -> {Black, Gray, Red, Orange, Blue}]
Plot[{Muk[F1[t]] N1[F1[t], x[t] /. Sol, F2[t]],
Mus[F2[t]] N2[F1[t], x[t] /. Sol, F2[t]],
Mus[F1[t]] N1[F1[t], x[t] /. Sol, F2[t]],
Muk[F2[t]] N2[F1[t], x[t] /. Sol, F2[t]]}, {t, 0, 1},
PlotStyle -> {Black, Gray, Red, Orange}]


The first event occurs when the black curve in the right hand image below touches/crosses the grey curve. The event causes the black line to bounce off the blue line in the left hand image. Likewise, the second event occurs when the orange line in the right hand image belwo touches/crosses the red curve. The event causes the black line to bounce off the orange line in the left hand image.

I'm meant to get graphs that look like the following: Does anyone know why the events are only being triggered once?

• When I run your code I see a Power::infy: "Infinite expression 1/0. encountered." Is that not relevant? – user21 Oct 1 '15 at 13:24
• Welcome to Mathematica.SE! 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! – user9660 Oct 1 '15 at 13:38
• Following up on @user21's comment: Change all of your upper time limits (in NDSolve and in Plot) from 1 to 0.999 and see what happens. – march Oct 1 '15 at 16:36
• @user21, Thanks for looking. That error only occurs because the WhenEvent works only once. Otherwise the error can't actually happen. – Auslander Oct 1 '15 at 22:33

It turns out that if I use a different detection method, I get the intended results. So it turns out that it wasn't just executing the event once; rather it simply wasn't seeing the subsequent events. I used "DetectionMethod" -> "Interpolation".