# How do I define and use variables within WhenEvent in NDSolve

**URL:** <https://community.wolfram.com/t/how-do-i-define-and-use-variables-within-whenevent-in-ndsolve/22152>\
**Category:** Questions\
**Tags:** calculus, equation-solving, numerical-computation\
**Created:** [May 31, 2023, 8:39pm UTC](https://community.wolfram.com/t/how-do-i-define-and-use-variables-within-whenevent-in-ndsolve/22152 "2023-05-31T20:39:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kadenjtro](https://avatars.discourse-cdn.com/v4/letter/k/b487fb/32.png) [@kadenjtro](https://community.wolfram.com/u/kadenjtro)\
**Post date:** [May 31, 2023, 8:39pm UTC](https://community.wolfram.com/t/how-do-i-define-and-use-variables-within-whenevent-in-ndsolve/22152/1 "2023-05-31T20:39:37Z")

</div>

Hi!

I’m having a really bizarre issue with WhenEvent–when I define a variable as some equation involving the variables that are defined and used in NDSolve, I get the following error: NDSolve::nbnum1: The function value Abs[x[t]-y[t]]\>0.01 is not True or False when the arguments are {0.,0.3,0.2,0.3,0.2}.

However, when I input the equation as it is (not previously defined as a variable), the solver runs fine. Here’s a simplified version of my code.

```
variable = Sqrt[Abs[x[t] - y[t]]^2]
times = {};
Print["Current time step in ODE solution ="];
Monitor[
  s = NDSolve[{x'[t] == x[t], y'[t] == y[t], y[0] == 0.2, x[0] == 0.3,
      WhenEvent[variable > 0.01, Print[2]]}, {x[t], y[t]}, {t, 0, 
     tAnneal}, Method -> {"EquationSimplification" -> "Residual"}, 
    EvaluationMonitor :> AppendTo[times, t]], times[[-1]] ];

```

This version throws an error. However, when I input

```
times = {};
Print["Current time step in ODE solution ="];
Monitor[
  s = NDSolve[{x'[t] == x[t], y'[t] == y[t], y[0] == 0.2, x[0] == 0.3,
      WhenEvent[Sqrt[Abs[x[t] - y[t]]^2] > 0.01, Print[2]]}, {x[t], 
     y[t]}, {t, 0, tAnneal}, 
    Method -> {"EquationSimplification" -> "Residual"}, 
    EvaluationMonitor :> AppendTo[times, t]], times[[-1]] ];

```

it works fine. This is an issue for me because I have a big datastructure with such equations for many different variables, and I need to be able to call an element of that list rather than writing in the equation itself.

Thank you for any help you all can give me!  
Kaden

---

<div class="post-metadata">

**Author:** ![mroge02](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/mroge02/32/28775_2.png) [@mroge02](https://community.wolfram.com/u/mroge02)\
**Post date:** [May 31, 2023, 10:18pm UTC](https://community.wolfram.com/t/how-do-i-define-and-use-variables-within-whenevent-in-ndsolve/22152/2 "2023-05-31T22:18:27Z")

</div>

Use:

```
With[{variable = variable}, WhenEvent[variable > 0.01, Print[2]]]

```

or

```
WhenEvent[Evaluate[variable > 0.01], Print[2]]

```

`NDSolve` makes a literal analysis of the code, replacing occurrences of state variables like `x[t]` and `y[t]` by symbols that represent their values at each time step. Since `WhenEvent` has the attribute `HoldAll`, the code in the first argument of `WhenEvent[variable > 0.1,...]` is literally the unevaluated `variable > 0.1`. So when `NDSolve` sets up the internal handler for the event, there appear to be no instances of `x[t]` or `y[t]` to be replaced, Later `variable` is evaluated, but the values have been mapped to internal variables. The expressions `x[t]` etc. remain `x[t]` etc., and you get an error “`Abs[x[t]-y[t]]>0.01` is not `True` or `False`”.

The trick above in both cases above is to replace the unevaluated `variable` with its value.

---

<div class="post-metadata">

**Author:** ![kadenjtro](https://avatars.discourse-cdn.com/v4/letter/k/b487fb/32.png) [@kadenjtro](https://community.wolfram.com/u/kadenjtro)\
**Post date:** [June 1, 2023, 2:20pm UTC](https://community.wolfram.com/t/how-do-i-define-and-use-variables-within-whenevent-in-ndsolve/22152/3 "2023-06-01T14:20:29Z")

</div>

Thank you so much! I figured there was something wrong about the order in which it replaced x[t] vs other variables but I had no idea how to fix it. I really appreciate it!

---

<div class="post-metadata">

**Author:** ![mroge02](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/mroge02/32/28775_2.png) [@mroge02](https://community.wolfram.com/u/mroge02)\
**Post date:** [June 1, 2023, 4:23pm UTC](https://community.wolfram.com/t/how-do-i-define-and-use-variables-within-whenevent-in-ndsolve/22152/4 "2023-06-01T16:23:44Z")

</div>

You’re welcome!
