Lesson

The intermediate value theorem and bisection: where a function crosses zero

Solving an equation that has no formula, starting from the interest rate hidden in monthly instalments: the intermediate value theorem proved by bisection, the error bound, the rate of convergence and real-world examples.

Last updated:


A challenge: a function you cannot see

A continuous function (its graph can be drawn without lifting the pen) is hidden.
You know its value at the two ends of [0,2][0,2]⁠: negative at one end, positive at the other.
You can ask for its value at any point you choose, and every test counts.
Before any method has a name, decide on a strategy of your own:
where will you test first, and why? Your goal is to trap a root in as narrow an interval as you can that is certain to contain it (less than 0.010.01 wide), with as few tests as possible.

The challenge needs JavaScript. A printable version of the question follows.

The question. A continuous function ff on [0,2][0,2] has f(0)<0f(0)<0 and f(2)>0f(2)>0⁠.
You may ask for f(x)f(x) at points of your choice, and each question counts.
Work out your strategy: which point do you test first, and why?
What do you do after each answer? How many tests would you promise in advance for an interval that is certain to contain a root and is narrower than 0.010.01⁠?

TestxxWhy this point?f(x)f(x)New interval with a root
1
2
3
4
5
6
7
8

Work alone for a few minutes, then compare your choices with a partner.
Questions for the discussion that follows: why did you pick the points you picked?
Did the size of ∣f∣|f| at a point tell you how close the root was?
What guarantees that the root stays in the interval? How many tests can you promise before seeing the function?

Warm-up: what do you already know?

1. A continuous function ff has f(1)=−2f(1)=-2 and f(3)=5f(3)=5⁠. Which of these is impossible?

A: ff has no zero in [1,3][1,3]

Impossible: the curve must get from −2-2 to 55 without lifting its pencil, so it crosses zero somewhere.
We state this idea precisely, and give it its name, later on this page.

B: f(2)=0.5f(2)=0.5

Possible: nothing fixes the value at the midpoint; the two end values say nothing about it.

C: ff has three zeros in [1,3][1,3]

Possible: a continuous curve can cross zero three times (up, down, up) and still go from negative to positive.

D: f(2)=−7f(2)=-7

Possible: the curve may first go lower and then rise to 55⁠.

2. The interval [0,0.2][0,0.2] is halved three times, always keeping the half that contains a root.
How wide is what is left?

A: 0.01250.0125

That is the width after four halvings.

B: 0.0250.025

Three halvings divide the width by 23=82^3=8⁠: 0.28=0.025\frac{0.2}{8}=0.025⁠.

C: 0.050.05

That is the width after two halvings.

D: 0.10.1

That is the width after one halving.

A problem with no formula

A shop offers a laptop for 1200 in cash, or in six monthly payments of 220 each, the first one a month from now.
In instalments you pay 1320 in all. Which monthly interest rate rr is hidden in this offer?
The function RATE of Excel and of Google Sheets computes such a rate:
RATE(6, -220, 1200) (the number of payments, the payment as money going out, the present value as money coming in) returns about 2.793% per month, and you can try it yourself (in Excel the separator may be a semicolon, depending on the language settings).

Think of the 1200 as a loan from the shop, which we repay with a payment of 220 at the end of each month.
Each month the debt grows by the monthly rate rr⁠, that is, it is multiplied by 1+r1+r⁠, and then drops by the payment:
B0=1200B_0=1200 and Bk=(1+r) Bk−1−220B_k=(1+r)\,B_{k-1}-220⁠, where BkB_k is the debt after kk payments.
The rate the shop charges is the rr for which the debt is exactly zero after the sixth payment:
B6=0B_6=0⁠. The table follows the debt for three rates (a negative number means that we paid more than we owed):

Month kkr=0r=0r=5%r=5\%r=2.793055%r=2.793055\%
0120012001200
19801040.01013.5
2760872.0821.8
3540695.6624.8
4320510.4422.2
5100315.9214.0
6−120111.70.0

At r=0r=0 the debt is gone before the last payment and we overpay by 120 (this is f(0)=120f(0)=120 below); at 5% a month a debt of 111.7 remains after six payments, so the rate is too high; at 2.793055% the sixth payment clears the debt exactly:
that is the hidden rate.

Written out, B6=0B_6=0 reads

1200 (1+r)6=220∑k=05(1+r)k=220[(1+r)5+(1+r)4+⋯+(1+r)+1]1200\,(1+r)^6=220\sum_{k=0}^{5}(1+r)^{k}=220\left[(1+r)^5+(1+r)^4+\cdots+(1+r)+1\right]

