is an option for iterative numerical computation functions that gives an expression to evaluate whenever a step is taken by the numerical method used.
StepMonitor
is an option for iterative numerical computation functions that gives an expression to evaluate whenever a step is taken by the numerical method used.
Details
- The option setting is normally given as StepMonitor:>expr.
- The :> is used instead of -> to avoid expr being immediately evaluated.
- Whenever expr is evaluated, all variables in the numerical computation are assigned their current values.
- Block[{var1=val1,…},expr] is effectively used.
Examples
open all close allBasic Examples (3)
Monitor steps taken for a numerical minimization with FindMinimum:
FindMinimum[Exp[x] + 1 / x, {x, 1}, StepMonitor :> Print["Step to x = ", x]]Block[{c = 0}, {FindMinimum[Exp[x] + 1 / x, {x, 1}, StepMonitor :> c++], c}]Use Reap and Sow to collect step data:
f[x_] = Exp[x] + 1 / x;
{res, steps} = Reap[FindMinimum[f[x], {x, 3}, StepMonitor :> Sow[{x, f[x]}]]]Show steps on a plot of the function:
Plot[f[x], {x, .1, 1.5}, Epilog -> {Red, Map[Point, steps[[1]]]}]Scope (4)
Monitor the solution progress when solving the sine-Gordon PDE:
Monitor[NDSolve[{Subscript[∂, t, t]u[t, x] == Subscript[∂, x, x]u[t, x] + Sin[u[t, x]] , u[0, x] == E^-x^2, u^(1, 0)[0, x] == 0, u[t, -10] == u[t, 10]}, u, {t, 0, 10}, {x, -10, 10}, StepMonitor :> (sol = u[t, x]; time = t)], Plot[sol, {x, -10, 10}, PlotRange -> {0, 8}, PlotLabel -> time]]Monitor steps taken to numerically solve a system of equations:
{res, {steps}} = Reap[FindRoot[{x + y ^ 2 == 0, y + Cos[x] == 0}, {{x, 0}, {y, 0}}, StepMonitor :> Sow[{x, y}]]]Show steps on a surface plot of the functions:
pts1 = Apply[Function[{x, y}, {x, y, x + y ^ 2}], steps, {1}];
pts2 = Apply[Function[{x, y}, {x, y, y + Cos[x]}], steps, {1}];Show[Plot3D[{x + y ^ 2, Cos[x] + y}, {x, -1, 0}, {y, -1, 0}, PlotStyle -> {{Opacity[.5], Blue}, {Opacity[.5], Red}}, Mesh -> False], Graphics3D[{Thickness[Medium], PointSize[Large], {Blue, Line[pts1], Point[pts1]}, {Red, Line[pts2], Point[pts2]}}]]Steps in parameter space for a nonlinear fit:
data = {{0.18, -0.13}, {0.84, -0.06}, {0.05, 0.88}, {0.24, -0.63}, {0.67, 0.93}, {0.05, 0.88}, {0.65, 0.92}, {0.01, 0.99}, {0.17, -0.04}, {0.23, -0.55}};model[{a_, k_, w_, p_}] = a Exp[-k x] Sin[w x + p];{fit, evals} = Reap[FindFit[data, model[{a, k, w, p}], {a, k, w, p}, x, StepMonitor :> Sow[{a, k, w, p}]]]Sequence of plots showing the evolution of the model over the steps:
lp = ListPlot[data, PlotRange -> All];
Map[Show[lp, Plot[model[#], {x, 0, 1}]]&, evals[[1]]]Plot the spatial (x) solution for time (t) steps used numerically solving a PDE with NDSolve:
{sol, steps} = Reap[NDSolve[{D[u[t, x], t] == D[u[t, x], x, x], u[0, x] == 0, u[t, 0] == Sin[6 π t], (D[u[t, x], x] /. x -> 1) == 0}, u, {t, 0, 1}, {x, 0, 1}, StepMonitor :> Sow[ParametricPlot3D[{t, x, u[t, x]}, {x, 0, 1}]]]];splot = Show[steps, PlotRange -> All, BoxRatios -> {1, 1, 0.4}]Show the steps with a surface plot of the solution:
Show[Plot3D[Evaluate[First[u[t, x] /. sol]], {t, 0, 1}, {x, 0, 1}, Mesh -> False], splot]Applications (4)
Show how precision is adapted for high-precision root finding:
newts = Reap[FindRoot[x ^ 2 - 2, {x, 1.}, WorkingPrecision -> 1000, StepMonitor :> Sow[Precision[x]], Method -> "Newton"]][[2, 1]];secs = Reap[FindRoot[x ^ 2 - 2, {x, 1., 2.}, WorkingPrecision -> 1000, StepMonitor :> Sow[Precision[x]], Method -> "Brent"]][[2, 1]];The quadratic convergence of Newton's method allows eventual precision doubling at each step:
ListPlot[{newts, secs}, PlotRange -> All]Investigate steps and evaluations for a numerical minimization:
{sol, data} = Reap[FindMinimum[(x - 1) ^ 2 + 100(y - x ^ 2) ^ 2, {{x, -1}, {y, 1}}, EvaluationMonitor :> Sow[{x, y}, "Evaluations"], StepMonitor :> Sow[{x, y}, "Steps"]], _, Rule];solShow evaluations in red, steps in yellow, and the final point in green:
ContourPlot[(x - 1) ^ 2 + 100(y - x ^ 2) ^ 2, {x, -1, 1}, {y, -1, 1}, Epilog -> {{Green, PointSize[0.05], Point[{x, y} /. sol[[2]]]}, {Yellow, PointSize[0.03], Point["Steps" /. data]}, {Red, PointSize[0.015], Point["Evaluations" /. data]}}]Get step sizes for the numerical solution of an ODE with NDSolve:
{sol, {steps}} = Reap[NDSolve[{x''[t] + (t + 1)x[t] == 0, x[0] == 1, x'[0] == 0}, x, {t, 0, 10}, StepMonitor :> Sow[t]]];Show the solution and step size as a function of t:
Column[{Plot[Evaluate[{x[t], x'[t]} /. First[sol]], {t, 0, 10}], ListPlot[Transpose[{Drop[steps, -1], Differences[steps]}]]}]Compare steps, evaluations, and timing for different ODE integration methods in NDSolve:
evals[method_] := Block[{e = 0, s = 0}, Timing[NDSolve[{x''[t] + Sin[x[t]] == 0, x[0] == 1, x'[0] == 0}, x, {t, 0, 100π}, MaxSteps -> Infinity, Method -> method, StepMonitor :> s++, EvaluationMonitor :> e++];{s, e}]]methods = {Automatic, "Adams", "BDF", "ExplicitRungeKutta", "ImplicitRungeKutta", "Extrapolation"};TableForm[Table[Flatten[{method, evals[method]}], {method, methods}], TableHeadings -> {{}, {"Method", "Timing", "Steps", "Evaluations"}}]Tech Notes
Related Guides
History
Introduced in 2003 (5.0)
Text
Wolfram Research (2003), StepMonitor, Wolfram Language function, https://reference.wolfram.com/language/ref/StepMonitor.html.
CMS
Wolfram Language. 2003. "StepMonitor." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/StepMonitor.html.
APA
Wolfram Language. (2003). StepMonitor. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/StepMonitor.html
BibTeX
@misc{reference.wolfram_2026_stepmonitor, author="Wolfram Research", title="{StepMonitor}", year="2003", howpublished="\url{https://reference.wolfram.com/language/ref/StepMonitor.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_stepmonitor, organization={Wolfram Research}, title={StepMonitor}, year={2003}, url={https://reference.wolfram.com/language/ref/StepMonitor.html}, note=[Accessed: 13-June-2026]}