({{, (RefLink[StepMonitor, paclet:ref/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. }})

OpenersNotesSection(Spacer1)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.

(RefLink[Block, paclet:ref/Block])[{(Subscript[var, 1])=(Subscript[val, 1]),…},expr] is effectively used.

OpenersPrimaryExamplesSection(Spacer1)Examples  (11)

OpenersExampleSection0(Spacer1)Basic Examples  (3)

Monitor steps taken for a numerical minimization with (RefLink[FindMinimum, paclet:ref/FindMinimum]):

FindMinimum[Exp[x] + 1/x,{x,1},StepMonitor:>Print["Step to x = ",x]]

Count steps:

Block[{c=0},{FindMinimum[Exp[x] + 1/x,{x,1}, StepMonitor:>c++],c}]

Use (RefLink[Reap, paclet:ref/Reap]) and (RefLink[Sow, paclet:ref/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]]]}]

OpenersExampleSection1(Spacer1)Scope  (4)

Monitor the solution progress when solving the sine-Gordon PDE:

Monitor[NDSolve[{(∂_(t,t))u[t,x]==(∂_(x,x))u[t,x]+Sin[u[t,x]] ,u[0,x]==(E^(-(x^2))),Derivative[1,0][u][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 (RefLink[NDSolve, paclet:ref/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]

OpenersExampleSection2(Spacer1)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];sol

Show 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 (RefLink[NDSolve, paclet:ref/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 (RefLink[NDSolve, paclet:ref/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"}}]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[EvaluationMonitor, paclet:ref/EvaluationMonitor])  ▪  (RefLink[Sow, paclet:ref/Sow])  ▪  (RefLink[Monitor, paclet:ref/Monitor])  ▪  (RefLink[Print, paclet:ref/Print])  ▪  (RefLink[Method, paclet:ref/Method])  ▪  (RefLink[MaxSteps, paclet:ref/MaxSteps])}})}})

({{DocumentationFooterIconsRelatedTutorial.png, ({{Tutorials}, {▪(RefLinkPlain[Monitoring and Selecting Algorithms, paclet:tutorial/MonitoringAndSelectingAlgorithms])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Tuning & Debugging, paclet:guide/TuningAndDebugging])}, {▪(RefLinkPlain[Differential Equations, paclet:guide/DifferentialEquations])}, {▪(RefLinkPlain[Numerical Evaluation & Precision, paclet:guide/NumericalEvaluationAndPrecision])}})}})