Dividing both sides by (1+r)6(1+r)^6 gives the discounted form 220∑k=16(1+r)−k=1200220\sum_{k=1}^{6}(1+r)^{-k}=1200⁠:
to discount a payment is to compute what it is worth today, so a payment of 220 made kk months from now is worth 220(1+r)k\dfrac{220}{(1+r)^{k}} today, and the rate we look for is the one at which the six discounted payments are worth exactly the price 1200.
Moving everything to one side defines the function ff whose root we look for:

f(r)=∑t=16220(1+r)t−1200=2201+r+220(1+r)2+⋯+220(1+r)6−1200=0\begin{aligned} f(r)&=\sum_{t=1}^{6}\frac{220}{(1+r)^{t}}-1200\\ &=\frac{220}{1+r}+\frac{220}{(1+r)^{2}}+\cdots+\frac{220}{(1+r)^{6}}-1200=0 \end{aligned}

Indeed f(r)=−B6(1+r)6f(r)=-\dfrac{B_6}{(1+r)^6}⁠, so f(r)=0f(r)=0 exactly when the debt is zero after the sixth payment.
The sign tells us which way to correct: f(r)>0f(r)>0 means that the debt would be gone too early (the rate rr is too low), and f(r)<0f(r)<0 means that a debt remains (the rate is too high).

Comparing with the bank. Let r1r_1 be the monthly rate the shop charges (here r1=2.793%r_1=2.793\%⁠) and r2r_2 the monthly rate we get from the bank on our savings.
If r2>r1r_2>r_1 it is worth paying in instalments and keeping the 1200 in the bank, because the interest we earn is higher than the interest the shop charges; if r2<r1r_2<r_1 it is better to pay cash.
A rate of 2.793% a month is about 39.2% a year (1.0279312−1≈0.3921.02793^{12}-1\approx0.392⁠), while a deposit paying 5% a year is only about 0.41% a month (1.051/12−1≈0.00411.05^{1/12}-1\approx0.0041⁠), far below r1r_1⁠:
here we pay cash, and the instalments would pay off only if the bank paid more than 2.793% a month.
The same holds for a bank loan: if it costs less than r1r_1 per month, taking it and paying cash is cheaper.
(We ignore taxes, fees and the difference between deposit and loan rates.)

The task is one equation in one unknown rr that we can evaluate but not solve by hand, and this lesson solves it numerically.
The unknown rr sits inside powers of 1+r1+r up to the sixth, and there is no simple formula that isolates it.
We can, however, compute f(r)f(r) for any rr we like, and that is enough.

Work it out before reading on:

  1. What is f(0)f(0)⁠? (At a rate of zero nothing is discounted.)
  2. What does f(r)f(r) approach when rr is very large?
  3. Given 1 and 2, what must be true of the equation, and where should we look?
Solution
  1. The payments add up to 6⋅220=13206\cdot220=1320⁠, so f(0)=1320−1200=120>0f(0)=1320-1200=120>0⁠: without interest we overpay by 120, as the table shows.
  2. As rr grows, every fraction 220(1+r)t\dfrac{220}{(1+r)^t} tends to 0, so lim⁡r→∞f(r)=−1200<0\lim_{r\to\infty}f(r)=-1200<0⁠. What does a negative value mean? A debt remains after the sixth payment, so the rate is too high. It is already negative at r=0.05r=0.05⁠: f(0.05)≈−83.35f(0.05)\approx-83.35⁠, which is the remaining debt 111.7 of the table divided by 1.0561.05^6⁠, with a minus sign.
  3. ff is positive at 0 (the rate is too low) and negative at 0.05 (too high), and it has no jumps, so its graph must cross zero somewhere between. A root, the monthly rate, lies in [0,0.05][0,0.05]⁠, and it is the only one: the discounted payments decrease as the rate grows, so ff decreases.

Why a root must exist

A continuous function cannot go from a positive value to a negative one without passing through zero.
This is the intermediate value theorem: if ff is continuous on [a,b][a,b] and f(a)f(a) and f(b)f(b) have opposite signs, that is, f(a) f(b)<0f(a)\,f(b)<0 (one is positive and the other negative), then f(c)=0f(c)=0 for some cc strictly between aa and bb⁠.
An interval [a,b][a,b] with f(a) f(b)≤0f(a)\,f(b)\le0⁠, the same condition with 0 allowed, is a bracket:
an end that is already a root ends the search. The theorem does not say where a root is, only that searching inside a bracket cannot fail.
We prove it with the help of bisection itself, so the proof comes after we meet the method.
The theorem needs only continuity and opposite signs; nothing else about ff⁠.

