使用している数値メソッドが段階を踏むたびに計算される式を与える反復的な数値計算関数のオプションである.
StepMonitor
使用している数値メソッドが段階を踏むたびに計算される式を与える反復的な数値計算関数のオプションである.
詳細
- オプション設定は一般にStepMonitor:>expr で与えられる.
- expr が直ちに評価されるのを避けるために,->ではなく:>が使われる.
- expr が評価されるたびに,数値計算中のすべての変数に現行の値が割り当てられる.
- 実際にはBlock[{var1=val1,…},expr]が使用される.
例題
すべて開く すべて閉じる例 (3)
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}]f[x_] = Exp[x] + 1 / x;
{res, steps} = Reap[FindMinimum[f[x], {x, 3}, StepMonitor :> Sow[{x, f[x]}]]]Plot[f[x], {x, .1, 1.5}, Epilog -> {Red, Map[Point, steps[[1]]]}]スコープ (4)
正弦Gordonの偏微分方程式を解く際の解の進行をモニターする:
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]]{res, {steps}} = Reap[FindRoot[{x + y ^ 2 == 0, y + Cos[x] == 0}, {{x, 0}, {y, 0}}, StepMonitor :> Sow[{x, y}]]]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]}}]]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}]]]lp = ListPlot[data, PlotRange -> All];
Map[Show[lp, Plot[model[#], {x, 0, 1}]]&, evals[[1]]]NDSolveで偏微分方程式を数値的に解く際に使われた時間(t)ステップについての空間(x)解をプロットする:
{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[Plot3D[Evaluate[First[u[t, x] /. sol]], {t, 0, 1}, {x, 0, 1}, Mesh -> False], splot]アプリケーション (4)
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]];ニュートン(Newton)法の二次収束は各ステップにおいて結果的に精度が2倍になることを許す:
ListPlot[{newts, secs}, PlotRange -> All]{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];solContourPlot[(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]}}]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]]];Column[{Plot[Evaluate[{x[t], x'[t]} /. First[sol]], {t, 0, 10}], ListPlot[Transpose[{Drop[steps, -1], Differences[steps]}]]}]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"}}]テクニカルノート
履歴
2003 で導入 (5.0)
テキスト
Wolfram Research (2003), StepMonitor, Wolfram言語関数, 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: 18-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: 18-June-2026]}