1
$\begingroup$

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 NDSolve`SetState[eta[t],0.9 eta[t]] in local 
variable specification \{NDSolve`SetState[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 :)

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – Michael E2 Jan 13 at 13:23
  • $\begingroup$ @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. $\endgroup$ – Wolfgang Schneider Jan 13 at 13:26
  • $\begingroup$ "...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. $\endgroup$ – Michael E2 Jan 13 at 13:40
  • $\begingroup$ do you know, wether there is a way, to change it? Basically alter the Solver? Best regards $\endgroup$ – Wolfgang Schneider Jan 13 at 14:03
  • $\begingroup$ I don't know of any way to change it. $\endgroup$ – Michael E2 Jan 14 at 5:12
2
$\begingroup$

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[0] == 1, y[0] == 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[0] == 1, y[0] == 0, eta[0] == 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}]

enter image description here

Plot[eta[t] /. sol2, {t, 0, 20}]

enter image description here

|improve this answer|||||
$\endgroup$
  • $\begingroup$ Thank you very! I'll have a look into it immediately $\endgroup$ – Wolfgang Schneider Jan 13 at 14:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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