Function Repository Resource:

FindRootPlot

Source Notebook

Visualize the function evaluations done by FindRoot

Contributed by: Wolfram Staff (with additional contributions by Jan Mangaldan)

ResourceFunction["FindRootPlot"][f,{x,xst}]

plots the steps and the points at which the function f and any of its derivatives are evaluated in FindRoot[f,{x,xst}], superimposed on a plot of f versus x.

ResourceFunction["FindRootPlot"][{f1,f2},{{x,xst},{y,yst}}]

plots the steps and the points at which the pair of functions and their derivatives are evaluated, superimposed on a contour plot of the merit function.

ResourceFunction["FindRootPlot"][f,range,property]

returns the specified property.

Details

To simplify processing and reduce possible confusion about the function f, ResourceFunction["FindRootPlot"] does not accept equations; it finds a root of f=0.
In the case of two functions and variables, the merit function is the sum of squares of the pair.
The argument property can be any of the following:
"Result"the result of FindRoot
"Statistics"an association showing the number of steps and evaluations of the function and its derivatives
"Plot"the plot of the function and the evaluation points
Allassociation containing "Result", "Statistics" and "Plot"
ResourceFunction["FindRootPlot"] takes the same options as FindRoot and Plot (in the one-dimensional case) or ContourPlot (in the two-dimensional case), with the following addition:
"EvaluationStyles"Automaticstyles used for the evaluation points
The "EvaluationStyles" option takes an association whose values correspond to the styles used for the evaluation points in the plot.
With the setting PlotLegendsAutomatic, ResourceFunction["FindRootPlot"] shows a legend for the evaluation points.

Examples

Basic Examples (2) 

Show the steps and function evaluations used in finding the root of cos(x)=0:

In[1]:=
ResourceFunction["FindRootPlot"][Cos[x], {x, 3, 6}, PlotRange -> All]
Out[1]=

Show the steps and function evaluations used in finding the root of the Rosenbrock problem 1-x=0,10(y-x2)=0:

In[2]:=
ResourceFunction[
 "FindRootPlot"][{10 (y - x^2), 1 - x}, {{x, -1.2}, {y, 1.}}]
Out[2]=

Scope (2) 

Use PlotLegends to display a legend for the evaluation points:

In[3]:=
ResourceFunction[
 "FindRootPlot"][{Exp[x - 2] - y, y^2 - x}, {{x, 1.}, {y, 1.}}, PlotLegends -> Automatic]
Out[3]=

Use a different rootfinding method from the default:

In[4]:=
ResourceFunction[
 "FindRootPlot"][{Exp[x - 2] - y, y^2 - x}, {{x, 1.}, {y, 1.}}, Method -> "AffineCovariantNewton", PlotLegends -> Automatic]
Out[4]=

Return the statistics of the evaluations done by FindRoot:

In[5]:=
ResourceFunction[
 "FindRootPlot"][{Exp[x - 2] - y, y^2 - x}, {{x, 1.}, {y, 1.}}, "Statistics"]
Out[5]=

Return all properties:

In[6]:=
ResourceFunction[
 "FindRootPlot"][{Exp[x - 2] - y, y^2 - x}, {{x, 1.}, {y, 1.}}, All]
Out[6]=

Options (6) 

ColorFunction (1) 

Use a different ColorFunction setting:

In[7]:=
ResourceFunction[
 "FindRootPlot"][{Exp[x - 2] - y, y^2 - x}, {{x, 0., 0.4}, {y, 0., 0.4}}, ColorFunction -> "M10DefaultDensityGradient"]
Out[7]=

EvaluationStyles (2) 

Use "EvaluationStyles" to change the style used for the evaluation points:

In[8]:=
ResourceFunction[
 "FindRootPlot"][{Exp[x - 2] - y, y^2 - x}, {{x, 1.}, {y, 1.}}, "EvaluationStyles" -> <|{"Steps" -> Directive[Blue, Thin, PointSize[0.015]], "Residual" -> Directive[Yellow, PointSize[0.025]], "Jacobian" -> Directive[Purple, PointSize[0.04]], "Solution" -> PointSize[0.055]}|>, PlotLegends -> Automatic]
Out[8]=

Default styles are used if a particular style is left unspecified:

In[9]:=
ResourceFunction[
 "FindRootPlot"][{Exp[x - 2] - y, y^2 - x}, {{x, 1.}, {y, 1.}}, "EvaluationStyles" -> <|{"Steps" -> Directive[Blue, Thin, PointSize[0.015]], "Jacobian" -> Directive[Purple, PointSize[0.04]]}|>, PlotLegends -> Automatic]
Out[9]=

Method (1) 

Compare different rootfinding methods:

In[10]:=
{ResourceFunction[
  "FindRootPlot"][{Exp[x - 2] - y, y^2 - x}, {{x, 1}, {y, 1}}, Method -> "Newton", PlotLegends -> Automatic], ResourceFunction[
  "FindRootPlot"][{Exp[x - 2] - y, y^2 - x}, {{x, 1}, {y, 1}}, Method -> "AffineCovariantNewton", PlotLegends -> Automatic]}
Out[10]=

PlotLegends (1) 

Show a legend for the evaluation points:

In[11]:=
ResourceFunction[
 "FindRootPlot"][{Exp[x - 2] - y, y^2 - x}, {{x, 0., 0.4}, {y, 0., 0.4}}, PlotLegends -> Automatic]
Out[11]=

PlotStyle (1) 

Change the style used for the functions:

In[12]:=
ResourceFunction["FindRootPlot"][Cos[x], {x, 3, 6}, PlotStyle -> Directive[Thick, Orange]]
Out[12]=
In[13]:=
ResourceFunction[
 "FindRootPlot"][{Exp[x - 2] - y, y^2 - x}, {{x, 0., 0.4}, {y, 0., 0.4}}, PlotStyle -> {Directive[Thick, Red], Directive[Dashed, Green]}]
Out[13]=

Version History

  • 1.0.0 – 16 February 2021

Related Resources

License Information