Bug introduced in 9.0 and fixed in 11.1
NDSolve
in Mathematica 9.0.0 (MacOS) is behaving strangely with a piecewise right hand side. The following code (a simplified version of my real problem):
sol = NDSolve[{x'[t] ==
Piecewise[{{2, 0 <= Mod[t, 1] < 0.5},
{-1, 0.5 <= Mod[t, 1] < 1}}
], x[0] == 0}, x, {t, 0, 1}];
Print[x[1] /. sol[[1]]];
gives the correct answer of 0.5 about 50% of the time, but often returns -0.5 and -1 instead. Rerunning it gives apparently random results. It always gives the correct result in Mathematica 8.
Here's what I've figured out so far:
- It apparently has something to do with the
Mod[t,1]
, because it works fine with just "t" in thePiecewise
. Unfortunately I'm looking at a piecewise periodic system (not just from t=0 to 1). - It's only the first segment of the solution from t=0 to t=0.5 that varies from run to run.
- Using initial condition
x[10^-100]==0
fixes the problem, but this is an ugly hack.
Can anyone replicate this strange behavior, know what's behind it, or have a better suggested fix?
"DiscontinuityProcessing"
method option? It probably might help to track down the bug somewhat faster... $\endgroup$