SigmaField
Mathematical Foundations for Quant Finance
0 of 256 lessons
      1. 1.3.1 Polynomials and their rootsConcept · 25 min
      2. 1.3.2 Bisection: finding a root by halvingConcept · 25 min
      3. 1.3.3 Newton's method, a first lookConcept · 25 min
      4. 1.3.4 Practice set: rootsPractice set · 20 min
      5. 1.3.5 Case study: IRR and yield to maturityCase study · 35 min

Level 1, Precalculus 1.3 Equations and roots Lesson 2 of 5

ConceptLesson 1.3.2 in Level 1, Precalculus. About 25 minutes.

Bisection: finding a root by halving

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 xx-axis, in the units of xx.

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

f continuous on [a,b]and f(a)⋅f(b)<0means a root lies between a and b\begin{gathered} f \text{ continuous on } [a, b] \\ \text{and } f(a) \cdot f(b) < 0 \\ \text{means a root lies between } a \text{ and } b \end{gathered}

A continuous function that changes sign must cross zero in between. Both conditions matter: take either away and the guarantee is gone.

An interval [a,b][a, b] with a sign change is a bracket. For f(x)=x2−2f(x) = x^2 - 2, f(1)=−1f(1) = -1 and f(2)=2f(2) = 2, so [1,2][1, 2] is a bracket. It holds the root 2\sqrt{2}.

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

m=a+b2m = \frac{a + b}{2}

Work out f(m)f(m). If f(a)f(a) and f(m)f(m) have opposite signs, the new bracket is [a,m][a, m]; otherwise it is [m,b][m, b]. If f(m)=0f(m) = 0, then mm is a root and you stop.

Here are six steps for x2−2x^2 - 2 on [1,2][1, 2].

stepaabbmmf(m)f(m)
1121.50.25
211.51.25−0.4375-0.4375
31.251.51.375−0.109375-0.109375
41.3751.51.43750.066406
51.3751.43751.40625−0.022461-0.022461
61.406251.43751.4218750.021729

At step 1, f(1.5)f(1.5) is positive, like f(2)f(2), so the sign change is in [1,1.5][1, 1.5]. At step 2, f(1.25)f(1.25) is negative, like f(1)f(1), so the root is in [1.25,1.5][1.25, 1.5]. After six steps the bracket is [1.40625,1.421875][1.40625, 1.421875], of width 0.015625, and it holds 2≈1.41421\sqrt{2} \approx 1.41421.

The bracket for x2−2x^2 - 2 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 x3−x−1x^3 - x - 1, 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.

0.511.522.5−2024
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 h(x)=3−x2h(x) = 3 - x^2 on [1,2][1, 2]: h(1)=2h(1) = 2 is positive and h(2)=−1h(2) = -1 is negative. The graph falls through zero at 3≈1.73205\sqrt{3} \approx 1.73205.

stepaabbmmh(m)h(m)
1121.50.75
21.521.75−0.0625-0.0625
31.51.751.6250.359375
41.6251.751.68750.152344

At 1.5 the value is positive, like h(1)h(1), so the new bracket is [1.5,2][1.5, 2]. For a rising function a positive value would have kept the left half instead. So compare the sign of f(m)f(m) with the sign at aa; its sign alone does not say which half to keep.

Several roots in one bracket

The cubic p(x)=(x−1)(x−2)(x−3)p(x) = (x - 1)(x - 2)(x - 3) from lesson 1.3.1 has three roots in [0,4][0, 4]. The first midpoint is 2, and p(2)=0p(2) = 0, so bisection stops at once with the root 2. On [0,3.5][0, 3.5] it finds 1 instead, and on [0.5,4][0.5, 4] 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 nn steps a bracket of width b−ab - a has width b−a2n\frac{b - a}{2^n}. The root is inside it, so the midpoint is within half that width of the root.

steps nn123102030
width, from 10.50.250.1250.0009770.000000950.00000000093

Every 10 steps divide the width by 210=10242^{10} = 1024, which is about 1000. So each 10 halvings buy about three more correct decimal places.

Rule: Steps needed

b−a2n≤tolexactly when2n≥b−atol\begin{gathered} \frac{b - a}{2^n} \le \text{tol} \\ \text{exactly when} \quad 2^n \ge \frac{b - a}{\text{tol}} \end{gathered}

