Skip to main content

Secant method algorithm

Short answer

Use initial guesses x0, x1; compute x2 = x1 - f(x1)*(x1-x0)/(f(x1)-f(x0)). If |x2-x1| < tolerance, x2 is root; otherwise set x0=x1, x1=x2 and iterate.

Write an algorithm (pseudo-code) of the Secant method for finding a real root of a non-linear equation.

The Secant method needs two starting guesses x0, x1 and no derivative.

Algorithm / pseudo-code

Algorithm: Secant method
Input : f(x), initial guesses x0, x1,
        tolerance e, maximum iterations N
Output: approximate root

1.  Read x0, x1, e, N
2.  Set i = 1
3.  Repeat steps 4 to 9 while i <= N
4.      f0 = f(x0),  f1 = f(x1)
5.      If |f1 - f0| is very small then
            print "Division by zero; change guesses"
            stop
6.      x2 = x1 - f1 * (x1 - x0) / (f1 - f0)
7.      If |x2 - x1| < e then
            print "Root =", x2, "after", i, "iterations"
            stop
8.      x0 = x1
        x1 = x2
9.      i = i + 1
10. Print "Method does not converge in N iterations"
11. Stop

For clarity the pseudo-code evaluates both f0 and f1 in every pass. In a real program only one new function value, f(x2), is needed per iteration, because the old f1 can be reused as the new f0. This makes each step cheaper than a Newton-Raphson step, which needs both f and f′.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Numerical Method chapter 2 questions →