Skip to main content

Section 9.3 The secant method

The secant method is a close relative of Newton's method. The only difference is that instead of drawing a tangent line and finding where it crosses the line \(y=0\) we do the same for a secant line. The slope of a secant line is computed as \((f(x)-f(p))/(x-p)\) where \(x, p\) are two distinct points on the x-axis. The benefit is that we do not need the derivative of \(f\text{,}\) we only use the function itself. The drawback is that we have to keep track not of just one point, but of two. This makes the algorithm slightly more complicated: it is not just fixed-point iteration of some function \(g\) as in Newton's method. Also, the analysis of its speed of convergence is more complicated: generally, it is between linear and quadratic; faster than bisection but slower than Newton.

Write a script that starts with the definition of a function \(f\) and two initial points, for example

f = @(x) x^3 - x;
x = 5;
p = 7;

and proceeds to find a root of \(f\) using the secant method: the next point to be computed will be

\begin{equation*} x - f(x) \frac{x-p}{f(x)-f(p)} \end{equation*}

where the fraction represents division by the slope of secant line.

Solution
f = @(x) x^3 - x;
x = 5;
p = 7;
max_tries = 1000

for k = 1:max_tries
    x1 = x - f(x)*(x-p)/(f(x)-f(p));
    if abs(x1-x) < 100*eps(x)
        break
    end
    p = x;
    x = x1;
end

if k < max_tries
    fprintf('Found x = %.12g after %d steps\n', x, k);
else
    disp('Failed to converge')
end

This script follows the structure of Example 8.2.1 except that we compute "new x", called x1, on the basis of two previous values (called x and p). After the computation, x and p are replaced by x1 and x: the newly computed point becomes one of the two points through which we draw next secant line.