What “continuous” means. In plain words, the graph can be drawn without lifting the pen:
no jumps, and no points where ff is undefined. That is a description; the precise definition is this.
ff is continuous at x0x_0 if for every accuracy η>0\eta>0 there is a distance γ>0\gamma>0 such that ∣x−x0∣<γ|x-x_0|<\gamma implies ∣f(x)−f(x0)∣<η|f(x)-f(x_0)|<\eta for every xx in the interval (the usual letters are ε\varepsilon and δ\delta⁠, but on this page they already mean the step εn\varepsilon_n and the residual δn\delta_n⁠).
In words: a small change of xx causes a small change of f(x)f(x)⁠.
ff is continuous on [a,b][a,b] if it is continuous at every point of it; at the end points it is continuous towards the inside of the interval (at aa from the right, at bb from the left).
For example x2x^2 is continuous, while 1x−0.3\dfrac{1}{x-0.3} is not continuous on [0,1][0,1]⁠, because it is not even defined at 0.30.3⁠, a point of the interval.

Refute it: “If f(a)f(b)>0f(a)f(b)>0⁠, then ff has no root in [a,b][a,b]⁠.” Find a continuous function and an interval that show the claim is wrong.

Solution

For f(x)=x2f(x)=x^2 on [−1,1][-1,1] both end values are 11⁠, yet x=0x=0 is a root.
For f(x)=x2−1f(x)=x^2-1 on [−2,2][-2,2] both values are 33⁠, but x=−1x=-1 and x=1x=1 are roots:
equal signs can hide two roots, and they do not tell us where to look.

Check yourself. Two of these four statements about the sign test f(a) f(b)≤0f(a)\,f(b)\le0 are true; pick one of them.

A: for a continuous ff, when the test holds there is a root in [a,b][a,b]

True: this is the intermediate value theorem, with 0 allowed.
The test is sufficient.

B: for a continuous ff, when the test fails there is no root in [a,b][a,b]

False. For f(x)=x2−1f(x)=x^2-1 on [−2,2][-2,2] we have f(−2)=f(2)=3f(-2)=f(2)=3 and the product is 9>09>0⁠, yet x=−1x=-1 and x=1x=1 are roots.
The test is sufficient, not necessary.

C: if f(a)=0f(a)=0 the test still holds, but the search is already finished

True: the product is 00⁠, so the test holds, and aa is already a root.

D: the test suits discontinuous functions too

False: the theorem needs continuity. For f(x)=1x−0.3f(x)=\dfrac{1}{x-0.3} on [0,1][0,1] the product f(0) f(1)≈−3.33⋅1.43f(0)\,f(1)\approx-3.33\cdot1.43 is negative, yet ff has no root:
see the example at the end of the page.

Bisection: halve the bracket

The bracket we start from is [a0,b0]=[a,b][a_0,b_0]=[a,b]⁠. Iteration nn takes the bracket [an−1,bn−1][a_{n-1},b_{n-1}] and looks at its middle, cn=an−1+bn−12c_n=\dfrac{a_{n-1}+b_{n-1}}{2}⁠.
Either f(an−1)f(a_{n-1}) and f(cn)f(c_n) have opposite signs, so there is a root in [an−1,cn][a_{n-1},c_n]⁠, or f(cn)f(c_n) and f(bn−1)f(b_{n-1}) do, so there is a root in [cn,bn−1][c_n,b_{n-1}]⁠.
The half we keep is the new bracket [an,bn][a_n,b_n]⁠, and we repeat. The explorer below runs the method on the monthly payments; drag aa and bb⁠, step through, and watch the shaded bracket shrink.

The explorer needs JavaScript. The tables below show the first steps of bisection on the monthly payments.

Back to the challenge. The strategy many of you found in the challenge, test the middle and keep the half where the sign changes, is the bisection method.
Work it out: how wide is the interval of the challenge after nn tests, and how many tests guarantee in advance a width smaller than 0.010.01⁠?

Solution

Each test at the middle halves the width, so after nn tests the width of the interval [0,2][0,2] is 22n\dfrac{2}{2^{n}}⁠.
After 7 tests the width is 0.01560.0156⁠, still too wide, and after 8 it is 0.00780.0078⁠.
Eight tests are enough for every continuous function, and we can promise them before seeing it:
8 is the first nn with n>log⁡220.01≈7.64n>\log_2\dfrac{2}{0.01}\approx7.64⁠. A test near one of the ends guarantees nothing:
if the sign changes in the large part, the interval has hardly shrunk.
The size of ∣f∣|f| guarantees nothing either: a small value does not mean that the root is close.

