61
$\begingroup$

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?

$\endgroup$
7
  • 2
    $\begingroup$ Of course, that is what Mathematics does. But how do I check? I can use Options to check which ones were given. But there is no such thing like Method[%] 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." $\endgroup$
    – uli
    Jan 18, 2012 at 11:34
  • 1
    $\begingroup$ This mentions some of the defaults taken. $\endgroup$ Jan 18, 2012 at 11:34
  • 1
    $\begingroup$ "If I publish results I cannot write..." - ergo, you should try to specify options explicitly if you want everything to be transparent. $\endgroup$ Jan 18, 2012 at 11:36
  • 1
    $\begingroup$ @uli: +1 It's an interesting question and I'm not sure that you'll find a satisfactory answer. I guess if you want to publish the results you can either specify the Mma version number you used, or manually choose the method that you use. $\endgroup$
    – Simon
    Jan 18, 2012 at 11:43
  • 1
    $\begingroup$ "...is it decidable whether a system is stiff or not?" - it's not entirely foolproof, but Mathematica does have stiffness detection methods. See this and this for instance. $\endgroup$ Jan 18, 2012 at 12:00

3 Answers 3

51
$\begingroup$

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[#] =!= {}) &]
$\endgroup$
3
  • $\begingroup$ I just saw ruebenko's answer. Using TraceInternal as above seems to give the same list of methods as his data, but it requires a lot of digging... $\endgroup$
    – Simon
    Jan 18, 2012 at 12:04
  • 1
    $\begingroup$ You can also use the second argument of Trace; for example, Trace[NDSolve[..], _NDSolve`InitializeMethod | _[NDSolve`MethodData[___]], TraceInternal -> True] $\endgroup$
    – Michael E2
    Jan 24, 2017 at 0:04
  • $\begingroup$ @Simon, to check which method was chosen by NSolve instead of NDSolve, I changed ! FreeQ[#, Method | NDSolveMethodData] &` to ! FreeQ[#, Method | NSolveMethodData] &`, but it seems not to work, could you give some suggestions? Thank you! $\endgroup$
    – Nobody
    Nov 30 at 10:07
19
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ Nice! But if I am not mistaken, this seems to be a NDSolve[] only solution? $\endgroup$
    – uli
    Jan 18, 2012 at 12:07
  • $\begingroup$ @Uli , sorry only got this question now, yes, this is NDSolve specific. $\endgroup$
    – user21
    Mar 19, 2012 at 9:34
12
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.