Topic : Secant Method - Roots of Equations
Simulation : Graphical Simulation of the Method
Language : Mathmatica 4.1
Authors : Nathan Collier, Autar Kaw
Date : 2 July 2002
Abstract : This simulation shows how the secant method for finding roots of an equation f[x] = 0 works.

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, b]) = -0.02;
(Subscript[x, e]) = 0.12;

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

Lower initial guess

xguess1 = 0.02;

Upper initial guess

xguess2 = 0.05;

SOLUTION

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

Check first if the lower and upper guesses bracket the root of the equation

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

Iteration 1

Choose two initial guesses of root.

x1' = xguess1;
x0 = xguess2;

Estimate of root

x1 = x0 - (f[x0]*(x1' - x0))/(f[x1'] - f[x0])

Absolute relative approximate error

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

m = (f[x0] - f[x1'])/(x0 - x1');
secantline[x_] = m*x + f[x0] - m*x0;

sline = Plot[secantline[x], {x, (Subscript[x, b]), (Subscript[x, e])}];

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

It should be noted that these two guesses do not have to bracket the root.  We have called the two initial guesses in the way which we have because that will be the format for subsequent iterations.  It does not matter which guess is which.  Try switching the two initial guesses to see what happens. One should converge faster than the other.

Iteration 2

Estimate of root

x2 = x1 - (f[x1]*(x0 - x1))/(f[x0] - f[x1])

Absolute relative approximate error

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

m = (f[x1] - f[x0])/(x1 - x0);
secantline[x_] = m*x + f[x1] - m*x1;

sline = Plot[secantline[x], {x, (Subscript[x, b]), (Subscript[x, e])}];

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

Iteration 3

Estimate of root

x3 = x2 - (f[x2]*(x1 - x2))/(f[x1] - f[x2])

Absolute relative approximate error

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

m = (f[x2] - f[x1])/(x2 - x1);
secantline[x_] = m*x + f[x2] - m*x2;

sline = Plot[secantline[x], {x, (Subscript[x, b]), (Subscript[x, e])}];

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