What bisection adds compared with a blind search: a guarantee.
It needs only the signs of ff⁠, never fails inside a bracket, and tells exactly how close it is.
The approximation produced by iteration nn is cnc_n⁠, the only point where ff was evaluated.
It is always an end of the new bracket [an,bn][a_n,b_n] (either [an−1,cn][a_{n-1},c_n] or [cn,bn−1][c_n,b_{n-1}]⁠), and that bracket contains a root and has width b−a2n=εn=∣cn−an−1∣\dfrac{b-a}{2^{n}}=\varepsilon_n=\lvert c_n-a_{n-1}\rvert⁠, so every root in it is at most εn\varepsilon_n away from cnc_n⁠.
The bracket is the guarantee and cnc_n is the value we return.
After NN iterations the last approximation is cNc_N and the remaining bracket is [aN,bN][a_N,b_N]⁠.
(The midpoint of the new bracket would have half the bound without a new evaluation of ff⁠: it is exactly the cn+1c_{n+1} that the next iteration computes.)

The challenge asked how many tests we can promise before seeing the function.
For bisection the number of iterations can be bounded in advance:
the formula below tells how many are enough. By the course convention we stop at the first iteration nn with εn≤εtol\varepsilon_n\le\varepsilon_{\text{tol}}⁠, where the tolerance εtol\varepsilon_{\text{tol}} is the error we agree to accept:
we require the approximation to be at most εtol\varepsilon_{\text{tol}} away from a root.
Since εn=b−a2n\varepsilon_n=\dfrac{b-a}{2^{n}}⁠, this is the first nn with

n ≥ log⁡2b−aεtol,that isN=⌈log⁡2b−aεtol⌉n\ \ge\ \log_2\frac{b-a}{\varepsilon_{\text{tol}}},\qquad\text{that is}\qquad N=\left\lceil\log_2\frac{b-a}{\varepsilon_{\text{tol}}}\right\rceil

and NN is the number of iterations (at least one iteration is performed, so N=max⁡(1,⌈log⁡2b−aεtol⌉)N=\max\bigl(1,\lceil\log_2\frac{b-a}{\varepsilon_{\text{tol}}}\rceil\bigr) when εtol≥b−a\varepsilon_{\text{tol}}\ge b-a⁠).
For [0,0.05][0,0.05] and εtol=10−6\varepsilon_{\text{tol}}=10^{-6} the right side is about 15.615.6⁠, so N=16N=16 iterations.
When the quotient is exactly a power of 2 no extra iteration is needed, as in the example of the course conventions, ε in bisection.

Bisection as a recursive definition. A definition with a stopping condition and a call to itself:

bisect(a, b):
    c = (a + b) / 2
    if f(c) = 0 or (b - a) / 2 <= tol:
        return c
    if f(a) * f(c) <= 0:
        return bisect(a, c)
    otherwise:
        return bisect(c, b)

Each call is one iteration and the interval halves at every call, so the recursion ends after at most N=⌈log⁡2b−aεtol⌉N=\left\lceil\log_2\frac{b-a}{\varepsilon_{\text{tol}}}\right\rceil calls; the test (b−a)/2≤tol(b-a)/2\le\text{tol} is εn≤εtol\varepsilon_n\le\varepsilon_{\text{tol}} of that iteration.

The tolerance cannot be as small as we like: it cannot be smaller than the gap between stored numbers, and around a root rr that is a normal number (not zero and not subnormal) that gap is larger than εmach∣r∣/2\varepsilon_{\text{mach}}\lvert r\rvert/2 and at most εmach∣r∣\varepsilon_{\text{mach}}\lvert r\rvert⁠.
So a tolerance below εmach∣r∣/2\varepsilon_{\text{mach}}\lvert r\rvert/2 is below the gap: the step test could then hold only because cnc_n equals an end of the bracket [an−1,bn−1][a_{n-1},b_{n-1}] it was computed from, so that εn=0\varepsilon_n=0⁠, which says nothing about the error, and εmach∣r∣\varepsilon_{\text{mach}}\lvert r\rvert is a safe floor (here εmach\varepsilon_{\text{mach}} is that of the number format).

Proof: bisection cannot fail, and this proves the intermediate value theorem

So far we have said three things about bisection: the bracket stays a bracket, it contains a root, and every root in it is at most εn\varepsilon_n from cnc_n⁠.
We prove them. The bracket we start from is [a0,b0]=[a,b][a_0,b_0]=[a,b]⁠; iteration n≥1n\ge1 takes [an−1,bn−1][a_{n-1},b_{n-1}]⁠, computes its midpoint cnc_n⁠, and keeps one of the two halves as [an,bn][a_n,b_n]⁠.

Claim 1. For every n≥0n\ge0⁠, f(an) f(bn)≤0f(a_n)\,f(b_n)\le0⁠: the interval [an,bn][a_n,b_n] is a bracket.

