Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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 )

share|improve this question
    
If you want to update a why not use a ParametricNDSolve? It might be good, if could show what you want to do. –  user21 Jul 29 '13 at 6:40

1 Answer 1

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}]
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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