3
$\begingroup$

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

$\endgroup$
2
$\begingroup$

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[0] == a},
   r, t, a]

pf[0]
pf[1]

As an alternative, you could try to experiment with:

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

Hope that helps.

$\endgroup$
  • $\begingroup$ 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? $\endgroup$ – jaclea Sep 10 '13 at 17:54
  • $\begingroup$ @jaclea, what exactly is the problem with ParametricNDSolve in this case? Perhaps I can help with that. $\endgroup$ – user21 Sep 10 '13 at 19:49
  • $\begingroup$ 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 NDSolve`Reinitialize. $\endgroup$ – jaclea Sep 11 '13 at 7:08
  • $\begingroup$ Here is a post regarding this problem: mathematica.stackexchange.com/questions/32084/… $\endgroup$ – jaclea Sep 11 '13 at 7:20
  • $\begingroup$ Filed the above. You speak of a lot of issues do you have more? $\endgroup$ – user21 Sep 11 '13 at 7:43

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.