6
$\begingroup$

The first example on the WhenEvent help page is

NDSolve[{y''[t] == -9.81, y[0] == 5, y'[0] == 0, 
   WhenEvent[y[t] == 0, y'[t] -> -0.95 y'[t]]}, y, {t, 0, 10}];

Say I want to stop the integration at the first bounce, I would put

NDSolve[{y''[t] == -9.81, y[0] == 5, y'[0] == 0, 
   WhenEvent[y[t] == 0,"StopIntegration"]}, y, {t, 0, 10}];

That, however, does not stop the integration (i.e. it goes on to t=10), neither does the condition y[t] < 0. What am I doing wrong?

$\endgroup$
0

1 Answer 1

8
$\begingroup$

Try this:

ySol = NDSolveValue[{y''[t] == -9.81, y[0] == 5, y'[0] == 0, 
   WhenEvent[y[t] == 0, {tMax = t, "StopIntegration"}]}, 
  y, {t, 0, 10}];
Plot[ySol[x], {x, 0, tMax}]

And in tMax you'll get 1.00964, that's slightly larger than t when y[t] = 0.

plot

$\endgroup$
3
  • $\begingroup$ great, that works! $\endgroup$
    – jacob
    Aug 7, 2014 at 13:14
  • $\begingroup$ @jacob You're code works too, I think you just want to plot to 10 and it will plot, because it's an InterpolationFunction and can plot outside the defined range. $\endgroup$
    – m0nhawk
    Aug 7, 2014 at 13:15
  • $\begingroup$ ok thanks, that's what was confusing me $\endgroup$
    – jacob
    Aug 7, 2014 at 13:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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