9
$\begingroup$

I am using NDSolve for problems like this:

sols = NDSolve[{(1 + 25 Exp[- u[s]^2] u[s]^2) u'[s]^2 == u[s]^2, u[0] == 0.5,
   WhenEvent[u[s] == 4, "StopIntegration"]}, u, {s, 0, 20}]

Plot[u[s] /. sols, {s, 0, 5}, Evaluated -> True]

enter image description here

There are multiple solution branches, I am only interested in the one which was stopped by the WhenEvent, i.e. the one which reaches $u=4$ for some value of $s$.

Is there a convenient way to restrict the solution set to only those ones for which the WhenEvent was triggered?

$\endgroup$
  • 5
    $\begingroup$ This doesn't seem like a very scalable answer, but what about something like Select[u /. sols, #[#["Domain"][[1, 2]]] == 4 &] ? $\endgroup$ – Chip Hurst Apr 26 '16 at 18:12
  • $\begingroup$ @ChipHurst, it might be the best way nevertheless. Thanks for the suggestion. $\endgroup$ – Simon Woods Apr 26 '16 at 18:30
8
$\begingroup$

This might be useful if your actual event is more complicated, use a discrete variable as a flag:

sols = NDSolve[{(1 + 25 Exp[-u[s]^2] u[s]^2) u'[s]^2 == u[s]^2, 
   u[0] == 0.5, a[0] == 1,
   WhenEvent[u[s] == 4, a[s] -> 0], 
   WhenEvent[a[s] == 0, "StopIntegration"]
        }, {u, a}, {s, 0, 20}, 
     DiscreteVariables -> {a[s]}]

u /. Select[sols, Last@((a /. #)["ValuesOnGrid"]) == 0 &]

I think the method suggested by @ChipHurst in comment is better for this example though.

$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.