7

3

I'm trying to use the new WhenEvent functionality of NDSolve in Mathematica 9, in order to perform an action when a variable in the system reaches a certain value. I would like this variable to come from an external variable, like so:

```
var = y; (* this could come from somewhere else *)
list = {};
(* keep track of values of x for which y[x] == 1 *)
NDSolve[{y''[x] == -y[x], y[0] == 1, y'[0] == 0,
WhenEvent[Round[var[x], 0.1] == 1, AppendTo[list, x]]},
y, {x, 0, 10}]
```

But this produces the errors

NDSolve::nbnum1: The function value Round[y[0.],0.1]==1 is not True or False when the arguments are {0.,1.,0.,0.,-1.}. >>

NDSolve::nbnum1: The function value Round[y[0.000143737],0.1]==1 is not True or False when the arguments are {0.000143737,1.,-0.000143737,-0.000143737,-1.}. >>

NDSolve::nbnum1: The function value Round[y[0.000287473],0.1]==1 is not True or False when the arguments are {0.000287473,1.,-0.000287473,-0.000287473,-1.}. >>

General::stop: Further output of NDSolve::nbnum1 will be suppressed during this calculation. >>

How can I use an "external" variable to specify which variable to watch in WhenEvent?

As a general technique, would

`WhenEvent[event,action]/.event->PUT YOUR EVENT HERE`

work? – Chris K – 2016-03-07T03:25:20.920My personal favourite is a replacement rule but the other way around,

`var /. var_ :> Reap[...`

– Rojo – 2012-12-13T08:36:37.430