# NDSolve: Reinitialize fails with If condition

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@NDSolveProcessEquations[{r'[t] == If[t <= 2, 2, 0], r == 0},r, t]
NDSolveStateData[SequenceForm["<", 0., ">"]]


Then we reinitialize the system by

newstate = NDSolveReinitialize[state, {r == 1}]
NDSolveStateData[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@NDSolveProcessEquations[{r'[t] == If[r[t] <= 2, 2, 0], r == 0},r, t]
NDSolveStateData[SequenceForm["<", 0., ">"]]


which works fine. Now when we try to reinitialize this system we get an error message!

newstate2 = NDSolveReinitialize[state2, {r == 1}]
NDSolveReinitialize::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

I filed this as a bug. Here is a workaround. It's much easier to use ParametricNDSolve for this:

pf = ParametricNDSolveValue[{r'[t] == If[r[t] <= 2, 2, 0], r == a},
r, t, a]

pf
pf


As an alternative, you could try to experiment with:

g[in_?NumericQ] := If[in <= 2, 2, 0]
state2 = First@
NDSolveProcessEquations[{r'[t] == g[r[t]], r == 0}, r, t];
newstate2 = NDSolveReinitialize[state2, {r == 1}]


Hope that helps.

• Unfortunately I don't want to use ParametricNDSolve in this case, since I have found a lot of weird issues regarding ParametricNDSolve (filed to Wolfram Support). Is there any other workaround? Sep 10 '13 at 17:54
• @jaclea, what exactly is the problem with ParametricNDSolve in this case? Perhaps I can help with that.
– user21
Sep 10 '13 at 19:49
• I found a lot of issues regarding differentiation ParametricNDSolve objects. For example, you cannot differentiate your system if it includes a matrix differential equation. I am really in need of using NDSolveReinitialize. Sep 11 '13 at 7:08
• Here is a post regarding this problem: mathematica.stackexchange.com/questions/32084/… Sep 11 '13 at 7:20
• Filed the above. You speak of a lot of issues` do you have more?
– user21
Sep 11 '13 at 7:43