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 3 of 5

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

Newton's method, a first look

Follow the tangent line

Bisection uses only the signs of ff. Newton's method also uses how steep the graph is, and when it works it is far faster.

Close up, a smooth curve looks almost straight. The straight line that touches the curve at a point, running in the same direction there, is its tangent line. A line is easy to solve, so Newton's method solves the tangent instead of the curve.

Guess a root, x0x_0. Follow the tangent at x0x_0 down to where it crosses the axis, and call that point x1x_1. Repeat from x1x_1 to get x2x_2, and so on: xnx_n is the guess after nn steps.

The slope of a curve, borrowed for now

A line has the same slope everywhere. A curve is steeper in some places than in others, so its slope depends on the point. Write f′(x)f'(x), read "f prime of x", for the slope of the tangent at xx.

Lesson 2.2.1 shows where the slope of a curve comes from. For now, borrow three facts. The slope of x2x^2 at xx is 2x2x, and the slope of x3x^3 is 3x23x^2.

Adding a term mxmx adds mm to the slope. Adding a constant changes nothing: it only shifts the graph up or down (lesson 1.1.2).

f(x)f(x)x2−2x^2 - 2x3−10x^3 - 10x3−x−1x^3 - x - 1x3−2x+2x^3 - 2x + 2
slope f′(x)f'(x)2x2x3x23x^23x2−13x^2 - 13x2−23x^2 - 2

Try it

You can check a borrowed slope with rise over run (lesson 1.2.1) across a tiny step. The cell takes the chord from xx to x+0.000001x + 0.000001.

For x2−2x^2 - 2 at 1.5 the chord slope is 3.0, and 2x2x gives 3.0. For x3−x−1x^3 - x - 1 at 1.5 both give 5.75. round(…, 4) hides the last digits, which differ because the step is not zero.

⌘/Ctrl + Enter runs the cell

The Newton step

The tangent at xnx_n passes through the point (xn,f(xn))(x_n, f(x_n)) with slope f′(xn)f'(x_n). By the point-slope form of lesson 1.2.1, it is the line

y=f(xn)+f′(xn)(x−xn)y = f(x_n) + f'(x_n)(x - x_n)

