0
$\begingroup$
yb[a_?NumericQ] := a^2;
eqns = {x'[s] == -yb[s], WhenEvent[x'[s] > 0, {yb[s] -> - yb[s]}], 
  x'[0] == 0}
NDSolve[eqns, x[s], {s, -50, 50}]

The WhenEvent doesn't evaluate x'[s] correctly,is there anyway to make this work?

$\endgroup$
  • 1
    $\begingroup$ Is this an example or a real problem? $\endgroup$ – Alex Trounev Jan 3 at 20:06
  • $\begingroup$ this is example,but actually I need to evaluate the sign of first derivative when solving first-order partial differential equation... $\endgroup$ – Xminer Jan 3 at 20:10
  • $\begingroup$ The problem is incorrect, so you need to change the question. Add a real example. $\endgroup$ – Alex Trounev Jan 3 at 20:24
4
$\begingroup$

You cannot change the functiondefinition yb[a] inside a WhenEvent!

Try

eqns = {x'[s] == - sgn[s] yb[s], sgn[0] == +1,WhenEvent[x'[s] > 0, {sgn[s] -> -sgn[s] }], x'[0] == 0}
{X, SGN} =NDSolveValue[eqns, {x, sgn} , {s, -50, 50}, DiscreteVariables -> sgn]
$\endgroup$
  • $\begingroup$ Thanks,this seems perfect for my problem. May I ask some questions for study? 1) why cannot change yb[a] inside WhenEvent? 2) why we need to introduce sgn function into the equation and specify it for DiscreteVariables Option? $\endgroup$ – Xminer Jan 3 at 20:35
  • 2
    $\begingroup$ yb[s] is an extern function , WhenEvent can only change state variables of NDSolve. $\endgroup$ – Ulrich Neumann Jan 3 at 20:43

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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