I frequently use StepMonitor
to keep an eye on NDSolve
while it's working. Usually it does the job just fine. Recently however, I've been working with sets of coupled ODEs that take several hundred iterations to solve. The business of oversight then gets rather tedious as you get hundreds of lines of intermediate values.
Is there a way to keep the output manageable by telling StepMonitor
to monitor only every $n$-th step?
Simple Example
Even an ODE as simple as
n = 1;
NDSolve[{y'[x] == y[x], y[0] == 1}, y, {x, 0, 5},
StepMonitor :> Print[{n, x, y[x]}] n++] // Timing
results in 53 lines of intermediate results. What if I want, say, just 5 lines? Also, performing so many intermediate evaluations clearly introduces significant overhead. The above takes 0.002964 seconds to evaluate. By comparison, the same command without any monitoring,
NDSolve[{y'[x] == y[x], y[0] == 1}, y, {x, 0, 5}] // Timing
takes 0.00095 seconds, i.e. less than a third of the time.
NDSolve
encounters a stiff system or a singularity, I'd know about sooner rather than later. Waiting forNDSolve
to eventually fail due to vanishing step size wastes quite a bit of time, especially for more involved computations. $\endgroup$StepMonitor
in itself adds about10^-5
sec. per step (plus whatever time it takes to compute the monitor expression). If the RHS of an ODE $d{\bf X}/dx = RHS({\bf X}, t)$ evaluates quickly like the OP's example, this is longer than the time to compute the step (which can also depend on theMethod
). This makes it appear in the OP's example as ifStepMonitor
slowsNDSolve
down by a factor of 3-4, whereas I'm suggesting it be viewed as adding a certain amount. (This is aside from the slight slowness and excessive output ofPrint
.) $\endgroup$