The next guess is where this line crosses the axis. Set y=0y = 0 and solve for xx, as in lesson 0.3.2: x−xn=−f(xn)f′(xn)x - x_n = -\frac{f(x_n)}{f'(x_n)}.

Rule: Newton's method

xn+1=xn−f(xn)f′(xn)x_{n+1} = x_n - \frac{f(x_n)}{f'(x_n)}

Step sideways by the height f(xn)f(x_n) divided by the slope f′(xn)f'(x_n). The slope must not be 0. If xnx_n is already a root, then f(xn)=0f(x_n) = 0 and the guess stays where it is.

A first example: 2\sqrt{2}

Take f(x)=x2−2f(x) = x^2 - 2, with slope 2x2x, and start at x0=2x_0 = 2. There f(2)=2f(2) = 2 and the slope is 4, so x1=2−24=1.5x_1 = 2 - \frac{2}{4} = 1.5. As a line, the tangent is y=2+4(x−2)=4x−6y = 2 + 4(x - 2) = 4x - 6, which crosses the axis at 64=1.5\frac{6}{4} = 1.5.

Two Newton steps for x2−2x^2 - 2 from x0=2x_0 = 2. The tangent at 2 lands on 1.5; the tangent at 1.5, enlarged, lands on 1.41667, almost on 2\sqrt{2}.

Here are the guesses, with each one's error, its distance from 2\sqrt{2}.

step nnguess xnx_nerror
020.586
11.50.0858
21.41666666666666670.00245
31.41421568627450990.0000021
41.41421356237468990.0000000000016
51.41421356237309510

Five steps give 2\sqrt{2} to every digit a computer stores. Bisection needed 20 steps for six digits.

Algebra gives the step a neat form. For x2−2x^2 - 2 it simplifies to an average:

x−x2−22x=x−x2+1x=12(x+2x)\begin{aligned} x - \frac{x^2 - 2}{2x} &= x - \frac{x}{2} + \frac{1}{x} \\ &= \frac{1}{2}\left(x + \frac{2}{x}\right) \end{aligned}

Each new guess is the average of xx and 2x\frac{2}{x}. If xx is too big then 2x\frac{2}{x} is too small, so their average lands in between. This rule for square roots is often called the Babylonian method.

Press Next step to follow the tangents yourself. Then pick x3−x−1x^3 - x - 1, the cubic from lesson 1.3.2, and count the steps: bisection needed 27.

Explore

Press Next step. Each step follows the tangent line at the current guess down to where it crosses zero, which is the next guess.

0.511.522.5−2024
Steps
0
Current guess
2
f there
2
Distance from the root 1.414214
0.585786437627

A second example: the cubic

Take f(x)=x3−x−1f(x) = x^3 - x - 1, with slope 3x2−13x^2 - 1, from x0=1.5x_0 = 1.5. The first step is 1.5−0.8755.75=1.34781.5 - \frac{0.875}{5.75} = 1.3478, to four places.

step nnguess xnx_nerror
11.34782608695652170.0231
21.3252003989509070.000482
31.32471817399905370.00000022
41.32471795724478980.000000000000044
51.3247179572447460

Four steps reach an error below 10−1310^{-13}. Bisection took 27 steps to reach 10−810^{-8}.

Why it is so fast

Look at the errors for 2\sqrt{2}: 0.0858, 0.00245, 0.0000021, 0.0000000000016. Each is about a third of the square of the one before. Squaring a small number doubles its zeros after the point, as 0.0012=0.0000010.001^2 = 0.000001 shows.

So near a root, the number of correct digits roughly doubles each step. Bisection gains the same small amount every step, about three decimal places per 10 steps. Newton's method gains more and more, but only once it is close: far from the root its first steps can go anywhere.

A program needs a rule for when to stop. Stop when a step moves the guess by less than a tolerance. Also give up after a fixed number of steps, so a failure cannot run for ever.

Try it

newton follows that rule. It returns the root and the number of steps, counting the last tiny step that shows the guess has stopped moving.

It finds 2\sqrt{2} in 6 steps, the cubic's root in 5, and the cube root of 10, 2.15443469, in 5 from x0=2x_0 = 2.

⌘/Ctrl + Enter runs the cell

When Newton's method fails

Newton's speed comes with no guarantee. Each failure below is one you will meet in practice.

  • A flat tangent. If f′(xn)=0f'(x_n) = 0, the tangent never meets the axis and the step divides by zero: x2−2x^2 - 2 from x0=0x_0 = 0 fails at once. A nearly flat tangent is almost as bad. From 0.1 the first step lands at 10.05, and it takes eight steps to get back to 2\sqrt{2}.
  • A cycle. Take x3−2x+2x^3 - 2x + 2, with slope 3x2−23x^2 - 2, from x0=0x_0 = 0. f(0)=2f(0) = 2 and f′(0)=−2f'(0) = -2 give x1=1x_1 = 1; then f(1)=1f(1) = 1 and f′(1)=1f'(1) = 1 give x2=0x_2 = 0, the start again. The guesses bounce between 0 and 1 for ever, while the only root is near −1.7693-1.7693.
  • A double root. For (x−1)2=x2−2x+1(x - 1)^2 = x^2 - 2x + 1, with slope 2x−22x - 2, the step simplifies to x+12\frac{x + 1}{2}. From 3 the guesses are 2, 1.5, 1.25, 1.125: the error only halves each step, no faster than bisection.
  • No root. x2+1x^2 + 1 has no real root, so the guesses can never settle. From 0.5 they go −0.75-0.75, 0.29, −1.57-1.57, −0.47-0.47, 0.84, jumping about for ever.
  • The wrong root. p(x)=(x−1)(x−2)(x−3)p(x) = (x - 1)(x - 2)(x - 3) has slope 3x2−12x+113x^2 - 12x + 11. From 1.5, halfway between the roots 1 and 2, one step lands on 3. The start decides which root you get, and it need not be the nearest.

To watch the cycle, pick x3−2x+2x^3 - 2x + 2 in the explorer above and press Next step a few times.

Using it safely

Careful code guards Newton's method. It caps the number of steps and checks ff at the answer. It can also keep a bracket from lesson 1.3.2, taking a bisection step whenever a Newton step would leave it.

SciPy's brentq (lesson 1.3.2) mixes safe and fast steps in a similar way. SciPy's newton(f, x0, fprime=slope) runs plain Newton's method, and raises RuntimeError when it has not converged after 50 steps. Without fprime, it estimates each slope from a chord through its last two guesses, so it needs no slope at all.

Try it

The first two lines find 2\sqrt{2}, with the slope and without it. The last digit differs by rounding.

The third call is the cycle. After 50 steps newton is still at 0, and it raises RuntimeError.

⌘/Ctrl + Enter runs the cell

Case study 1.3.5 uses newton without a slope to find interest rates. Lesson 2.4.3 works out the slope of any function with calculus and runs Newton's method again. Case study 2.4.6 uses it for an option's implied volatility.

Common mistakes

  1. Subtract the step: xn+1=xn−f(xn)f′(xn)x_{n+1} = x_n - \frac{f(x_n)}{f'(x_n)}. Adding it sends the guess the wrong way.
  2. Divide the height by the slope, f(xn)f′(xn)\frac{f(x_n)}{f'(x_n)}, not the slope by the height.
  3. Use the slope at the current guess, f′(xn)f'(x_n), not the slope at the start.
  4. A zero slope divides by zero. Start somewhere else.
  5. It is fast only near the root. Far away the guesses can jump, cycle or wander, so cap the steps and check ff at the answer.
  6. A double root slows it down to one halving of the error per step.
  7. The start picks the root, and not always the nearest one. Nothing keeps the guesses inside a bracket unless you add it.

Newton's method

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

  • Newton's method follows the tangent line at the current guess down to the axis: xn+1=xn−f(xn)f′(xn)x_{n+1} = x_n - \frac{f(x_n)}{f'(x_n)}, where f′(x)f'(x) is the slope at xx.
  • The step comes from the point-slope form y=f(xn)+f′(xn)(x−xn)y = f(x_n) + f'(x_n)(x - x_n), set to 0.
  • Near a root the correct digits roughly double each step, far faster than bisection.
  • It can fail: a flat tangent, a cycle, a double root (slow), no root, or a jump to an unexpected root. Cap the steps, check ff at the answer, and fall back on a bracket.
  • In SciPy: newton(f, x0, fprime=...) for Newton's method (without fprime it uses chord slopes), and brentq(f, a, b) for a safe bracketed search.

Free further reading

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