# NDSolve DAE solution order is mixed up

Bug introduced in 10.4.0.0 or earlier and fixed in 11.0.1.

Compare the two solution sets below: in the first one (x == False), there are only 3 dependent variables while in the second one (x == True), a dependent algebraic variable is introduced as r[t] == 5 (converting the ODE to a DAE). A DAE often requires a different NDSolve method, here I used "EquationSimplification" -> {Automatic, "SimplifySystem" -> True} which messes up the solution order as is apparent from the plots.

ClearAll[t, R, A, P, r, x];
eq = {
R'[t] == -.1 R[t],
A'[t] == .1 A[t],
P'[t] == 0,
If[x, r[t] == Sin[t], Nothing],
R[0] == 1, A[0] == 1, P[0] == 1
};

sol1 = Block[{x = False},
NDSolve[eq, {R, A, P}, {t, 0, 20}, DependentVariables -> {R, A, P}]];
sol2 = Block[{x = True},
NDSolve[eq, {R, A, P, r}, {t, 0, 20}, DependentVariables -> {R, A, P, r},
Method -> {"EquationSimplification" -> {Automatic,
"SimplifySystem" -> True}}]];

Grid[{
LogPlot[#[t] /. sol1, {t, 0, 20}, PlotLabel -> #] & /@ {R, A, P},
LogPlot[#[t] /. sol2, {t, 0, 20}, PlotLabel -> #] & /@ {R, A, P, r}
}]


Can anyone reproduce this bug?

(Mathematica 10.4.0.0, Windows 7, 64bits)

TechSupport filed an internal report acknowledging the wrong order of output.

• Removing DependentVariables -> ...  in sol2 will also solve the issue. If r[t] is to be regarded as an algebraic function it may be more robust to provide initial conditions, e.g. { r[0] == 5, r[t] == 5 }.
– gwr
Commented May 11, 2016 at 20:04
• @gwr As a matter of fact, r can be any t-dependent function like Sin[t], I only left it as a constant for the sake of simplicity. I've edited it into the post. Commented May 11, 2016 at 21:18
• Ok, I see, but then the question has been a bit misleading. Also note that the plots do not match your question. :)
– gwr
Commented May 11, 2016 at 21:28
• @gwr Thanks for pointing out the discrepancy! My question was specifically about the reproducibility of this bug, though I have not yet added the tag [bug] as TS did not confirm it yet. Commented May 11, 2016 at 21:50
• Have you gotten any feedback with regard to the [bug] tag from TS yet?
– gwr
Commented May 19, 2016 at 19:19

It seems that this might be a bug or maybe a weakness of the NDSolvemachinery. One can observe, that with the option "SimplifySystem" -> False the issue does not arise. In fact a lot of settings will cause no problem at all.

As far as I can see, the culprit is the option DependentVariables as it seems to influence the sequence of equation simplifications somehow. This can be seen here:

ClearAll[t, R, A, P, r, x];
With[
{  eq = {
R'[t] == -.1 R[t], A'[t] == .1 A[t], P'[t] == 0,
r[t] == Sin[t],

r[0] == Sin[0], R[0] == 1, A[0] == 1, P[0] == 1
},
sequences = Permutations@{R, A, P, r}
},
Module[
{solutions, trials},
solutions = Map[
First @ NDSolve[
eq,
{R, A, P, r},
{t, 0, 20},
DependentVariables -> #, (* use the actual permutation *)
Method -> {"EquationSimplification" -> {Automatic, "SimplifySystem" -> True}}] &
,
sequences
];
trials = Transpose @ { sequences, solutions };
Apply[
Function[ {seq, sol} ,
{
Map[ToString] /* StringJoin@ seq,
Sequence @@ (
LogPlot[ #[t] /. sol, {t, 0, 20}, PlotLabel -> #, ImageSize -> Tiny,
PlotTheme -> "Minimal"] & /@ {R, A, P, r}
)
}
],
trials,
1
] // Multicolumn[#, 3] &
]
]


We see that of the 24 possible sequences for the dependent variables only 4 produce the correct solution:

{A,P,R,r} , {A, P, r, R}, {A, r, P, R}, {r, A, P, R}

## Update

This issue seems fixed now:

\$Version


11.1.0 for Microsoft Windows (64-bit) (March 13, 2017)

The above code now produces the same results for all permutations of the sequence of variables as it should:

• I see, it all hinges on "SimplifySystem" -> True as with False everthing turns out ok.
– gwr
Commented May 11, 2016 at 21:42
• Yes, the internal simplification somehow rotates the list of variables by one to the left, regardless of the number of added algebraic functions (I've tried with more than one such variable). Commented May 11, 2016 at 21:47
• @IstvánZachar I do note, that it works out when the option DependentVariables is dropped? -- My gut feeling here is bug.
– gwr
Commented May 11, 2016 at 21:48
• To be honest, it works with any of the option-specifications being dropped : ) Commented May 11, 2016 at 21:56
• @IstvánZachar It seems that the option DependentVariablesindeed is the culprit, as the sequence given there influences the equation simplification as can be seen from my updated answer.
– gwr
Commented May 12, 2016 at 9:48