# Problem with NDSolve: Maximum number of steps reached

I am using NDSolve to solving a first order autonomous system of ODEs $$\dot x(t) = f(x(t))$$, $$x\in\mathbb R^n$$ numerically for a set of initial conditions $$x(0)=a,b,c,…$$. I am attempting to solve on the interval {t,0,15}, but Mathematica will give me

NDSolve::mxst: Maximum number of 15923184 steps reached at the point τ == 10.197792214025839.

and the obtained solutions would stop at this value of t.

Interesting is also, that this number of maximum steps, and the number of t where it stops depends on the initial value $$x(0)$$ which I use.

So I am wondering:

(1) How does Mathematica choose this maximum number of steps, and does it have good reasons for it? I suspect that it has to do with oscillations becoming rapid in the solutions with increasing time, but I am not sure. I do not get the warning that the system may have become stiff.

(2) Can I increase this maximum number of solutions?

(3) Is there a specific method, I could dictate NDSolve to use, which can deal with rapid oscillations for large times better than the default method NDSolve is using?

Thanks for help and insights!

EDIT: Here the corresponding code. It is quite unreadable here on the forum due to all the greek letter. But if you coppy and paste it into a Mathamtica document, it should be nicely readable.

I am using NDSolve to solving a first order autonomous system of ODEs $$\dot x(t) = f(x(t))$$, $$x\in\mathbb R^n$$ numerically for a set of initial conditions $$x(0)=a,b,c,…$$. I am attempting to solve on the interval {t,0,15}, but Mathematica will give me

NDSolve: Maximum number of 15923184 steps reached at the point t == 10.198105126672289.

and the obtained solutions would stop at this value of t.

Interesting is also, that this number of maximum steps, and the number of t where it stops depends on the initial value $$x(0)$$ which I use.

So I am wondering:

(1) How does Mathematica choose this maximum number of steps, and does it have good reasons for it? I suspect that it has to do with oscillations becoming rapid in the solutions with increasing time, but I am not sure. I do not get the warning that the system may have become stiff.

(2) Can I increase this maximum number of solutions?

(3) Is there a specific method, I could dictate NDSolve to use, which can deal with rapid oscillations for large times better than the default method NDSolve is using?

Thanks for help and insights!

EDIT: Here the corresponding code. It is quite unreadable here on the forum due to all the greek letter. But if you coppy and paste it into a Mathamtica document, it should be nicely readable.

(*τ range*)
{τi, τf} = {-40, 40};
τ0 = 0;

(*initial values*)
H0 = .1;
Σ0 = .1;
Ω0 = .9;
φ0 = 0;
t0 = 0;
M0 = 1 - Σ0^2 - Ω0;
sol1 = NDSolve[{
H'[τ] ==
H[τ] (-(1 + 2 Σ[τ]^2 + Ω[τ] (3 Cos[t[τ] - φ[τ]]^2 - 1))),
Σ'[τ] == -(2 - (2 Σ[τ]^2 + Ω[τ] (3 Cos[t[τ] - φ[τ]]^2 -
1))) Σ[τ] + 1 - Σ[τ]^2 - Ω[τ],
Ω'[τ] ==
2 Ω[τ] (1 + (2 Σ[τ]^2 + Ω[τ] (3 Cos[t[τ] - φ[τ]]^2 -
1)) - 3 Cos[t[τ] - φ[τ]]^2),
φ'[τ] == -3 Sin[t[τ] - φ[τ]] Cos[t[τ] - φ[τ]],
M'[τ] ==
2 (2 Σ[τ]^2 + Ω[τ] (3 Cos[t[τ] - φ[τ]]^2 -1) - Σ[τ]) M[τ],
t'[τ] == 1/H[τ],
H[τ0] == H0, Σ[τ0] == Σ0, Ω[τ0] == Ω0, φ[τ0] == φ0,
M[τ0] == M0, t[τ0] == t0},
{H, Σ, Ω, φ, M, t}, {τ, τi, τf}]

• Difficult to guess what is going on. I can't even see that example code, let alone replicate it for diagnosing ?. Jun 3 '19 at 18:49
• That is a strange number, it usually gets stuck at 10000 steps. Try MaxSteps->Infinity. Jun 3 '19 at 18:49
• You can see the step size decrease with Differences[Flatten[H["Grid"] /. sol1]][[;; ;; 1000]] // RealExponent // ListPlot, which plots the log-base-10 of a reasonable sample of the step sizes. Jun 3 '19 at 22:57
• For some problems, MaxSteps -> Automatic means a maximum of 10000. For others, it seems to be automatically determined, but always at least 10000 . I don't know enough to know why. I can say that the max number of steps in the OP's problem is roughly proportional to the length of the time integration tf - 0 from the initial condition at 0. It must make an estimate at the beginning of the max steps it would take; if the step size decreases too much over the course of the integration, then it might underestimate what is enough. AFAIK, there's no discussion in the docs. Jun 4 '19 at 1:20

Many moons back the MaxSteps was fixed to 10000. With this set up NDSolve would often time integrate for a while and then issue a message that it ran into MaxSteps and abort the integration. You'd then add a MaxSteps->Infinity and redo the computation and had to wait again. One day I was so upset about that that I changed it to roughly the following where h is the initial step size:

• If tend == Infinity use 10000.
• If MaxSteps -> Automatic use Floor[Times[FACTOR, Abs[(tend - tstart)/h]]] if this results in an Integer. Sometimes it does not, for example if an initial h can not be estimated.
• Else use 10000.

FACTOR is hard coded as 10. I have left out some details. This works for a very large class of problems. It does not work when the initial step size is larger than the step size taken during the time integration. This can certainly happen when the differential equation it oscillatory.

Concerning you second question, you can use MaxSteps->Infinity or any number to increase the number of steps. You can also use Monitor to keep track of the progress NDSolve makes. Something like:

Monitor[NDSolve[{D[u[t, x], t] == D[u[t, x], x, x], u[0, x] == 0,
u[t, 0] == Sin[t], u[t, 5] == 0}, u, {t, 0, 100}, {x, 0, 5},
MaxSteps -> 10001,
MaxStepSize -> 0.01,
EvaluationMonitor :> (monitor = Row[{"t = ", CForm[t]}])], monitor]


Concerning 3: I am not an expert for time integration but nothing seems wrong, so I would not change it.

• I suppose this answers my questions, and more, thank you very much for all the info, this monitoring is really great! So do I understand correctly: Since I do not enter MaxSteps manually, Mathematica seems to apply some algorithm to pick it for me. Now if I pick MaxSteps→Infinity it will solve until my chosen final time no matter what, unless it runs into an issue like a singularity, etc., correct? I will write a second question in the comment below. Jun 5 '19 at 15:27
• Second question: Say I use MaxSteps→Infinity and want to solve until t=15, but at arround t=12 I get tired of it, and decide that I would be happy with stopping here, is there a way for me to break at this point, and obtain the solution obtained so far, without throwing everything away? Jun 5 '19 at 15:31
• @Britzel, 1) Yes. 2) No, not unless you take special steps previously. If want to do this, have a look at the WhenEvent ref page. IIRC that has an example of an Abort button. Jun 5 '19 at 17:46
• I see, thanks for the answers! Jun 5 '19 at 17:49