Message Boards Message Boards

WhenEvent issue with vector valued diffeq

Posted 10 years ago

Below is some code that numerically solves a 2D lossless ballistic flight. r[t] is essentially {x[t],y[t]} as a 2-vector. I try to stop integration when the projectile hits the ground as given by r[t][[2]]<0, but WhenEvent does not seem to recognize the syntax. Is there something wrong with my code?

eqs = {r''[t] == -{0, 1}, r'[0] == {2, 3}, r[0] == {0, 0}, 
   WhenEvent[r[t][[2]] < 0, "StopIntegration"; tf = t]};

sol = NDSolveValue[eqs, r, {t, 0, 8}];

Plot[{sol[t][[1]], sol[t][[2]]}, {t, 0, 8}]

enter image description here

POSTED BY: David Keith
4 Replies

Hi,

In you example the domain of sol is (0.,6.), looks like when interpolation function is plotted it's values outside the domain are extrapolated.

I.M,

POSTED BY: Ivan Morozov
Posted 10 years ago

Thank you, Ivan. But I think the domain I get for the interpolating function solution is {0,8}, which is the full extent of the integration time, without stopping at t=6, where r[t][[2]] becomes negative from above.

POSTED BY: David Keith

Yes, sorry, I removed tf = t, if kept the code produce wrong domain. This modification gives correct domain and tf (the plot is still wrong):

eqs = {r''[t] == -{0, 1}, r'[0] == {2, 3}, r[0] == {0, 0}, 
   WhenEvent[r[t][[2]] < 0, {"StopIntegration", tf = t}]};
sol = NDSolveValue[eqs, r, {t, 0, 8}];
sol["Domain"]
tf

{{0., 6.}}

6.

I.M.

POSTED BY: Ivan Morozov
Posted 10 years ago

Yes! Thank you, Ivan. That works fine. And the plot is correct if plotted {t,0,tf}. The mistake was mine in thinking a semicolon could separate parts of a compound statement as actions for WhenEvent. The documentation says actions in a list are evaluated sequentially.

Best regards, David

POSTED BY: David Keith
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract