I am trying to solve a set of ODE's with switching. I implemented using WhenEvents.
xd = {1/√6, √5/√6};
x0 = {1/√2, 1/√2};
min = -0.0001;
max = 0.0001;
tmax = 10;
sol = NDSolve[{
x1'[t] == -x2[t]*u1[t],
x2'[t] == x1[t]*u1[t],
WhenEvent[xd[[1]]* x2[t] - xd[[2]] x1[t] > max, u1[t] -> -1],
WhenEvent[xd[[1]]* x2[t] - xd[[2]] x1[t] < min, u1[t] -> 1],
x1[0] == x0[[1]], x2[0] == x0[[2]], u1[0] == -1},
{x1, x2, x3, u1}, {t, 0, tmax}, DiscreteVariables -> {u1}]
Plot[{xd[[1]], xd[[2]], x1[t] /. sol, x2[t] /. sol}, {t, 0, tmax},
PlotPoints -> 10000]
Plot[u1[t] /. sol, {t, 0, tmax}, PlotPoints -> 10000]
How ever, I need to another event WhenEvent[ xd[[1]]* x2[t] - xd[[2]] x1[t] < max && xd[[1]]* x2[t] - xd[[2]] x1[t] > min, u1[t] -> 0], it takes hours and does not produce results.
I think, the last event is raising too many events, so it's taking a lot of time. Is there a work around for this?


WhenEvent[Null; <ineq.>, u1[t] -> 0]from my answer in the link does what you want? – Michael E2 Sep 20 '20 at 21:55f(x)>eps,f(x)<-epsit works. I want to include the event-eps<f(x)<eps. – kosa Sep 20 '20 at 23:30