# NDSolve with Events that reevaluate with different Discrete Variables

I have the following problem:

I have a set of ODEs and some discrete variables which I can solve successfully. Now I want mathematica to check at every $$0.1 \,t$$ time step, wether it would increase the function $$url' [t]$$ to decrease the variable $$\eta[t]$$ and do so, if that's the case.

So I constructed the following

WhenEvent[Mod[t,0.1],If[url'[t] > With[{eta[t] -> 0.9 eta[t]}, url'[t]],
eta[t] -> 0.9 eta[t]]


However I get the error

"Variable NDSolveSetState[eta[t],0.9 eta[t]] in local
variable specification \{NDSolveSetState[eta[t],0.9 eta[t]} requires a value."


From what I understand, the braces somehow inhibit the SetState and following reevaluation of the function.

Thank you for your time :)

• Since url'[t] is treated as a variable, not as a function depending on eta[t], you might have to write the formula instead of With[..] (which has incorrect syntax in any case). Also, I can't decipher the error with the information given. I can see what went wrong, but I can't tell why or how to fix it without code that reproduces it. – Michael E2 Jan 13 '20 at 13:23
• @MichaelE2 thank you for your answer! I have found one example of adapting parameters, but I'm afraid, I don't understand it. mathematica.stackexchange.com/questions/122017/… From what I understand now, the problem is the sequence of evaluation steps does not allow to evaluate the functions twice at that step. – Wolfgang Schneider Jan 13 '20 at 13:26
• "...does not allow to evaluate the functions twice at that step": Yes, I think that is right. That's what I meant by url'[t] is treated as a variable. The value is computed once during a step and that value is used throughout the event processing. – Michael E2 Jan 13 '20 at 13:40
• do you know, wether there is a way, to change it? Basically alter the Solver? Best regards – Wolfgang Schneider Jan 13 '20 at 14:03
• I don't know of any way to change it. – Michael E2 Jan 14 '20 at 5:12

## 1 Answer

Here's a proof of concept. Problems with code usually require the code for the problem to be analyzed, so while it accomplishes what is described, I don't know if it can be adapted to the OP's case.

{sol} = NDSolve[{x'[t] == -y[t] - x[t]^3, y'[t] == x[t] - y[t]^3,
x == 1, y == 0}, {x, y}, {t, 0, 20}];
xp[x_, y_, e_] := -y - e x^3; (* RHS for x'[t] *)
{sol2} = NDSolve[{x'[t] == xp[x[t], y[t], eta[t]],
y'[t] == x[t] - y[t]^3,
x == 1, y == 0, eta == 1,
WhenEvent[Mod[t, 0.1],
If[xp[x[t], y[t], eta[t]] > xp[x[t], y[t], 0.9 eta[t]],
eta[t] -> 0.9 eta[t]]]},
{x, y, eta}, {t, 0, 20}, DiscreteVariables -> {eta}];
ParametricPlot[{x[t], y[t]} /. {sol, sol2} // Evaluate, {t, 0, 20}] Plot[eta[t] /. sol2, {t, 0, 20}] • Thank you very! I'll have a look into it immediately – Wolfgang Schneider Jan 13 '20 at 14:19