3
$\begingroup$

I would like to use StopIntegration in WhenEvent, when more than one condition should be checked. For example, in the code below I would like to integrate until either q1[t] == 0 or q2[t] == 0

sol[s_] := 
 NDSolve[{q1'[t] == -q2[t], q2'[t] == q1[t], q1[0] == Cos[s], 
   q2[0] == Sin[s], 
   WhenEvent[
    q1[t] == 0 || q2[t] == 0, {tmax = t, "StopIntegration"}]}, {q1, 
   q2}, {t, 0, 10}]

But it does not work, when I try to check it

Manipulate[
 ParametricPlot[
  Evaluate[{q1[t], q2[t]} /. sol[s], {t, 0, tmax}, 
  PlotRange -> 1], {{s, Pi/4}, 0, 2 Pi}]

although it works perfectly if I leave only one of the conditions. Of course in this simple example I can just write q1[t]q2[t]==0, but I would like to understand how to proceed in general with more complex conditions.

Thank you in advance.

$\endgroup$
  • 4
    $\begingroup$ I think you should specify a list of events: WhenEvent[{q1[t] == 0, q2[t] == 0}, {youractions}] . Take a look at the "Scope" section of the WhenEvent docs, under "Events", for a similar example. $\endgroup$ – MarcoB Jun 10 '15 at 13:05
  • $\begingroup$ Thanks, it works. But it still a little bit strange for me. For example, q2[t] == 0 && q1[t] > 0 works, but q1[t] > 0 && q2[t] == 0 does not. $\endgroup$ – Ivan Jun 10 '15 at 13:20
  • $\begingroup$ I agree! So far I only managed to make one condition per WhenEvent expression work at a time. It would stand to reason that it should accept compound conditions, but either it just doesn't, or I have not found a way to coax it to do it yet... $\endgroup$ – MarcoB Jun 10 '15 at 22:08
  • 1
    $\begingroup$ Note that things like f == 0 and f == 0 && pred, also with <, >, and Mod, are special cases in WhenEvent (see mathematica.stackexchange.com/questions/39374/…), compared to the general pred, where the "event" is pred changing from False to True. $\endgroup$ – Michael E2 Jun 10 '15 at 22:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.