Topic : Newton Raphson Method - Roots of Equations
Simulation : Graphical Simulation of the Method
Language : Mathmatica 4.1
Authors : Nathan Collier, Autar Kaw
Date : 11 July 2002
Abstract : This simulation illustrates 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

(Subscript[x, begin]) = -0.02;
(Subscript[x, end]) = 0.12;

curve = 
        Plot[f[x], {x, (Subscript[x, begin]), (Subscript[x, end])}, 
          PlotLabel -> "Entered function on given interval", 
          TextStyle -> {FontSize -> 11}];

Initial guess

x0 = 0.05;

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]

maxi = f[(Subscript[x, begin])];
mini  =  f[(Subscript[x, begin])];
step  =  ((Subscript[x, end]) - (Subscript[x, begin]))/10;
Do[ If[f[i] >  maxi, maxi = f[i]]; 
        If[f[i] <  mini, mini = f[i]],  {i, (Subscript[x, begin]), (Subscript[x, end]), 
          step}];
tot = maxi - mini;
mini = mini - 0.1*tot;
maxi  =  maxi  +  0.1*tot;

Iteration 1

x1 = x0 - f[x0]/g[x0]

(Subscript[ϵ, a]) = Abs[(x1 - x0)/x1*100]

tanline[x_] := 
      f[x0] + ((0 - f[x0])/(x1 - x0))*(x - x0)

tline = Plot[tanline[x], {x, (Subscript[x, begin]), (Subscript[x, end])}];

Show[Graphics[Line[{{x0, maxi}, {x0, mini}}]], curve, 
        Graphics[Line[{{x1, maxi}, {x1, mini}}]], tline, Axes -> True, 
        PlotLabel -> "Entered function on given interval with upper \
and lower guesses and estimated root", 
        TextStyle -> {FontSize -> 11}];

Iteration 2

x2 = x1 - f[x1]/g[x1]

(Subscript[ϵ, a]) = Abs[(x2 - x1)/x2*100]

tanline[x_] := 
      f[x1] + ((0 - f[x1])/(x2 - x1))*(x - x1)

tline = Plot[tanline[x], {x, (Subscript[x, begin]), (Subscript[x, end])}];

Show[Graphics[Line[{{x1, maxi}, {x1, mini}}]], curve, 
        Graphics[Line[{{x2, maxi}, {x2, mini}}]], tline, Axes -> True, 
        PlotLabel -> "Entered function on given interval with upper \
and lower guesses and estimated root", 
        TextStyle -> {FontSize -> 11}];

Iteration 3

x3 = x2 - f[x2]/g[x2]

(Subscript[ϵ, a]) = Abs[(x3 - x2)/x3*100]

tanline[x_] := 
      f[x2] + ((0 - f[x2])/(x3 - x2))*(x - x2)

tline = Plot[tanline[x], {x, (Subscript[x, begin]), (Subscript[x, end])}];

Show[Graphics[Line[{{x3, maxi}, {x3, mini}}]], curve, 
        Graphics[Line[{{x2, maxi}, {x2, mini}}]], tline, Axes -> True, 
        PlotLabel -> "Entered function on given interval with upper \
and lower guesses and estimated root", 
        TextStyle -> {FontSize -> 11}];