Topic : Newton-Raphson Method - Roots of Equations
Simulation : Convergence of the Method
Language : Mathmatica 4.1
Authors : Nathan Collier, Autar Kaw
Date : 2 July 2002
Abstract : This simulation illustrates the convergence of the Newton-Raphson method of finding the root of an equation f[x] = 0.

INPUTS: Enter the Following

Function in f[x] = 0

f[x_] := x^3 - 0.165*x^2 + 3.993*10^-4

Range of 'x' you want to see the function

xb = -0.02;
xe = 0.12;

curve = 
        Plot[f[x], {x, xb, xe}, 
          PlotLabel -> "Entered function on given interval", 
          TextStyle -> {FontSize -> 11}];

Initial guess

x0 = 0.05;

Maximum number of iterations

(Subscript[n, maximum]) = 4;

Counting from the left, enter the number of the root desired

numroot = 2;

Because this method uses a line tangent to the function at the initial guess, we must calculate the derivative of the function to find the slope of the line at this point.  Here we will define the derivative of the function f(x) as g(x).

g[x_] := f'[x]

$MinPrecision  =  20;

True Solution

This is the solution found by Mathematica

xactual = Root[f[x], numroot]

Value of root as a function of iterations

Here the Newton-Raphson method algorithm is applied to generate the values of the roots, thrue error, absolute relative true error, approximate error, absolute relative approximate error, and the number of significant digits at least correct in the estimated root as a function of number of iterations.

Clear[xr];

SetPrecision[xr, 40]

Array[xr, (Subscript[n, maximum])];

For[i = 1; xr[0] = x0, i <= (Subscript[n, maximum]), i++, 
      xr[i] = xr[i - 1] - f[xr[i - 1]]/g[xr[i - 1]]]

Absolute true error

Array[Et, (Subscript[n, maximum])];

For[i = 1, i <= (Subscript[n, maximum]), i++, 
      Et[i] = Abs[xactual - xr[i]]]

Absolute relative true error

Array[ϵt, (Subscript[n, maximum])];

For[i = 1, 
      i <= (Subscript[n, maximum]), i++, ϵt[i] = 
        Abs[Et[i]/xactual*100]]

Absolute approximate error

Array[Ea, (Subscript[n, maximum])];

For[i = 1, i <= (Subscript[n, maximum]), i++, 
      If[i <= 1,  Ea[i] =  Abs[xr[i] - x0], 
        Ea[i] = Abs[xr[i] - xr[i - 1]]]]

Absolute relative approximate error

Array[ϵa, (Subscript[n, maximum])];

For[i = 1, 
      i <= (Subscript[n, maximum]), i++, ϵa[i] = 
        Abs[Ea[i]/xr[i]*100]]

Significant digits at least correct

Array[sigdig, (Subscript[n, maximum])];

For[i = 1, i <= (Subscript[n, maximum]), i++, 
      If[(ϵa[i] ≥ 5)  ||  (i <= 1), 
        sigdig[i] = 0, 
        sigdig[i] = 
          Floor[(2 - Log[10, Abs[ϵa[i]/0.5]])]]]

Graphs

xrplot = Table[xr[i], {i, 1, (Subscript[n, maximum])}];

ListPlot[xrplot,PlotJoined -> True, PlotRange -> All, AxesOrigin -> {1, Min[xrplot]}, PlotLabel -> "Estimated root as a function of number \
of iterations"];

Etplot = Table[Et[i], {i, 1, (Subscript[n, maximum])}];

ListPlot[Etplot,PlotJoined -> True, PlotRange -> All, AxesOrigin -> {1, Min[Etplot]}, PlotLabel -> "Absolute true error as a function of \
number of iterations"];

ϵtplot = 
        Table[ϵt[i], {i, 1, (Subscript[n, maximum])}];

ListPlot[ϵtplot,PlotJoined -> True, PlotRange -> All, AxesOrigin -> {1, Min[ϵtplot]}, PlotLabel -> "Absolute relative true error as a \
function of number of iterations"];

Eaplot = Table[Ea[i], {i, 1, (Subscript[n, maximum])}];

ListPlot[Eaplot,PlotJoined -> True, PlotRange -> All,AxesOrigin -> {1, Min[Eaplot]}, PlotLabel -> "Absolute approximate error as a function \
of number of iterations"];

ϵaplot = 
        Table[ϵa[i], {i, 1, (Subscript[n, maximum])}];

ListPlot[ϵaplot,PlotJoined -> True, PlotRange -> All, AxesOrigin -> {1, Min[ϵaplot]}, PlotLabel -> "Absolute relative approximate error as a \
function of number of iterations"];

sigdigplot = Table[sigdig[i], {i, 1, (Subscript[n, maximum])}];

<< Graphics`Graphics`

BarChart[sigdigplot];