Here tol is the width you want to reach. Find the smallest whole number nn with 2n≥b−atol2^n \ge \frac{b - a}{\text{tol}}. With logarithms (lesson 1.4.3) this is n≥log⁡2b−atoln \ge \log_2 \frac{b - a}{\text{tol}}, rounded up.

From width 1 to 10−610^{-6}, you need 2n≥1,000,0002^n \ge 1{,}000{,}000. 219=524,2882^{19} = 524{,}288 is too small and 220=1,048,5762^{20} = 1{,}048{,}576 is enough, so it takes 20 steps. Reaching 10−810^{-8} takes 27 steps, and 10−1010^{-10} takes 34.

The count depends only on the bracket and the accuracy, never on ff. 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 2\sqrt{2} and 3\sqrt{3} in 20 steps each, to 10−610^{-6}. It finds the root of x3−x−1x^3 - x - 1, 1.32471796, in 27 steps, to 10−810^{-8}.

⌘/Ctrl + Enter runs the cell

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. x2+1x^2 + 1 is 2 at both −1-1 and 1, and it has no root. But equal signs can also hide roots: p(0.5)p(0.5) and p(2.5)p(2.5) are both negative, with the roots 1 and 2 between them.
  • A double root. (x−1)2(x - 1)^2 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 x−2x - 2 for x<1x < 1 and x+1x + 1 for x≥1x \ge 1. It changes sign on [0,2][0, 2], from −2-2 to 3, yet it has no root. It leaps from just below −1-1 to 2 at x=1x = 1, 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 f(a)f(a) and f(b)f(b) have opposite signs. After finishing, check that ff is close to 0 at the answer.

Try it

The cell runs bisect on the jump. It returns about 1, but ff there is about −1-1, far from 0: the second check catches it.

A blow-up fools it the same way. On [−1,2][-1, 2], 1x\frac{1}{x} changes sign, and bisection homes in on 0, where 1x\frac{1}{x} is over 8 million.

⌘/Ctrl + Enter runs the cell

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 2\sqrt{2} it takes 7 steps to reach its default accuracy, about 10−1210^{-12}. Bisection took 20 steps for 10−610^{-6}.

The second call asks for the double root of (x−1)2(x - 1)^2 on [0,2][0, 2], and brentq refuses.

⌘/Ctrl + Enter runs the cell

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

  1. Check the bracket first. If f(a)⋅f(b)>0f(a) \cdot f(b) > 0, there is nothing to trap.
  2. Compare the sign of f(m)f(m) with the sign at aa. For a falling function, a positive f(m)f(m) means keep [m,b][m, b].
  3. Keep the half with the sign change, not the half whose midpoint value looks smaller.
  4. A sign change needs a continuous function. A jump or a blow-up changes sign without a root, so check ff at the answer.
  5. No sign change does not mean no root. Two roots, or a double root, hide behind equal signs.
  6. A bracket with several roots gives one of them, and the bracket decides which.
  7. The width halves each step. After nn steps it is b−a2n\frac{b - a}{2^n}, not b−a2n\frac{b - a}{2n}.

Bisection

0 of 11 correct

  1. 1.
  2. 2.
  3. 3.
  4. 4.
  5. 5.
  6. 6.
  7. 7.
  8. 8.
  9. 9.
  10. 10.
  11. 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, e is Euler's number, sqrt(x) is a square root.
  • sin(x), cos(x) and tan(x) take radians; write pi for π and sin(x)^2 for 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 [a,b][a, b] has a root between aa and bb. Such an interval is a bracket.
  • Bisection checks the midpoint and keeps the half where the sign changes. Compare with the sign at aa, so it works for rising and falling functions.
  • After nn steps the width is b−a2n\frac{b - a}{2^n}; reaching accuracy tol takes the smallest nn with 2n≥b−atol2^n \ge \frac{b - a}{\text{tol}}.
  • 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 ff at the answer after.
  • scipy.optimize.brentq(f, a, b) is the fast, safe version to use in practice.

Free further reading

Optional. This lesson covers what you need; these add more examples and practice. All are free to read.