TExcelTAD
Home › Numerical Methods › The Bisection Method

The Bisection Method

If a continuous function changes sign on an interval, it has a root there (the intermediate value theorem). Bisection halves the interval again and again, keeping the half where the sign change is. It is slow but cannot fail, which makes it the safety net inside practical root finders.

The algorithm

Given f(a)f(a) and f(b)f(b) of opposite signs:

  1. Let m=a+b−a2m = a + \frac{b - a}{2}.
  2. If f(m)f(m) has the same sign as f(a)f(a), set a=ma = m; otherwise set b=mb = m.
  3. Repeat until b−ab - a is below the tolerance.

After kk steps the bracket has width b−a2k\frac{b - a}{2^k}. To reach a tolerance τ\tau you need

k=⌈log⁡2b−aτ⌉k = \left\lceil\log_2\frac{b - a}{\tau}\right\rceil

steps, whatever ff is. The error shrinks by a factor of 2 per step: linear convergence with rate 12\frac{1}{2}, about 0.30.3 decimal digits per step.

python · try it yourself
import numpy as np

def bisect(f, a, b, tol=1e-12):
    fa = f(a)
    if fa * f(b) > 0:
        raise ValueError("f(a) and f(b) must have opposite signs")
    steps = 0
    while b - a > tol:
        m = a + (b - a) / 2          # safer than (a + b) / 2 in extreme ranges
        fm = f(m)
        if np.sign(fm) == np.sign(fa):
            a, fa = m, fm
        else:
            b = m
        steps += 1
    return a + (b - a) / 2, steps

f = lambda x: x**3 - 2 * x - 5          # Wallis's classic test equation
r, steps = bisect(f, 2, 3)
print(f"root {r:.12f} after {steps} steps; predicted {int(np.ceil(np.log2(1 / 1e-12)))}")
root 2.094551481543 after 40 steps; predicted 40

Watching the error

python · try it yourself
import numpy as np

f = lambda x: np.exp(-x) - x
true = 0.5671432904097838               # the omega constant
a, b = 0.0, 1.0
for k in range(1, 31):
    m = (a + b) / 2
    if f(a) * f(m) > 0:
        a = m
    else:
        b = m
    if k % 5 == 0:
        print(f"step {k:2d}: width {b - a:.2e}, error {abs((a + b) / 2 - true):.2e}")
step  5: width 3.12e-02, error 1.10e-02
step 10: width 9.77e-04, error 2.49e-04
step 15: width 3.05e-05, error 1.06e-05
step 20: width 9.54e-07, error 3.27e-07
step 25: width 2.98e-08, error 1.41e-08
step 30: width 9.31e-10, error 3.60e-10

The width halves exactly; the actual error jumps around but is always below the width.

Strengths and weaknesses

  • Guaranteed convergence once a bracket is known, and a guaranteed error bound.
  • Needs only the sign of ff, so it works even when ff is noisy far from the root.
  • Slow: 50+ steps for full double precision, compared with about 5 for Newton.
  • Needs a bracket; cannot find double roots like x2=0x^2 = 0 (no sign change).
  • With a discontinuity it may converge to a jump (for 1x\frac{1}{x} it "finds" x=0x = 0).

In software

  • scipy.optimize.bisect exists but brentq (Brent's method) is almost always better: it keeps a bracket like bisection but uses secant and inverse quadratic steps when they work (Rootfinding in Practice).
  • git bisect uses the same idea to find the commit that introduced a bug in log⁡2n\log_2 n tests.

Exercises

Exercise 1: Steps needed

How many bisection steps reduce a bracket of width 8 below 10−610^{-6}?

Answer
Solution

log⁡2(8×106)≈22.93\log_2(8 \times 10^6) \approx 22.93, so 23 steps.

Exercise 2: First steps by hand

Do three bisection steps for f(x)=x2−3f(x) = x^2 - 3 on [1,2][1, 2].

Answer

[1.5,2][1.5, 2], [1.5,1.75][1.5, 1.75], [1.625,1.75][1.625, 1.75].

Solution

f(1.5)=−0.75<0f(1.5) = -0.75 < 0 so keep [1.5,2][1.5, 2]; f(1.75)=0.0625>0f(1.75) = 0.0625 > 0 so [1.5,1.75][1.5, 1.75]; f(1.625)=−0.359<0f(1.625) = -0.359 < 0 so [1.625,1.75][1.625, 1.75]. (3≈1.732\sqrt{3} \approx 1.732.)

Exercise 3: Pole versus root (code)

Run bisection on f(x)=tan⁡xf(x) = \tan x on [1,2][1, 2], which contains no root. What does it return, and why?

Answer

π2≈1.5708\frac{\pi}{2} \approx 1.5708, the pole, because tan⁡\tan changes sign there.

Solution
python · try it yourself
import numpy as np
a, b = 1.0, 2.0
for _ in range(50):
    m = (a + b) / 2
    if np.tan(a) * np.tan(m) > 0:
        a = m
    else:
        b = m
print(round(m, 10), round(np.pi / 2, 10), f"|tan(m)| = {abs(np.tan(m)):.1e}")
1.5707963268 1.5707963268 |tan(m)| = 1.2e+15

Always check the residual: a huge ∣f∣\lvert f\rvert at the "root" reveals a discontinuity.

Test yourself

Bisection converges…

  1. quadratically
  2. linearly with rate 1/21/2
  3. superlinearly
  4. only for polynomials

Bisection needs…

  1. the derivative
  2. two points where ff has opposite signs
  3. a good initial guess near the root
  4. a polynomial

Why can bisection not find the root of f(x)=(x−1)2f(x) = (x - 1)^2?

  1. ff is not continuous
  2. ff does not change sign at the root
  3. the root is irrational
  4. it converges too slowly