Skip to content
Tactiverge

School/○ Innovator/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.

About 40 min · 3 goals · Checked by automated tests

1.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.

2.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?

Loading the interactive…

Can't use the interactive? Read a description instead

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.

3.Checkpoint: guided practice

Check what you just discovered before going on. Hints appear after a wrong answer.

Practice 1 · a fresh version every time

A cable tension must satisfy f(x) = x**2 - 11 = 0. Bisection starts on [1, 4] (f(1) < 0 < f(4)), tests the midpoint each step and keeps the half where the sign changes. What midpoint is tested in step 2? Give 4 decimal places.

4.Predict

Bisection starts with the bracket [0, 2]. How wide is the bracket after 10 steps?

Guess first. Being wrong here is how the surprise works.

5.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

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.

Derivation: How many bisection steps for a given accuracy?
  1. 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).

  2. 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.

  3. 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.

  4. To guarantee w_k ≤ tol we need 2^k ≥ (b − a)/tol, so k = ⌈log₂((b − a)/tol)⌉.

  5. 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 21

Common 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.

6.Practice on your own

Practice, not a test. Wrong answers get a hint and another try.

Practice 2 · a fresh version every time

Bisection starts from the bracket [0, 2] and stops once the bracket width is at most 0.05. What is the smallest number of steps that guarantees this?

steps

Practice 3 · a fresh version every time

Bisection starts from the bracket [1, 3] and stops once the bracket width is at most 0.001. What is the smallest number of steps that guarantees this?

steps

Practice 4 · a fresh version every time

A cable tension must satisfy f(x) = x**2 - 7 = 0. Bisection starts on [1, 4] (f(1) < 0 < f(4)), tests the midpoint each step and keeps the half where the sign changes. What midpoint is tested in step 4? Give 4 decimal places.

Practice 5

f(x) = (x − 1)² on [0, 3]. Why can't bisection start?

7.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.

8.Reflect

Concepts: Root finding and iterative methods