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 : 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 wide), with as few tests as possible.
The challenge needs JavaScript. A printable version of the question follows.
The question. A continuous function on has and .
You may ask for 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 ?
| Test | Why this point? | 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 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 has and . Which of these is impossible?
A: has no zero in
Impossible: the curve must get from to without lifting its pencil, so it crosses zero somewhere.
We state this idea precisely, and give it its name, later on this page.
B:
Possible: nothing fixes the value at the midpoint; the two end values say nothing about it.
C: has three zeros in
Possible: a continuous curve can cross zero three times (up, down, up) and still go from negative to positive.
D:
Possible: the curve may first go lower and then rise to .
2. The interval is halved three times, always keeping the half that contains a root.
How wide is what is left?
A:
That is the width after four halvings.
B:
Three halvings divide the width by : .
C:
That is the width after two halvings.
D:
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 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 , that is, it is multiplied by , and then drops by the payment:
and , where is the debt after payments.
The rate the shop charges is the for which the debt is exactly zero after the sixth payment:
. The table follows the debt for three rates (a negative number means that we paid more than we owed):
| Month | |||
|---|---|---|---|
| 0 | 1200 | 1200 | 1200 |
| 1 | 980 | 1040.0 | 1013.5 |
| 2 | 760 | 872.0 | 821.8 |
| 3 | 540 | 695.6 | 624.8 |
| 4 | 320 | 510.4 | 422.2 |
| 5 | 100 | 315.9 | 214.0 |
| 6 | −120 | 111.7 | 0.0 |
At the debt is gone before the last payment and we overpay by 120 (this is 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, reads
Dividing both sides by gives the discounted form :
to discount a payment is to compute what it is worth today, so a payment of 220 made months from now is worth 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 whose root we look for:
Indeed , so exactly when the debt is zero after the sixth payment.
The sign tells us which way to correct: means that the debt would be gone too early (the rate is too low), and means that a debt remains (the rate is too high).
Comparing with the bank. Let be the monthly rate the shop charges (here ) and the monthly rate we get from the bank on our savings.
If 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 it is better to pay cash.
A rate of 2.793% a month is about 39.2% a year (), while a deposit paying 5% a year is only about 0.41% a month (), far below :
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 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 that we can evaluate but not solve by hand, and this lesson solves it numerically.
The unknown sits inside powers of up to the sixth, and there is no simple formula that isolates it.
We can, however, compute for any we like, and that is enough.
Work it out before reading on:
- What is ? (At a rate of zero nothing is discounted.)
- What does approach when is very large?
- Given 1 and 2, what must be true of the equation, and where should we look?
Solution
- The payments add up to , so : without interest we overpay by 120, as the table shows.
- As grows, every fraction tends to 0, so . What does a negative value mean? A debt remains after the sixth payment, so the rate is too high. It is already negative at : , which is the remaining debt 111.7 of the table divided by , with a minus sign.
- 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 , and it is the only one: the discounted payments decrease as the rate grows, so 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 is continuous on and and have opposite signs, that is, (one is positive and the other negative), then for some strictly between and .
An interval with , 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 .
What “continuous” means. In plain words, the graph can be drawn without lifting the pen:
no jumps, and no points where is undefined. That is a description; the precise definition is this.
is continuous at if for every accuracy there is a distance such that implies for every in the interval (the usual letters are and , but on this page they already mean the step and the residual ).
In words: a small change of causes a small change of .
is continuous on if it is continuous at every point of it; at the end points it is continuous towards the inside of the interval (at from the right, at from the left).
For example is continuous, while is not continuous on , because it is not even defined at , a point of the interval.
Refute it: “If , then has no root in .” Find a continuous function and an interval that show the claim is wrong.
Solution
For on both end values are , yet is a root.
For on both values are , but and 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 are true; pick one of them.
A: for a continuous , when the test holds there is a root in
True: this is the intermediate value theorem, with 0 allowed.
The test is sufficient.
B: for a continuous , when the test fails there is no root in
False. For on we have and the product is , yet and are roots.
The test is sufficient, not necessary.
C: if the test still holds, but the search is already finished
True: the product is , so the test holds, and is already a root.
D: the test suits discontinuous functions too
False: the theorem needs continuity. For on the product is negative, yet has no root:
see the example at the end of the page.
Bisection: halve the bracket
The bracket we start from is . Iteration takes the bracket and looks at its middle, .
Either and have opposite signs, so there is a root in , or and do, so there is a root in .
The half we keep is the new bracket , and we repeat. The explorer below runs the method on the monthly payments; drag and , 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 tests, and how many tests guarantee in advance a width smaller than ?
Solution
Each test at the middle halves the width, so after tests the width of the interval is .
After 7 tests the width is , still too wide, and after 8 it is .
Eight tests are enough for every continuous function, and we can promise them before seeing it:
8 is the first with . 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 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 , never fails inside a bracket, and tells exactly how close it is.
The approximation produced by iteration is , the only point where was evaluated.
It is always an end of the new bracket (either or ), and that bracket contains a root and has width , so every root in it is at most away from .
The bracket is the guarantee and is the value we return.
After iterations the last approximation is and the remaining bracket is .
(The midpoint of the new bracket would have half the bound without a new evaluation of : it is exactly the 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 with , where the tolerance is the error we agree to accept:
we require the approximation to be at most away from a root.
Since , this is the first with
and is the number of iterations (at least one iteration is performed, so when ).
For and the right side is about , so 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 calls; the test is 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 that is a normal number (not zero and not subnormal) that gap is larger than and at most .
So a tolerance below is below the gap: the step test could then hold only because equals an end of the bracket it was computed from, so that , which says nothing about the error, and is a safe floor (here 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 from .
We prove them. The bracket we start from is ; iteration takes , computes its midpoint , and keeps one of the two halves as .
Claim 1. For every , : the interval is a bracket.
Proof, by induction on . Base, . is a bracket by assumption.
Hypothesis. For some , . Step. Let .
- Case 1: . We keep , and .
- Case 2: . Then , and . The first factor is positive, so the second is at most 0: . We keep , and .
In both cases the claim holds for . Conclusion: by induction it holds for every .
(The zero case counts: the course convention for a bracket is .)
Claim 2 (the intermediate value theorem). If is continuous on and , there is an with .
The proof uses bisection itself, all the way:
- The intervals are nested: for every , , because by the step rule is one of the two halves of . Hence for every we have , that is, .
- The ends converge: for every we have , that is, , so the numbers increase and are bounded above by , so the limit exists (a bounded increasing sequence converges: a theorem of calculus, with a reminder of its proof below). In the same way for every we have , that is, , so the numbers decrease and are bounded below by , so the limit exists.
- The limits are equal: the width is halved at every iteration, so , and therefore , that is, . Write . Since increases to and decreases to , for every .
- is a root: is continuous at , so and , and therefore . By Claim 1 every term of this sequence is at most 0, so the limit is too: . Only satisfies this. If the ends are not roots, so lies strictly between and .
Why does now mean a root?
In the monthly-payments example was the interest rate (the letter for “rate”).
Here is a root of . The two meet in the example because the root of that is the interest rate we were looking for; in a general proof 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 for every (the numbers do not decrease) and there is a number with for every .
Then the limit exists, and it equals , the supremum of the set of all the numbers .
Proof. The set is not empty and is bounded above by , so by completeness it has a supremum .
Let . The number is smaller than the least upper bound, so it is not an upper bound of :
there is an index with . The numbers do not decrease, so for every
and therefore for every . This is the definition of .
Conclusion, and the decreasing case. A non-decreasing sequence that is bounded above converges to its supremum.
If instead for every and (bounded below), apply the claim to the numbers :
they do not decrease and are bounded above by , so they converge to their supremum, and converges to the infimum of .
The brackets of the monthly-payments equation, for :
each is half of the one before, all contain , and their width tends to 0.
Claim 3 (the error and the number of iterations). Let be any root in (in particular the of Claim 2).
The approximation is an end of , so the distance between them is at most the width of the bracket:
We stop at the first iteration with , that is, , and taking logarithms ; the smallest such integer is .
For the monthly payments , while : iterations, as computed above.
The claim covers every root inside the bracket; other roots of 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 bisectdescribes 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 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.bisecthalves 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 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 are.
The error itself need not shrink at every step: in the table below is closer to the root than .
In the table below the last column is the ratio of consecutive errors, .
It jumps: 7.04 means that the error grew at that step, because bisection guarantees the bound , 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 shows the approximation , and the new bracket , and row 0 is the starting bracket.
In the column , negative means that a debt remains (the rate is too high) and positive that is too low.
The true monthly rate is 2.793%, the rate at which :
| 0 | – | – | 0 | 0.05 | – |
| 1 | 0.025 | 11.79 | 0.025 | 0.05 | – |
| 2 | 0.0375 | −37.28 | 0.025 | 0.0375 | 3.27 |
| 3 | 0.03125 | −13.14 | 0.025 | 0.03125 | 0.347 |
| 4 | 0.028125 | −0.7759 | 0.025 | 0.028125 | 0.0586 |
| 5 | 0.0265625 | 5.481 | 0.0265625 | 0.028125 | 7.04 |
| 6 | 0.02734375 | 2.346 | 0.02734375 | 0.028125 | 0.429 |
| 7 | 0.027734375 | 0.7835 | 0.027734375 | 0.028125 | 0.334 |
| 8 | 0.0279296875 | 0.003425 | 0.0279296875 | 0.028125 | 0.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 on , whose root is . There are two ways:
build a table of values (TABLE) and look in it for the change of sign, or use CALC to evaluate 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 , and the residual .
We stop at the first iteration where and ; for the monthly rate, is 0.0001 percentage points.
The explorer uses this combined rule (both the step and the residual must meet their thresholds), so it takes more steps than a stopping condition on alone:
for it shows 27 rows against the 16 steps of the condition on (about 11 more).
Here is very steep: its slope near the root is about , which means that near the root a small change in changes by about (a local approximation):
a deviation of in changes by about .
The residual is about 3992 times the error , so to get below we must come about 3992 times closer to the root than requires:
the condition on 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.
| slope | ||
|---|---|---|
| (the root) | ||
Drag the point along the curve of the monthly payments and watch the slope of the tangent. The steepness is per unit of , not the angle on the screen: the curve looks gentle only because spans 0.05 while spans 220.
In the highlighted row of the table above, is closer to the root than , and even :
is smaller than , and , and its distance from the root, , is smaller than , and .
These are two thresholds in two different units: bounds the error in the unknown (here a rate), 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 are strong signs of accuracy, not a proof.
The answer to the rate question. With the tolerance of the explorer, , bisection stops at iteration with .
The monthly rate the shop charges is , and it is at most away from .
By the condition on alone 23 iterations would be enough (), but at iteration 23 the residual is still , 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 on . Here and : opposite signs, so the bracket looks fine.
Predict: does have a root in ? What will bisection do, and what will look like at the end?
Solution
A fraction is never , so there is no root. The sign changes because is undefined at :
it jumps from very large negative to very large positive values there.
Bisection does not notice, and the bracket shrinks toward :
| bracket kept | width | |||
|---|---|---|---|---|
| 1 | 0.5 | 5 | 0.5 | |
| 2 | 0.25 | −20 | 0.25 | |
| 3 | 0.375 | 13.33 | 0.125 | |
| 4 | 0.3125 | 80 | 0.0625 | |
| 5 | 0.28125 | −53.33 | 0.03125 | |
| 6 | 0.296875 | −320 | 0.015625 | |
| 7 | 0.3046875 | 213.33 | 0.0078125 |
The width halves every time, as always, yet 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, 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 .
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 are.
Continue learning
Regula falsi uses the size of the values of 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.