1
$\begingroup$

This is an example in Mathematica's help document.

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}];
Plot[y[t] /. %, {t, 0, 10}]

I do a modification like this:

NDSolve[{y''[t] == -9.81, y[0] == 5, y'[0] == 0, a = y[t]; b=y'[t];
   WhenEvent[a == 0, b -> -0.95 b]}, y, {t, 0, 10}];
Plot[y[t] /. %, {t, 0, 10}]

It doesn't work.

If I want to keep a new symbol just as here a to represent the y[t] or some expression about y[t] in the WhenEvent, what can I do to achieve this goal?

(The error may be caused by the HoldAll attribute of WhenEvent. Why I want to replace the y[t] by a? It is important to me. Bucause I will use a dynamic a in the WhenEvent )

$\endgroup$
1
  • $\begingroup$ If you want to update a why not use a ParametricNDSolve? It might be good, if could show what you want to do. $\endgroup$
    – user21
    Commented Jul 29, 2013 at 6:40

1 Answer 1

1
$\begingroup$

It's not quite clear what you are looking for, how about using a function:

test[a_?NumericQ] := a + 1
NDSolve[{y''[t] == -9.81, y[0] == 5, y'[0] == 0, 
   WhenEvent[test[y[t]] == 0, y'[t] -> -0.95 y'[t]]}, y, {t, 0, 10}];
Plot[y[t] /. %, {t, 0, 10}]
$\endgroup$

Your Answer

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

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