When there is no formula
Quadratics have a formula for their roots (lesson 1.2.2), and cubics and quartics have long, awkward ones. From degree 5 on, no general formula built from arithmetic and roots exists. Most equations that mix powers with other functions have no formula either.
A numerical method finds a root without a formula. It starts from a rough answer and improves it step by step, until the answer is as accurate as you need. The accuracy is a distance along the -axis, in the units of .
The guarantee: a sign change traps a root
A function is continuous when its graph has no gaps or jumps: you can draw it without lifting your pen. Polynomials are continuous everywhere. Lesson 2.1.2 makes the idea exact.
Rule: A sign change traps a root
A continuous function that changes sign must cross zero in between. Both conditions matter: take either away and the guarantee is gone.
An interval with a sign change is a bracket. For , and , so is a bracket. It holds the root .
Bisection: halve the bracket
Bisection shrinks a bracket while keeping the root inside. Cut the bracket at its midpoint and check the sign there. One of the two halves still has a sign change, so it is a smaller bracket.
Rule: One bisection step
Work out . If and have opposite signs, the new bracket is ; otherwise it is . If , then is a root and you stop.
Here are six steps for on .
| step | ||||
|---|---|---|---|---|
| 1 | 1 | 2 | 1.5 | 0.25 |
| 2 | 1 | 1.5 | 1.25 | |
| 3 | 1.25 | 1.5 | 1.375 | |
| 4 | 1.375 | 1.5 | 1.4375 | 0.066406 |
| 5 | 1.375 | 1.4375 | 1.40625 | |
| 6 | 1.40625 | 1.4375 | 1.421875 | 0.021729 |
At step 1, is positive, like , so the sign change is in . At step 2, is negative, like , so the root is in . After six steps the bracket is , of width 0.015625, and it holds .
The bracket for at the start and after four steps. Each step keeps the half with the sign change, so the root never leaves the bracket.
Press Next step to run the table yourself and watch the shaded bracket halve. Then pick , whose root is near 1.3247, and watch the bracket close in on it.
Explore
Press Next step. Each step checks the midpoint and keeps the half where the sign changes, so the bracket around the root halves.
- Steps
- 0
- Bracket
- [1, 2], width 1
- f there
- 0.25
- Distance from the root 1.414214
- 0.085786437627
A falling function
The step only compares signs, so it works whether the graph rises or falls. Take on : is positive and is negative. The graph falls through zero at .
| step | ||||
|---|---|---|---|---|
| 1 | 1 | 2 | 1.5 | 0.75 |
| 2 | 1.5 | 2 | 1.75 | |
| 3 | 1.5 | 1.75 | 1.625 | 0.359375 |
| 4 | 1.625 | 1.75 | 1.6875 | 0.152344 |
At 1.5 the value is positive, like , so the new bracket is . For a rising function a positive value would have kept the left half instead. So compare the sign of with the sign at ; its sign alone does not say which half to keep.
Several roots in one bracket
The cubic from lesson 1.3.1 has three roots in . The first midpoint is 2, and , so bisection stops at once with the root 2. On it finds 1 instead, and on it finds 3: the bracket decides which root you get.
How close, and how many steps
Each step halves the width of the bracket, whatever the function. After steps a bracket of width has width . The root is inside it, so the midpoint is within half that width of the root.
| steps | 1 | 2 | 3 | 10 | 20 | 30 |
|---|---|---|---|---|---|---|
| width, from 1 | 0.5 | 0.25 | 0.125 | 0.000977 | 0.00000095 | 0.00000000093 |
Every 10 steps divide the width by , which is about 1000. So each 10 halvings buy about three more correct decimal places.
Rule: Steps needed
Here tol is the width you want to reach. Find the smallest whole number with . With logarithms (lesson 1.4.3) this is , rounded up.
From width 1 to , you need . is too small and is enough, so it takes 20 steps. Reaching takes 27 steps, and takes 34.
The count depends only on the bracket and the accuracy, never on . That is bisection's strength: it is slow, but certain once the root is bracketed.
Try it
bisect is the step above in a loop. It stops when the bracket is narrower than tol, and returns the midpoint with the number of steps.
It finds and in 20 steps each, to . It finds the root of , 1.32471796, in 27 steps, to .
Where bisection fails
The guarantee needs a sign change and a continuous function. Take either away, and bisection can miss a root or report one that is not there.
- No sign change, so no bracket. is 2 at both and 1, and it has no root. But equal signs can also hide roots: and are both negative, with the roots 1 and 2 between them.
- A double root. is 1 at both 0 and 2. It touches zero at 1 without changing sign (lesson 1.3.1), so no bracket around 1 exists, however small. Bisection cannot find a double root at all.
- A jump. Take the piecewise function (lesson 1.1.2) that is for and for . It changes sign on , from to 3, yet it has no root. It leaps from just below to 2 at , and bisection closes in on the jump.
Three ways a bracket misleads. Equal signs at both ends can hide two roots or a double root, and a jump changes sign with no root.
So bisection needs two checks. Before starting, make sure and have opposite signs. After finishing, check that is close to 0 at the answer.
Try it
The cell runs bisect on the jump. It returns about 1, but there is about , far from 0: the second check catches it.
A blow-up fools it the same way. On , changes sign, and bisection homes in on 0, where is over 8 million.
In practice: brentq
SciPy's brentq(f, a, b) takes the same bracket. It mixes bisection steps with faster guesses from lines and curves drawn through recent points, so it is both safe and quick. Without a sign change it refuses to start and raises ValueError.
Try it
With full_output=True it also returns a report. For it takes 7 steps to reach its default accuracy, about . Bisection took 20 steps for .
The second call asks for the double root of on , and brentq refuses.
Lesson 1.3.3 meets a faster method that also uses the steepness of the graph. Case study 1.3.5 brackets interest rates with brentq.
Common mistakes
- Check the bracket first. If , there is nothing to trap.
- Compare the sign of with the sign at . For a falling function, a positive means keep .
- Keep the half with the sign change, not the half whose midpoint value looks smaller.
- A sign change needs a continuous function. A jump or a blow-up changes sign without a root, so check at the answer.
- No sign change does not mean no root. Two roots, or a double root, hide behind equal signs.
- A bracket with several roots gives one of them, and the bracket decides which.
- The width halves each step. After steps it is , not .
Bisection
0 of 11 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
How to type answers
- Powers with
^:x^7,2^(1/3),a^-2. - Multiply with
*or by writing side by side:3x^2,2(x+1). ln(x)is the natural log,log(x, 2)is log base 2,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for sin²x.- Your first answer to each question is saved: it counts towards your score and brings the question back for review.
- Exact answers like
ln(20)/ln(3)are best. Decimals need about 4 significant figures.
Summary card
- A continuous function that changes sign on has a root between and . Such an interval is a bracket.
- Bisection checks the midpoint and keeps the half where the sign changes. Compare with the sign at , so it works for rising and falling functions.
- After steps the width is ; reaching accuracy tol takes the smallest with .
- It fails without a sign change (no root, two roots, a double root) and without continuity (a jump or blow-up). Check the bracket before and at the answer after.
scipy.optimize.brentq(f, a, b)is the fast, safe version to use in practice.