Proof, by induction on nn⁠. Base, n=0n=0⁠. [a0,b0]=[a,b][a_0,b_0]=[a,b] is a bracket by assumption.
Hypothesis. For some n≥1n\ge1⁠, f(an−1) f(bn−1)≤0f(a_{n-1})\,f(b_{n-1})\le0⁠. Step. Let cn=an−1+bn−12c_n=\dfrac{a_{n-1}+b_{n-1}}{2}⁠.

  • Case 1: f(an−1) f(cn)≤0f(a_{n-1})\,f(c_n)\le0⁠. We keep [an,bn]=[an−1,cn][a_n,b_n]=[a_{n-1},c_n]⁠, and f(an) f(bn)≤0f(a_n)\,f(b_n)\le0⁠.
  • Case 2: f(an−1) f(cn)>0f(a_{n-1})\,f(c_n)>0⁠. Then f(cn)≠0f(c_n)\ne0⁠, and [f(an−1) f(cn)]⋅[f(cn) f(bn−1)]=f(cn)2 f(an−1) f(bn−1)≤0\bigl[f(a_{n-1})\,f(c_n)\bigr]\cdot\bigl[f(c_n)\,f(b_{n-1})\bigr]=f(c_n)^2\,f(a_{n-1})\,f(b_{n-1})\le0⁠. The first factor is positive, so the second is at most 0: f(cn) f(bn−1)≤0f(c_n)\,f(b_{n-1})\le0⁠. We keep [an,bn]=[cn,bn−1][a_n,b_n]=[c_n,b_{n-1}]⁠, and f(an) f(bn)≤0f(a_n)\,f(b_n)\le0⁠.

In both cases the claim holds for nn⁠. Conclusion: by induction it holds for every n≥0n\ge0⁠.
(The zero case counts: the course convention for a bracket is ≤0\le0⁠.)

Claim 2 (the intermediate value theorem). If ff is continuous on [a,b][a,b] and f(a) f(b)≤0f(a)\,f(b)\le0⁠, there is an r∈[a,b]r\in[a,b] with f(r)=0f(r)=0⁠.
The proof uses bisection itself, all the way:

  1. The intervals are nested: for every n≥1n\ge1⁠, [an,bn]⊆[an−1,bn−1][a_n,b_n]\subseteq[a_{n-1},b_{n-1}]⁠, because by the step rule [an,bn][a_n,b_n] is one of the two halves of [an−1,bn−1][a_{n-1},b_{n-1}]⁠. Hence for every n≥0n\ge0 we have an≤an+1≤bn+1≤bna_n\le a_{n+1}\le b_{n+1}\le b_n⁠, that is, a0≤a1≤a2≤⋯≤b2≤b1≤b0a_0\le a_1\le a_2\le\cdots\le b_2\le b_1\le b_0⁠.
  2. The ends converge: for every n≥0n\ge0 we have an≤an+1a_n\le a_{n+1}⁠, that is, a0≤a1≤a2≤⋯a_0\le a_1\le a_2\le\cdots⁠, so the numbers ana_n increase and are bounded above by b0b_0⁠, so the limit α=lim⁡n→∞an\alpha=\lim_{n\to\infty}a_n exists (a bounded increasing sequence converges: a theorem of calculus, with a reminder of its proof below). In the same way for every n≥0n\ge0 we have bn+1≤bnb_{n+1}\le b_n⁠, that is, b0≥b1≥b2≥⋯b_0\ge b_1\ge b_2\ge\cdots⁠, so the numbers bnb_n decrease and are bounded below by a0a_0⁠, so the limit β=lim⁡n→∞bn\beta=\lim_{n\to\infty}b_n exists.
  3. The limits are equal: the width is halved at every iteration, so bn−an=b−a2nb_n-a_n=\dfrac{b-a}{2^{n}}⁠, and therefore lim⁡n→∞(bn−an)=0\lim_{n\to\infty}(b_n-a_n)=0⁠, that is, β−α=0\beta-\alpha=0⁠. Write r=α=βr=\alpha=\beta⁠. Since ana_n increases to rr and bnb_n decreases to rr⁠, an≤r≤bna_n\le r\le b_n for every nn⁠.
  4. rr is a root: ff is continuous at rr⁠, so lim⁡n→∞f(an)=f(r)\lim_{n\to\infty}f(a_n)=f(r) and lim⁡n→∞f(bn)=f(r)\lim_{n\to\infty}f(b_n)=f(r)⁠, and therefore lim⁡n→∞f(an) f(bn)=f(r)2\lim_{n\to\infty}f(a_n)\,f(b_n)=f(r)^2⁠. By Claim 1 every term of this sequence is at most 0, so the limit is too: f(r)2≤0f(r)^2\le0⁠. Only f(r)=0f(r)=0 satisfies this. If f(a) f(b)<0f(a)\,f(b)<0 the ends are not roots, so rr lies strictly between aa and bb⁠.
Why does rr now mean a root?

