Monitor only every $n$-th step in NDSolve
If only you need to reduce the number of printed lines, this Print
s once every 10 instances.
n = 1;
NDSolve[
{y'[x] == y[x], y[0] == 1}
, y
, {x, 0, 5}
, StepMonitor :> If[
Mod[++n, 10] === 0
, Echo[
TemplateApply[StringTemplate["n:``, x:``, y:``", {n, x, y[x]}]]
, "Monitor"]]
] // Timing
But the idea of filling the screen with updates seems sub-optimal. You could use a Dynamic
update of a single line. To reduce the overhead, this limits the update to once per second.
AbsoluteTiming[
DynamicModule[{n = 1, status},
Dynamic[Refresh[Row@status, UpdateInterval -> 1]];
sol = NDSolve[{y'[x] == y[x], y[0] == 1}, y, {x, 0, 5},
StepMonitor :> (status = {n += 1, x, y[x]})];]]
Obviously you could combine the two solutions.
EDIT
All that said, after a better answer by @george2079 it seems the correct tool to use is Monitor
.
if you just want a status monitor you can do like this:
Monitor[NDSolve[{y'[x] == x, y[0] == 1}, y, {x, 0, 5},
StepMonitor :> (p = x; ++n)], {n, p}]
this prints every step but wont clutter your screen.
There is inevitably an overhead, but typically in a real case where you care about monitoring results it will be small compared to the function eval.
this is useful sometimes too:
p = 0; ProgressIndicator[Dynamic[p], {0, 5}]
NDSolve[{y'[x] == f[x], y[0] == 1}, y, {x, 0, 5},
StepMonitor :> (p = x)]
Note if NDSolve
needs to do recursion then the -x- val wont accurately reflect progress however.