I'm trying to integrate an equation with a periodic solution using NDsolve
. I want to stop the integration after the derivative of my solution has become zero for the $n$-th time (in my example code, $n=5$). For this purpose I included a "counter" variable $i$ in WhenEvent
. In principle everything works fine, except that here "StopIntegration"
is not recognized. I guess it has to do something with the fact that "StopIntegration"
is wrapped within an If
statement. However, in principle "StopIntegration"
should be read by Mathematica (as Print["Integration stopped at t=", tend]
is) and NDSolve should stop? Below is a simple minimal working example of my Problem.
Module[{i = 0},
First@NDSolve[{D[x[t], t] == 2 π y[t],
D[y[t], t] == -2 π x[t], x[0] == 0, y[0] == 1,
WhenEvent[y[t] == 0,
If[i >= 4, {tend = t, "StopIntegration",
Print["Integration stopped at t=", tend]}, i += 1]],
WhenEvent[t == 10, tend = t]}, {x, y}, {t, 0, 10},
Method -> "LSODA"]]
Any help would be highly appreciated.