In the monthly-payments example rr was the interest rate (the letter for “rate”).
Here rr is a root of ff⁠. The two meet in the example because the root of that ff is the interest rate we were looking for; in a general proof rr is just the name of a root.

A reminder from calculus: a bounded monotone sequence converges

This is a theorem of calculus, repeated here as a reminder; it is not part of what we teach in the lecture.
We use the completeness of the real numbers as known: every non-empty set of real numbers that is bounded above has a least upper bound, called its supremum.

Claim. Suppose an≤an+1a_n\le a_{n+1} for every n≥0n\ge0 (the numbers do not decrease) and there is a number KK with an≤Ka_n\le K for every nn⁠.
Then the limit lim⁡n→∞an\lim_{n\to\infty}a_n exists, and it equals SS⁠, the supremum of the set AA of all the numbers ana_n⁠.

Proof. The set AA is not empty and is bounded above by KK⁠, so by completeness it has a supremum SS⁠.
Let ε>0\varepsilon>0⁠. The number S−εS-\varepsilon is smaller than the least upper bound, so it is not an upper bound of AA⁠:
there is an index NN with aN>S−εa_N>S-\varepsilon⁠. The numbers do not decrease, so for every n≥Nn\ge N

S−ε<aN≤an≤SS-\varepsilon<a_N\le a_n\le S

and therefore ∣an−S∣<ε|a_n-S|<\varepsilon for every n≥Nn\ge N⁠. This is the definition of lim⁡n→∞an=S\lim_{n\to\infty}a_n=S⁠.

Conclusion, and the decreasing case. A non-decreasing sequence that is bounded above converges to its supremum.
If instead an≥an+1a_n\ge a_{n+1} for every nn and an≥Ka_n\ge K (bounded below), apply the claim to the numbers −an-a_n⁠:
they do not decrease and are bounded above by −K-K⁠, so they converge to their supremum, and ana_n converges to the infimum of AA⁠.

The graph of the monthly-payments function on the interval from 0 to 0.05, and below it the brackets [a_n, b_n] for n from 0 to 5. Each bracket is half of the one before and still contains the root 0.027931.

The brackets [an,bn][a_n,b_n] of the monthly-payments equation, for n=0,…,5n=0,\dots,5⁠:
each is half of the one before, all contain rr⁠, and their width 0.052n\dfrac{0.05}{2^{n}} tends to 0.

Claim 3 (the error and the number of iterations). Let rr be any root in [an,bn][a_n,b_n] (in particular the rr of Claim 2).
The approximation cnc_n is an end of [an,bn][a_n,b_n]⁠, so the distance between them is at most the width of the bracket:

∣cn−r∣ ≤ bn−an=b−a2 n=εn|c_n-r|\ \le\ b_n-a_n=\frac{b-a}{2^{\,n}}=\varepsilon_n

We stop at the first iteration with εn≤εtol\varepsilon_n\le\varepsilon_{\text{tol}}⁠, that is, 2n≥b−aεtol2^n\ge\dfrac{b-a}{\varepsilon_{\text{tol}}}⁠, and taking logarithms n≥log⁡2b−aεtoln\ge\log_2\dfrac{b-a}{\varepsilon_{\text{tol}}}⁠; the smallest such integer is N=⌈log⁡2b−aεtol⌉N=\left\lceil\log_2\dfrac{b-a}{\varepsilon_{\text{tol}}}\right\rceil⁠.
For the monthly payments ε16=0.05216≈7.6×10−7≤10−6\varepsilon_{16}=\dfrac{0.05}{2^{16}}\approx7.6\times10^{-7}\le10^{-6}⁠, while ε15≈1.5×10−6>10−6\varepsilon_{15}\approx1.5\times10^{-6}>10^{-6}⁠: N=16N=16 iterations, as computed above.
The claim covers every root inside the bracket; other roots of [a,b][a,b] outside it are not covered, and bisection finds one of the roots.

In the real world: bisection in code that runs today

  • Finding the commit that introduced a bug. The documentation of git bisect describes it as a binary search over the history of the project: each test leaves half of the suspect commits, like bisection. In a linear history of 1000 commits ⌈log⁡21000⌉=10\lceil\log_2 1000\rceil=10 tests are enough; we ran the command on such a history and it ran the test 10 times (it prints “roughly 9 steps” because it counts the steps after the current one). In a real history, with merges, the number is approximate. There can be several “first bad” commits, and the search finds one of them, like a bracket that contains several roots.
  • The SciPy library. The function scipy.optimize.bisect halves the interval, refuses to run when the signs at the ends are equal, and its documentation says “Slow but sure”.
  • Checking the bracket before starting. Libraries such as SciPy, Boost, QuantLib and GSL check that the end values have opposite signs and report an error instead of continuing if the condition does not hold; a comment in the code of Boost argues that if the signs at two points are opposite then, “f being continuous”, there is certainly a root between them: this is the intermediate value theorem used as a guarantee of existence. The code tests signs only, and continuity is left to the caller. Equal signs do not prove that there is no root, and a sign change proves at least one root, not exactly one.

