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 and of opposite signs:
- Let .
- If has the same sign as , set ; otherwise set .
- Repeat until is below the tolerance.
After steps the bracket has width . To reach a tolerance you need
steps, whatever is. The error shrinks by a factor of 2 per step: linear convergence with rate , about decimal digits per step.
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
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 , so it works even when 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 (no sign change).
- With a discontinuity it may converge to a jump (for it "finds" ).
In software
scipy.optimize.bisectexists butbrentq(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 bisectuses the same idea to find the commit that introduced a bug in tests.
Exercises
Exercise 1: Steps needed
How many bisection steps reduce a bracket of width 8 below ?
Answer
Solution
, so 23 steps.
Exercise 2: First steps by hand
Do three bisection steps for on .
Answer
, , .
Solution
so keep ; so ; so . (.)
Exercise 3: Pole versus root (code)
Run bisection on on , which contains no root. What does it return, and why?
Answer
, the pole, because changes sign there.
Solution
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 at the "root" reveals a discontinuity.
Test yourself
Bisection converges…
- quadratically
- linearly with rate
- superlinearly
- only for polynomials
Bisection needs…
- the derivative
- two points where has opposite signs
- a good initial guess near the root
- a polynomial
Why can bisection not find the root of ?
- is not continuous
- does not change sign at the root
- the root is irrational
- it converges too slowly