Skip to main content

Bisection method pseudo-code

Short answer

Bisection repeatedly halves an interval where f(a)f(b) < 0, using midpoint c = (a+b)/2. If f(c)=0 or interval half-width below tolerance, c is the root; otherwise replace a or b with c keeping a sign change, then repeat.

Write a pseudo-code (algorithm) to find a real root of a non-linear equation using the Bisection method.

Bisection method repeatedly halves an interval [a, b] in which f(a)·f(b) < 0 and keeps the half that still contains the root.

Pseudo-code

Algorithm: Bisection method
Input : function f(x), interval ends a and b,
        tolerance e, maximum iterations N
Output: approximate root

1.  Read a, b, e, N
2.  If f(a) * f(b) >= 0 then
        print "Root is not bracketed; choose other a, b"
        stop
3.  Set i = 1
4.  Repeat steps 5 to 9 while i <= N
5.      c = (a + b) / 2
6.      If f(c) = 0 or (b - a)/2 < e then
            print "Root =", c, "after", i, "iterations"
            stop
7.      If f(a) * f(c) < 0 then
            b = c
        else
            a = c
8.      i = i + 1
9.  End repeat
10. Print "Method did not converge in N iterations"
11. Stop

Notes

  • The number of iterations needed for tolerance e is n ≥ log((b − a)/e) / log 2.
  • The method always converges, but slowly (error is halved each step).

✓ Answer checked by Claude Opus 5.5 (high effort)

All Numerical Method chapter 2 questions →