What bisection guarantees and what it does not

The cost of bisection is speed: the bound εn\varepsilon_n on the error only halves each step, a rate that we define and measure in the page on the rate of convergence, and the method ignores how large the values of ff are.
The error itself need not shrink at every step: in the table below c4=0.028125c_4=0.028125 is closer to the root than c5=0.0265625c_5=0.0265625⁠.

In the table below the last column is the ratio of consecutive errors, ∣cn−r∣∣cn−1−r∣\dfrac{\lvert c_n-r\rvert}{\lvert c_{n-1}-r\rvert}⁠.
It jumps: 7.04 means that the error grew at that step, because bisection guarantees the bound εn\varepsilon_n⁠, not a fall of the error at every step.
We define the ratio properly on the rate-of-convergence page.

The first iterations on the monthly payments: row nn shows the approximation cnc_n⁠, f(cn)f(c_n) and the new bracket [an,bn][a_n,b_n]⁠, and row 0 is the starting bracket.
In the column f(c)f(c)⁠, negative means that a debt remains (the rate cc is too high) and positive that cc is too low.
The true monthly rate is 2.793%, the rate at which f=0f=0⁠:

nncnc_nf(cn)f(c_n)ana_nbnb_n∣cn−r∣∣cn−1−r∣\dfrac{\lvert c_n-r\rvert}{\lvert c_{n-1}-r\rvert}
0––00.05–
10.02511.790.0250.05–
20.0375−37.280.0250.03753.27
30.03125−13.140.0250.031250.347
40.028125−0.77590.0250.0281250.0586
50.02656255.4810.02656250.0281257.04
60.027343752.3460.027343750.0281250.429
70.0277343750.78350.0277343750.0281250.334
80.02792968750.0034250.02792968750.0281250.00437

The highlighted row: the midpoint closest to the root up to row 7.

Self-study, importance 2 This part is for self-study: it is not taught in the lecture, and you read it on your own. the calculator guide for bisection, a table of values and CALC It helps with: computing bisection steps on a scientific calculator.

Try it on a calculator. The emulator below is a scientific calculator with a typical layout (ES).
Follow the steps: the key to press is highlighted, and you can also press the keys yourself.
We use f(x)=x2−2f(x)=x^2-2 on [1,2][1,2]⁠, whose root is 2\sqrt2⁠. There are two ways:
build a table of values (TABLE) and look in it for the change of sign, or use CALC to evaluate ff at the midpoint of each bracket.
Switch between them with the tabs above the guide text.

When to stop? Two quantities tell us how close we are: the step εn\varepsilon_n⁠, and the residual δn=∣f(cn)∣\delta_n=\lvert f(c_n)\rvert⁠.
We stop at the first iteration where εn≤εtol\varepsilon_n\le\varepsilon_{\text{tol}} and δn≤δtol\delta_n\le\delta_{\text{tol}}⁠; for the monthly rate, εtol=10−6\varepsilon_{\text{tol}}=10^{-6} is 0.0001 percentage points.
The explorer uses this combined rule (both the step εn\varepsilon_n and the residual δn\delta_n must meet their thresholds), so it takes more steps than a stopping condition on εn\varepsilon_n alone:
for εtol=δtol=10−6\varepsilon_{\text{tol}}=\delta_{\text{tol}}=10^{-6} it shows 27 rows against the 16 steps of the condition on εn\varepsilon_n (about 11 more).
Here ff is very steep: its slope near the root is about −3992-3992⁠, which means that near the root a small change Δr\Delta r in rr changes ff by about 3992 Δr3992\,\Delta r (a local approximation):
a deviation of 10−610^{-6} in rr changes ff by about 0.0040.004⁠.
The residual δn=∣f(cn)∣\delta_n=|f(c_n)| is about 3992 times the error ∣cn−r∣|c_n-r|⁠, so to get below δtol=10−6\delta_{\text{tol}}=10^{-6} we must come about 3992 times closer to the root than εtol=10−6\varepsilon_{\text{tol}}=10^{-6} requires:
the condition on δn\delta_n is the stricter one.

The plot below shows the function of the monthly payments with its tangent line, so the steepness can be seen and felt.

The graph needs JavaScript. The table below gives the same information.

rrf(r)f(r)slope f′(r)f'(r)
00120120−4620-4620
0.010.0175.0075.00−4381.7-4381.7
0.020.0232.3132.31−4158.8-4158.8
0.0279310.027931 (the root)00−3992.1-3992.1
0.040.04−46.73-46.73−3754.5-3754.5
0.050.05−83.35-83.35−3571.1-3571.1

