Skip to main content

Newton-Raphson algorithm

Short answer

Newton-Raphson iteration: x(n+1) = x(n) - f(x(n))/f'(x(n)). Algorithm: input f, f', x0, tolerance e, max N. Loop: if |f'(x0)| near 0 stop; compute x1 = x0 - f(x0)/f'(x0); if |x1-x0| < e or |f(x1)| < e, root = x1; else x0 = x1; repeat until convergence or N iterations.

Write an algorithm to find a real root of a non-linear equation using the Newton-Raphson method.

Newton-Raphson iteration: x(n+1) = x(n) − f(x(n)) / f′(x(n)).

Algorithm / pseudo-code

Algorithm: Newton-Raphson method
Input : f(x), f'(x), initial guess x0,
        tolerance e, maximum iterations N
Output: approximate root

1.  Read x0, e, N
2.  Set i = 1
3.  Repeat steps 4 to 9 while i <= N
4.      If |f'(x0)| is very small (near 0) then
            print "Derivative is zero; choose another x0"
            stop
5.      x1 = x0 - f(x0) / f'(x0)
6.      If |x1 - x0| < e (or |f(x1)| < e) then
            print "Root =", x1, "after", i, "iterations"
            stop
7.      x0 = x1
8.      i = i + 1
9.  End repeat
10. Print "Method does not converge in N iterations"
11. Stop

Notes

  • Both f(x) and f′(x) must be coded as functions.
  • The check in step 4 avoids division by zero.
  • The iteration limit N stops an endless loop when the method diverges or oscillates.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Numerical Method chapter 2 questions →