Lite / Intelligent Robotic Systems / Week 7
Bisection: trap the answer, halve the trap
No formula gives the cable tension where a sag equation balances. But if you know one guess is too low and one too high, you can corner the answer to any accuracy you like.
Why should I care?
Many engineering equations have no neat algebraic solution: a cam profile meeting a follower, a cable's catenary, a motor's operating point. Numerical methods solve them by iteration: improve a guess again and again until the error is small enough. Bisection is the slow, unbreakable one.
- Run the bisection method by hand and in code for an equation f(x) = 0.
- Derive the bracket width (b − a)/2^k after k steps and the steps needed for a tolerance.
- Explain why bisection is guaranteed to converge when f is continuous and changes sign.
Predict
Bisection starts with the bracket [0, 2]. How wide is the bracket after 10 steps?
- 0.2
- About 0.002
- About 0.0000002
- It depends on the equation
Guess first, then reveal
2 / 2¹⁰ = 2/1024 ≈ 0.00195. Every step halves the bracket, whatever the equation. That predictability is bisection's great strength: you know in advance how many steps you need.
Discover
Choose x² − 2 and Bisection. Step ten times and watch the lavender bracket and the error column. Then move the ends so f(a) and f(b) have the same sign. Finally try cos x − x: does the bracket still halve at the same rate?
What the interactive shows (text version)
The stepper plots f(x) = x² − 2 on 0 to 3 with a lavender bracket. Each bisection step halves the bracket: [0, 2], [1, 2], [1, 1.5], [1.25, 1.5] and so on, closing in on 1.41421. With ends that give f the same sign, the stepper reports that bisection cannot start.
Understand
Bracket, test, keep half
Pick a and b with f(a) * f(b) < 0. Compute the midpoint m. If f(m) has the same sign as f(a), the root is in [m, b]; otherwise it is in [a, m]. Repeat until the bracket is narrower than your tolerance.
Error you can predict
The error after k steps is at most (b − a)/2^(k+1) if you report the midpoint. That bound does not depend on the equation at all, so you can plan the computation before you run it. Each step gains one binary digit of accuracy (linear convergence).
When it fails
Bisection needs a sign change. A root where the curve only touches zero (like x² at 0) has no sign change and can't be bracketed. A discontinuity (like 1/x) can fake a sign change with no root. Always check f(a)·f(b) < 0 first and plot f if unsure.
Explain it another way
Intuitive
Guessing someone's age with 'older or younger?': each answer halves the range. You can't miss, and you know how many questions you'll need.
Technical
Stop on a bracket-width tolerance, not on f(m) == 0 (exact zeros are rare in floating point). Also cap the number of iterations. Python's standard library has no bisection root finder, but the loop is only a few lines.
Mathematical
Convergence is linear with rate 1/2: e_{k+1} ≈ e_k/2. To gain d decimal digits you need about d·log₂ 10 ≈ 3.32·d steps.
Applied
Bisection is often used as a safe fallback inside faster solvers: if a quick method misbehaves, the solver falls back to halving a bracket it trusts.
Derivation: How many bisection steps for a given accuracy?
Start with a bracket [a, b] where f(a) and f(b) have opposite signs, so a continuous f must cross zero somewhere inside (the intermediate value theorem).
Each step tests the midpoint m = (a + b)/2 and keeps the half where the sign still changes. The new bracket has width (b − a)/2.
After k steps the width is w_k = (b − a)/2^k. The root is always inside, so the midpoint is within w_k/2 of it.
To guarantee w_k ≤ tol we need 2^k ≥ (b − a)/tol, so k = ⌈log₂((b − a)/tol)⌉.
Example: [0, 2] to a width of 10⁻⁶ needs ⌈log₂(2 × 10⁶)⌉ = ⌈20.93⌉ = 21 steps. Each extra decimal place costs about 3.3 more steps.
Worked example 1
Find √2 as the root of x² − 2 on [0, 2] to a width of 10⁻⁶.
import math
def bisect_root(f, a, b, tol=1e-6, max_steps=100):
"""Root of f in [a, b] by bisection. Requires f(a) and f(b) of opposite sign."""
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 and steps < max_steps:
m = (a + b) / 2
fm = f(m)
if fm == 0:
return m, steps + 1
if (fm > 0) == (fa > 0):
a, fa = m, fm
else:
b = m
steps += 1
return (a + b) / 2, steps
root, k = bisect_root(lambda x: x * x - 2, 0, 2)
print(round(root, 6), k, math.ceil(math.log2(2 / 1e-6))) # 1.414214 21 21Common mix-ups
Stop when f(midpoint) == 0.
✓ In floating point that may never happen. Stop when the bracket is narrower than a tolerance.A sign change always means a root.
✓ Only for a continuous function. 1/x changes sign across 0 but has no root.
Practice
Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.
Apply it
A cam follower's height is h(θ) = 0.02·cos θ + 0.005·θ (metres, θ in radians). Use bisection to find the angle in [0, 2] where h = 0.01 m, to a width of 10⁻⁵. Predict the number of steps first, then check your code matches.
Reflect
Bisection is slow but certain. Name a situation in engineering where certainty is worth the extra steps.
Write your answer on paper or in your own notes. Lite never stores what you write.