I have found a weird problem using If conditions containing an state inequality of the form state<=
.
First consider the simple ODE with an If condition t<=2
. We first process the equations.
state = First@NDSolve`ProcessEquations[{r'[t] == If[t <= 2, 2, 0], r[0] == 0},r, t]
NDSolve`StateData[SequenceForm["<", 0., ">"]]
Then we reinitialize the system by
newstate = NDSolve`Reinitialize[state, {r[0] == 1}]
NDSolve`StateData[SequenceForm["<", 0., ">"]]
which works absolutely fine. Now consider another ODE system with a slightly different If condition, namely If[r[t] <= 2, 2, 0]
. We process the equations with
state2 = First@NDSolve`ProcessEquations[{r'[t] == If[r[t] <= 2, 2, 0], r[0] == 0},r, t]
NDSolve`StateData[SequenceForm["<", 0., ">"]]
which works fine. Now when we try to reinitialize this system we get an error message!
newstate2 = NDSolve`Reinitialize[state2, {r[0] == 1}]
NDSolve`Reinitialize::ntcs: Cannot solve constraint equations for initial conditions.
It seems like when I try to reinitialize Mathematica has a problem due to the condition r[t]<=2
. I have noticed the same problem with for example PieceWise functions, as noted in this post:
NDSolve: ProcessEquations and Reinitialize with Piecewise functions