多くの数値計算およびプロット関数のオプションで,入力から派生した関数が数値的に評価される際に評価する式を与える.
EvaluationMonitor
多くの数値計算およびプロット関数のオプションで,入力から派生した関数が数値的に評価される際に評価する式を与える.
例題
すべて開く すべて閉じる例 (4)
根を求めるのに使われたすべての関数評価を含む情報を出力する:
FindRoot[x ^ 2 - 2, {x, 1}, EvaluationMonitor :> Print["x = ", x, " x^2 - 2 =", x ^ 2 - 2]]Block[{c = 0}, {FindRoot[x ^ 2 - 2, {x, 1}, EvaluationMonitor :> c++], c}]{res, data} = Reap[FindRoot[x ^ 2 - 2, {x, 1}, EvaluationMonitor :> Sow[x - Sqrt[2]]]]ListLogLogPlot[data[[1]]]Monitor[NDSolveValue[{D[u[t, x, y], {t, 2}] == Laplacian[u[t, x, y], {x, y}], u[0, x, y] == 0, Derivative[1, 0, 0][u][0, x, y] == 0, DirichletCondition[u[t, x, y] == If[t < 1, t, 1], x == 0]}, u, {x, y}∈RegionUnion[Rectangle[{0, 0}, {5, 1}], Disk[{5, 1 / 2}, 1 / 2]], {t, 0, 10}
, EvaluationMonitor :> (monitor = Row[{"t = ", CForm[t]}])], monitor]スコープ (5)
正弦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}, EvaluationMonitor :> (sol = u[t, x]; time = t)], Plot[sol, {x, -10, 10}, PlotRange -> {0, 8}, PlotLabel -> time]]{res, data} = Reap[FindMinimum[(x - 1) ^ 2 + 100(y - Sin[x]) ^ 2, {{x, -10}, {y, 1}}, EvaluationMonitor :> Sow[{x, y}]]]ListPlot[data[[1]]]NIntegrateを使って数値積分を計算するための評価:
{res, data} = Reap[NIntegrate[Sqrt[x(1 - x)], {x, 0, 1}, EvaluationMonitor :> Sow[x]]];resListPlot[data[[1]]]NDSolveと外挿法を使って微分方程式を解く際の評価:
{sol, evals} = Reap[NDSolve[{x''[t] + Sin[x[t]] == 0, x[0] == 1, x'[0] == 0}, x, {t, 0, 10}, EvaluationMonitor :> Sow[{t, x[t], x'[t]}], Method -> "Extrapolation"]];プロットは,この方法ではすべての評価が解の曲線上にある訳ではないことを示している:
Show[Plot[Evaluate[First[{x[t], x'[t]} /. sol]], {t, 0, 10}], ListPlot[{evals[[1, All, {1, 2}]], evals[[1, All, {1, 3}]]}]]f[x_, y_] := E^x - Sin[x^3 - 3 y];Block[{c = 0}, Plot3D[f[x, y], {x, -2, 2}, {y, -2, 2}, EvaluationMonitor :> c++, PlotLabel :> ToString[c] <> " evaluations"]]記号導関数が使われているので,関数評価に使われる評価回数は少なくなる:
Block[{c = 0}, Plot3D[Evaluate[f[x, y]], {x, -2, 2}, {y, -2, 2}, EvaluationMonitor :> c++, PlotLabel :> ToString[c] <> " evaluations"]]一般化と拡張 (2)
Catch[FindMinimum[Exp[x] + 1 / x ^ 2, {x, 1}, EvaluationMonitor :> If[x ≤ 0, Throw["Negative x"]]]]Catch[FindMinimum[Exp[x] + 1 / x ^ 2, {x, 10}, EvaluationMonitor :> If[x ≤ 0, Throw["Negative x"]]]]{sol, evaldata} = Reap[FindMinimum[(x - 1) ^ 2 + 100(y - x ^ 2) ^ 2, {{x, -1}, {y, 1}}, EvaluationMonitor :> Sow[{x, y}, 0], Gradient -> {"Symbolic", EvaluationMonitor :> Sow[{x, y}, 1]},
Method -> {"Newton", "Hessian" -> {"Symbolic", EvaluationMonitor :> Sow[{x, y}, 2]}}], _, Rule];solcolors = {Blue, Yellow, Red};
sizes = {PointSize[0.015], PointSize[0.03], PointSize[0.05]};ContourPlot[(x - 1) ^ 2 + 100(y - x ^ 2) ^ 2, {x, -1, 1}, {y, -1, 1}, Epilog -> Table[{colors[[i + 1]], sizes[[i + 1]], Map[Point, i /. evaldata]}, {i, 2, 0, -1}]]アプリケーション (6)
順番を示すツールチップを用いてプロットのどの部分で評価が行われたかを示す:
f[x_] = Exp[-x ^ 2];
{plot, evals} = Reap[Block[{e = 0}, Plot[f[x], {x, -5, 5}, PlotRange -> All, EvaluationMonitor :> Sow[Tooltip[Point[{x, f[x]}], ++e]]]]];
Show[plot, Graphics[{Red, evals}]]evals[sv_ ? NumberQ] := Block[{c = 0}, FindRoot[Cos[x], {x, sv}, EvaluationMonitor :> c++];c]Plot[evals[sv], {sv, 0, π}]TableForm[Table[ Block[{e = 0}, {method, Norm[{x - 1, y - 1} /. FindMinimum[(x - 1) ^ 2 + 100(y - x ^ 2) ^ 2, {{x, -1}, {y, 1}}, Method -> method, EvaluationMonitor :> e++][[2]]], e}], {method, {Automatic, "LevenbergMarquardt", "Newton", "QuasiNewton", "Gradient"}}], TableHeadings -> {{}, {"Method", "Error", "Evaluations"}} ]NDSolveの各ODE積分法に必要な評価数と時間を比較する:
evals[method_] := Block[{e = 0}, Timing[NDSolve[{x''[t] + x[t] == 0, x[0] == 1, x'[0] == 0}, x, {t, 0, 100π}, MaxSteps -> Infinity, Method -> method, EvaluationMonitor :> e++];e]]methods = {Automatic, "Adams", "BDF", "ExplicitRungeKutta", "ImplicitRungeKutta", "Extrapolation"};TableForm[Table[Join[{method}, evals[method]], {method, methods}], TableHeadings -> {{}, {"Method", "Timing", "Evaluations"}}]NDSolveのPrecisionGoalとAccuracyGoalの関数としてのステップと評価:
se[method_][g_] := Block[{s = 0, e = 0}, NDSolve[{x''[t] + x'[t] / 10 + x[t] / (1 + x[t] ^ 2) == Sin[t], x[0] == 1, x'[0] == 0}, x, {t, 0, 100}, PrecisionGoal -> g, AccuracyGoal -> g, WorkingPrecision -> If[g > 8, 2g, MachinePrecision], StepMonitor :> s++, EvaluationMonitor :> e++, MaxSteps -> Infinity, Method -> method];{s, e}]固定次数の明示的ルンゲ・クッタ(Runge–Kutta)法を使う:
erk = Table[Flatten[{g, se[{"ExplicitRungeKutta", "DifferenceOrder" -> 8}][g]}], {g, 0, 20, 2}]ext = Table[Flatten[{g, se["Extrapolation"][g]}], {g, 0, 20, 2}]2つのメソッドを比較する.12を超えるゴールでは,明らかに適応的順が優れている:
ListLogPlot[{erk[[All, {1, 2}]], erk[[All, {1, 3}]], ext[[All, {1, 2}]], ext[[All, {1, 3}]]}, PlotStyle -> {Purple, Red, Orange, Blue}]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[{w_, p_}] = Exp[-0.1 x] Sin[w x + p];{fit, {evals}} = Reap[FindFit[data, model[{w, p}], {w, p}, x, EvaluationMonitor :> Sow[{w, p}]]]residual = Map[Function[{x}, Evaluate[model[{w, p}]]], data[[All, 1]]] - data[[All, 2]];
ContourPlot[Evaluate[residual.residual], {w, 0, 10}, {p, 0, 2 π}, Epilog -> {Red, Point[evals]}]{bfit, {bevals}} = Reap[FindFit[data, model[{w, p}], {{w, 4}, {p, 4}}, x, EvaluationMonitor :> Sow[{w, p}]]]ContourPlot[Evaluate[residual.residual], {w, 0, 10}, {p, 0, 2 π}, Epilog -> {{Red, Point[evals]}, {Green, Point[bevals]}}]Show[Plot[Evaluate[model[{w, p}] /. {fit, bfit}], {x, 0, 1}, PlotStyle -> {{Red}, {Green}}], ListPlot[data]]特性と関係 (1)
EvaluationMonitorによる評価はBlockのように調べられる:
f[x_] := x ^ 2 - 2;FindRoot[f[x], {x, 1, 2}, EvaluationMonitor :> Print["x = ", x, " f[x] = ", f[x]]]これは実質的にBlockを変数の数値の割当てとともに使うのに等しい:
ff[xx_ ? NumberQ] := Block[{x = xx}, Print["x = ", x, " f[x] = ", f[x]];f[x]]FindRoot[ff[x], {x, 1, 2}]テクニカルノート
関連するワークフロー
- 変数の中間値を表示する ▪
- 変数の値を動的に追跡する
履歴
2003 で導入 (5.0) | 2007 で更新 (6.0)
テキスト
Wolfram Research (2003), EvaluationMonitor, Wolfram言語関数, https://reference.wolfram.com/language/ref/EvaluationMonitor.html (2007年に更新).
CMS
Wolfram Language. 2003. "EvaluationMonitor." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2007. https://reference.wolfram.com/language/ref/EvaluationMonitor.html.
APA
Wolfram Language. (2003). EvaluationMonitor. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/EvaluationMonitor.html
BibTeX
@misc{reference.wolfram_2026_evaluationmonitor, author="Wolfram Research", title="{EvaluationMonitor}", year="2007", howpublished="\url{https://reference.wolfram.com/language/ref/EvaluationMonitor.html}", note=[Accessed: 13-September-2026]}
BibLaTeX
@online{reference.wolfram_2026_evaluationmonitor, organization={Wolfram Research}, title={EvaluationMonitor}, year={2007}, url={https://reference.wolfram.com/language/ref/EvaluationMonitor.html}, note=[Accessed: 13-September-2026]}