There are commands like NonlinearModelFit[] or NDSolve[] that have the option Method it typically defaults to Automatic. How can you check after the evaluation of the command which method Mathematica picked?
- 124,525
- 11
- 401
- 574
- 1,365
- 13
- 13
3 Answers
I think you can actually see (most of) what Mathematica is doing by using Trace[..., TraceInternal -> True].
For example,
Select[Flatten[
Trace[NDSolve[y'[x] == x && y[0] == 0, y, {x, 0, 6}],
TraceInternal -> True]], ! FreeQ[#, Method | NDSolve`MethodData] &]
shows the DE was evaluated using NDSolve`LSODA and Newton's method. (I think)
And
Select[Flatten[
Trace[NDSolve[{Derivative[1][x][t]^2 + x[t]^2 == 1, x[0] == 1/2},
x, {t, 0, 10 Pi}, SolveDelayed -> True],
TraceInternal -> True]], ! FreeQ[#, Method | NDSolve`MethodData] &]
used NDSolve`IDA.
As an aside, here's something I just learnt from Trott's Mathematica guidebook for numerics, to see all of the methods and suboptions for NDSolve
{#, First /@ #2} & @@@
Select[{#, Options[#]} & /@ (ToExpression /@
DeleteCases[Names["NDSolve`*"],(* PDE method only *) "NDSolve`MethodOfLines"]),
(Last[#] =!= {}) &]
- 10,167
- 5
- 57
- 72
-
I just saw ruebenko's answer. Using
TraceInternalas above seems to give the same list of methods as hisdata, but it requires a lot of digging... – Simon Jan 18 '12 at 12:04 -
1You can also use the second argument of
Trace; for example,Trace[NDSolve[..], _NDSolve`InitializeMethod | _[NDSolve`MethodData[___]], TraceInternal -> True]– Michael E2 Jan 24 '17 at 00:04 -
@Simon, to check which method was chosen by
NSolveinstead ofNDSolve, I changed! FreeQ[#, Method | NDSolveMethodData] &to! FreeQ[#, Method | NSolve`MethodData] &`, but it seems not to work, could you give some suggestions? Thank you! – Nobody Nov 30 '23 at 10:07
For NDSolve with one step methods you can use the MethodMonitor.
data = Last[
Reap[sol =
NDSolve[{y'[x] == y[x] Cos[x + y[x]], y[0] == 1}, y, {x, 0, 30},
Method -> "StiffnessSwitching",
"MethodMonitor" :> (Sow[NDSolve`Self[[0]]];)];]];
See:
tutorial/NDSolveStiffnessTest
tutorial/NDSolveExtrapolation
Adams, BDF, IDA are multi-step methods and do not work with this approach.
- 10,167
- 5
- 57
- 72
-
Nice! But if I am not mistaken, this seems to be a
NDSolve[]only solution? – uli Jan 18 '12 at 12:07 -
I asked this question once after a presentation by Jon McLoone. His answer was that that was not possible and that Mathematica can switch methods many times if the situation asks for it. So it wouldn't be useful either. I agree that this is not completely satisfactory.
- 65,815
- 14
- 188
- 323
NDSolve[], by default it switches between"BDF"and"Adams", depending on whether the system being solved is stiff or not. If you're performing nonlinear least squares withFindFit[], Mathematica is smart enough to automatically use"LevenbergMarquardt". – J. M.'s missing motivation Jan 18 '12 at 11:30Optionsto check which ones were given. But there is no such thing likeMethod[%]that informs me what Mathematics did. If I publish results I cannot write "The fitting was probably done with LevenbergMarquardt, but I can’t tell for sure, because there is no command to check." – uli Jan 18 '12 at 11:34Automatic-options there are only heuristics, that may give a wrong classification? – uli Jan 18 '12 at 11:54