Monitoring and Selecting Algorithms

Functions in the Wolfram Language are carefully set up so that you normally do not have to know how they work inside. But particularly for numerical functions that use iterative algorithms, it is sometimes useful to be able to monitor the internal progress of these algorithms.

StepMonitoran expression to evaluate whenever a successful step is taken
EvaluationMonitoran expression to evaluate whenever functions from the input are evaluated

Options for monitoring progress of numerical functions.

This prints the value of every time a step is taken.
In[1]:=
Click for copyable input
Out[1]=

Note the importance of using rather than . You need a delayed rule to make expr be evaluated each time it is used, rather than just when the rule is given.

Reap and Sow provide a convenient way to make a list of the steps taken.
In[2]:=
Click for copyable input
Out[2]=
This counts the steps.
In[3]:=
Click for copyable input
Out[3]=

To take a successful step toward an answer, iterative numerical algorithms sometimes have to do several evaluations of the functions they have been given. Sometimes this is because each step requires, say, estimating a derivative from differences between function values, and sometimes it is because several attempts are needed to achieve a successful step.

This shows the successful steps taken in reaching the answer.
In[4]:=
Click for copyable input
Out[4]=
This shows every time the function was evaluated.
In[5]:=
Click for copyable input
Out[5]=
The pattern of evaluations done by algorithms in the Wolfram Language can be quite complicated.
In[6]:=
Click for copyable input
Out[6]=
Method->Automaticpick methods automatically (default)
Method->"name"specify an explicit method to use
Method->{"name",{"par1"->val1,}}specify more details of a method

Method options.

There are often several different methods known for doing particular types of numerical computations. Typically the Wolfram Language supports most generally successful ones that have been discussed in the literature, as well as many that have not. For any specific problem, it goes to considerable effort to pick the best method automatically. But if you have sophisticated knowledge of a problem, or are studying numerical methods for their own sake, you may find it useful to tell the Wolfram Language explicitly what method it should use. Function reference pages list some of the methods built into the Wolfram Language; others are discussed in "Numerical and Related Functions" or in advanced documentation.

This solves a differential equation using method , and returns the number of steps and evaluations needed.
In[7]:=
Click for copyable input
With the method selected automatically, this is the number of steps and evaluations that are needed.
In[8]:=
Click for copyable input
Out[8]=
This shows what happens with several other possible methods. The Adams method that is selected automatically is the fastest.
In[9]:=
Click for copyable input
Out[9]=
This shows what happens with the explicit RungeKutta method when the difference order parameter is changed.
In[10]:=
Click for copyable input
Out[10]=