Drag the point along the curve of the monthly payments f(r)f(r) and watch the slope f′(r)f'(r) of the tangent. The steepness is per unit of rr⁠, not the angle on the screen: the curve looks gentle only because rr spans 0.05 while ff spans 220.

In the highlighted row of the table above, c4c_4 is closer to the root than c5c_5⁠, c6c_6 and even c7c_7⁠:
∣f(c4)∣=0.7759|f(c_4)|=0.7759 is smaller than 5.4815.481⁠, 2.3462.346 and 0.78350.7835⁠, and its distance from the root, 1.94×10−41.94\times10^{-4}⁠, is smaller than 1.37×10−31.37\times10^{-3}⁠, 5.87×10−45.87\times10^{-4} and 1.96×10−41.96\times10^{-4}⁠.

These are two thresholds in two different units: εtol\varepsilon_{\text{tol}} bounds the error in the unknown (here a rate), δtol\delta_{\text{tol}} bounds the residual (here the gap between the discounted payments and the price).
The explorer uses the same number for both, to keep it simple; in a program of our own we would choose them separately.
Only a bracket comes with a guarantee: the root stays inside it, so its width bounds the error; in bisection that width is known in advance and tends to 0.
Without a bracket, a small step and a small ∣f∣|f| are strong signs of accuracy, not a proof.

The answer to the rate question. With the tolerance of the explorer, εtol=δtol=10−8\varepsilon_{\text{tol}}=\delta_{\text{tol}}=10^{-8}⁠, bisection stops at iteration n=34n=34 with c34≈0.02793055c_{34}\approx0.02793055⁠.
The monthly rate the shop charges is 2.793055%2.793055\%⁠, and it is at most ε34=0.05234≈2.9×10−12\varepsilon_{34}=\dfrac{0.05}{2^{34}}\approx2.9\times10^{-12} away from c34c_{34}⁠.
By the condition on εn\varepsilon_n alone 23 iterations would be enough (ε23≈6.0×10−9\varepsilon_{23}\approx6.0\times10^{-9}⁠), but at iteration 23 the residual is still 2.2×10−52.2\times10^{-5}⁠, larger than the tolerance.

Want to work through the whole method from the start, with exercises?
My tutorial on bisection:

When the assumption is wrong: a sign change without a root

The theorem needs continuity, and the method does not check it.
Take f(x)=1x−0.3f(x)=\dfrac{1}{x-0.3} on [0,1][0,1]⁠. Here f(0)≈−3.33<0f(0)\approx-3.33<0 and f(1)≈1.43>0f(1)\approx1.43>0⁠: opposite signs, so the bracket looks fine.

Predict: does ff have a root in [0,1][0,1]⁠? What will bisection do, and what will ∣f∣|f| look like at the end?

Solution

A fraction 1x−0.3\frac1{x-0.3} is never 00⁠, so there is no root. The sign changes because ff is undefined at x=0.3x=0.3⁠:
it jumps from very large negative to very large positive values there.
Bisection does not notice, and the bracket shrinks toward 0.30.3⁠:

nncnc_nf(cn)f(c_n)bracket keptwidth
10.55[0,  0.5][0,\;0.5]0.5
20.25−20[0.25,  0.5][0.25,\;0.5]0.25
30.37513.33[0.25,  0.375][0.25,\;0.375]0.125
40.312580[0.25,  0.3125][0.25,\;0.3125]0.0625
50.28125−53.33[0.28125,  0.3125][0.28125,\;0.3125]0.03125
60.296875−320[0.296875,  0.3125][0.296875,\;0.3125]0.015625
70.3046875213.33[0.296875,  0.3046875][0.296875,\;0.3046875]0.0078125

The width halves every time, as always, yet ∣f(cn)∣|f(c_n)| grows instead of shrinking.

The theorem guarantees a root only for a continuous function; when the assumption is wrong, bisection keeps working technically, the bracket keeps shrinking, but the answer misleads.
This is why the course conventions check two things when a method stops:
that the steps became small (here the width did), and that the answer solves the problem, that is, ∣f∣|f| is small there (here it is not).

Summary: bisection

Bisection needs a bracket and a continuous function, and never fails inside it:
we proved that the bracket stays a bracket and that the error is at most εn=b−a2n\varepsilon_n=\dfrac{b-a}{2^n}⁠.
The bound halves at each step (we define the rate in the page on the rate of convergence), and the method ignores how large the values of ff are.

Continue learning

Regula falsi uses the size of the values of ff as well: its page proves when this shortens the search and when it does not.
After it comes the rate of convergence, and then the practice session for the three lectures:
a table by hand, the usual mistakes, transfer questions and a Python implementation.