# WhenEvent issue with vector valued diffeq

Posted 9 years ago
6265 Views
|
4 Replies
|
2 Total Likes
|
 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}] 
4 Replies
Sort By:
Posted 9 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 9 years ago
 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 9 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 9 years ago
 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,