Exercise

Practice: Newton and the secant method

Newton steps by hand and a cycle where the tangent does not help, the usual mistakes, transfer questions and a Python implementation: practice for the lecture on methods that use the slope.

Last updated:


By the end of the practice session you can:

  • do Newton steps by hand and see the digits double;
  • recognize a cycle and say what a program should check;
  • avoid the usual mistakes of Newton and secant;
  • implement Newton and the secant method in Python and compare their cost.

This practice follows the lecture Using the Slope: read it or attend it first.

By hand: Newton steps, and a cycle

  1. For f(x)=x2−2f(x)=x^2-2 and x0=1x_0=1 do three Newton steps. After each step, write the error ∣xn−2∣|x_n-\sqrt2|⁠‏. What happens to the number of correct digits from step to step?
  2. For f(x)=x3−2x+2f(x)=x^3-2x+2 start at x0=0x_0=0 and compute x1x_1⁠‏, x2x_2 and x3x_3⁠‏. What happens, and why does the tangent idea not help here?
  3. Start the same function at x0=−2x_0=-2⁠‏. What changes? What would you add to a program to notice the problem of question 2?
Hint for question 1

The Newton step for f(x)=x2−2f(x)=x^2-2 simplifies to xn+1=12(xn+2xn)x_{n+1}=\dfrac12\left(x_n+\dfrac{2}{x_n}\right)⁠‏.

Solutions
  1. x1=1.5x_1=1.5 (error 0.0860.086⁠)‏, x2≈1.41667x_2\approx1.41667 (error 0.00250.0025⁠)‏, x3≈1.41422x_3\approx1.41422 (error 2.1×10−62.1\times10^{-6}⁠)‏. The number of correct digits is 1.071.07⁠, 2.612.61 and 5.675.67⁠: the ratio between consecutive steps is 2.42.4 and then 2.22.2⁠, so it nearly doubles at each step, because ∣en+1∣≈C ∣en∣2|e_{n+1}|\approx C\,|e_n|^2⁠. This is quadratic convergence.
  2. f(0)=2f(0)=2 and f′(0)=−2f'(0)=-2⁠‏, so x1=0−2−2=1x_1=0-\frac{2}{-2}=1⁠‏. Then f(1)=1f(1)=1 and f′(1)=1f'(1)=1⁠‏, so x2=1−1=0x_2=1-1=0⁠‏, and x3=1x_3=1⁠‏. The iteration jumps between 00 and 11 forever: the tangent at each point points to the other point, and the method never approaches the root.
  3. From x0=−2x_0=-2 we get x1=−1.8x_1=-1.8 and the iteration converges to the root ≈−1.7693\approx-1.7693⁠‏. A program should use the guards of the course conventions: a cap on the number of steps, a check that ∣f(xn)∣|f(x_n)| decreases, and a fall-back to bisection on a bracket.

Common mistakes

“Newton always converges, and always faster.”

Why this is wrong

It converges fast only near the root and when the slope is not close to zero.
From σ0=0.005\sigma_0=0.005 (the starting volatility of the implied volatility problem) it explodes, and on x3−2x+2x^3-2x+2 from 0 it cycles for ever.
Bisection is slower but never fails inside a bracket.

“Stop when two consecutive estimates are close.”

Why this is incomplete

Close estimates can mean convergence, but also a very flat function or a slowly crawling method.
As in the course conventions, check both the step size and ∣f∣|f| against the tolerance.

“Quadratic convergence means fewer evaluations of everything.”

Why this is wrong

It means fewer steps. Each Newton step also needs the derivative; when the derivative is expensive, the secant method with its cheaper steps can finish first.

Transfer practice

  1. Do two Newton steps by hand for f(x)=x2−5f(x)=x^2-5 starting at x0=3x_0=3⁠, and compare with 5=2.2360680…\sqrt5=2.2360680\ldots
  2. Newton’s method on some equation gave the errors 3×10−2, 9×10−4, 8×10−7, 7×10−133\times10^{-2},\ 9\times10^{-4},\ 8\times10^{-7},\ 7\times10^{-13} in four successive steps. About how many correct digits are there after each step, and what does that say about the order of convergence?
  3. In the implied volatility problem we look for the volatility σ\sigma at which the model price of the option equals the market price, that is a root of f(σ)=model price(σ)−market pricef(\sigma)=\text{model price}(\sigma)-\text{market price}⁠. Why does Newton fail when it starts from σ0=0.005\sigma_0=0.005⁠?
  4. Which method would you choose when (a) ff is the output of a long simulation, (b) f′f' is known and you have a good start, (c) an automated pipeline must always return an answer?
Hint for question 1

For f(x)=x2−5f(x)=x^2-5 the slope is f′(x)=2xf'(x)=2x⁠, so xn+1=xn−xn2−52xn=xn2+52xnx_{n+1}=x_n-\dfrac{x_n^2-5}{2x_n}=\dfrac{x_n}{2}+\dfrac5{2x_n}⁠.

Solutions
  1. x1=32+56=73=2.3333333x_1=\tfrac32+\tfrac56=\tfrac73=2.3333333 and x2=76+1514=4721=2.2380952x_2=\tfrac76+\tfrac{15}{14}=\tfrac{47}{21}=2.2380952⁠, which differs from 5\sqrt5 by 2.03×10−32.03\times10^{-3}⁠. One more step gives 2.23606892.2360689⁠, error 9.2×10−79.2\times10^{-7}⁠: close to the root the error is roughly squared each time.
  2. The number of correct digits is −log⁡10E-\log_{10}E⁠: 1.5, 3.0, 6.1 and 12.2 (rounded). Each number is about twice the one before, so each error is roughly the square of the previous one, which is what order 2 means.
  3. At σ=0.005\sigma=0.005 the price barely depends on σ\sigma⁠, so vega, the slope f′(σ)f'(\sigma)⁠, is almost zero. The tangent is almost horizontal and crosses zero very far away.
  4. (a) The secant method (no derivative), preferably started inside a bracket; (b) Newton–Raphson; (c) a bracketing method, or a combination of a bracket with a fast method, because only a bracket gives a guarantee.

Root finding, cell by cell

Newton–Raphson and the secant method on new equations that a calculator solves only with SOLVE (the calculator’s command for solving an equation).
Check the conditions first (the value of ff and of f′f' at the start, and whether the first step stays near the root), then fill the iteration table one cell at a time and read the order of convergence from it.
Up to three calculators can sit beside the table.

The workout needs JavaScript. Without it, practise with the example above: Newton on x² − 2 from x₀ = 1 gives x₁ = 1.5, x₂ = 1.41667, x₃ = 1.41422.

Implement it yourself

Complete the two functions below: Newton’s step and the secant step.
The program runs both on f(x)=x2−2f(x)=x^2-2 and compares with 2\sqrt2⁠. Press Run; if a line says FAIL, find out why before opening the solution.
Afterwards, compare how many steps each method took, and why the secant method needed one more.

Python in this task. It asks you to write code: two lines inside functions that are already written.
def defines a function; for n in range(1, max_steps + 1) repeats with n=1,2,…,n=1,2,\dots, max_steps; abs(step) is ∣step∣|\text{step}|⁠; return x, n gives back two values; math.sqrt(2) is 2\sqrt2⁠; repr(x) writes the number x with all the digits needed to tell it from its neighbours.

import math

def f(x):
    return x * x - 2

def df(x):
    return 2 * x

def newton(f, df, x, tol=1e-12, max_steps=50):
    for n in range(1, max_steps + 1):
        # TODO: Newton's step, f(x) divided by f'(x)
        step = 0.0
        x = x - step
        if abs(step) <= tol:
            return x, n
    return x, max_steps

def secant(f, x0, x1, tol=1e-12, max_steps=50):
    for n in range(1, max_steps + 1):
        # TODO: the secant step, using only the values of f at
        # x0 and x1
        x2 = x1
        if abs(x2 - x1) <= tol:
            return x2, n
        x0, x1 = x1, x2
    return x1, max_steps

for name, (x, n) in [
    ("Newton from 1", newton(f, df, 1)),
    ("secant from 1, 2", secant(f, 1, 2))
]:
    print(
        "ok  " if abs(x - math.sqrt(2)) < 1e-10 else "FAIL",
        name, "x =", repr(x), "after", n, "steps"
    )
ok   Newton from 1 x = 1.414213562373095 after 6 steps
ok   secant from 1, 2 x = 1.4142135623730951 after 7 steps
Solution

In newton replace the TODO line by

        step = f(x) / df(x)

and in secant by

        x2 = x1 - f(x1) * (x1 - x0) / (f(x1) - f(x0))

Newton took 6 steps and the secant method 7: the secant method uses an estimate of the slope instead of the true slope, so its order is 1+52≈1.618\tfrac{1+\sqrt5}{2}\approx1.618 and not 2.
As a further step, count how many times each function is evaluated in total, as the lesson’s cost discussion suggests, and try starting Newton at x=0x=0 to see what happens when the slope is zero.

One more run: a tolerance below the gap. Beside 2\sqrt2 the gap between stored binary64 numbers is 2−52≈2.2×10−162^{-52}\approx2.2\times10^{-16} (see a tolerance cannot be smaller than the gap), so a tolerance of 10−1610^{-16} is below it.
In the code, Newton’s method tests abs(step) and the secant method tests abs(x2 - x1).
Work out which of the two can produce a value below 10−1610^{-16} here.
Then add these two lines at the end of the completed program, run it, and read the number of steps in each result:

print(newton(f, df, 1, 1e-16))
print(secant(f, 1, 2, 1e-16))
(1.414213562373095, 50)
(1.414213562373095, 9)
Solution

Only the secant method can. Two stored numbers beside 2\sqrt2 differ by 0 or by at least one gap, so ∣x2−x1∣|x_2-x_1| is below 10−1610^{-16} only when it is 0.
Newton’s step is f(x)/f′(x)f(x)/f'(x) with f′(x)≈2.83f'(x)\approx2.83⁠, and at the two stored numbers beside 2\sqrt2 we have f(x)=±4.4×10−16f(x)=\pm4.4\times10^{-16}⁠, never 0, so the step is never below 1.57×10−161.57\times10^{-16}⁠.

Newton’s method runs all 50 steps and returns (1.414213562373095, 50).
At x=1.4142135623730951x=1.4142135623730951 the step is 1.57×10−161.57\times10^{-16}⁠, about 0.7 of a gap. The exact result of subtracting it lies nearer to the stored number one gap below xx than to xx⁠, so it is rounded there.
From that number ff has the opposite sign and the step has the same size upward, which rounds back.
The iterate jumps between two neighbours, every step is larger than the tolerance, and only the cap on the steps stops the loop.

With the secant formula of the lesson, the secant method returns after 9 steps, (1.414213562373095, 9):
its last step lands on the same stored number, so ∣x2−x1∣=0|x_2-x_1|=0⁠. A tolerance below the gap can be met only by equal numbers, and the tolerance cannot make a method produce them.
This is why the course conventions ask for a cap on the number of steps.

Continue learning

Back to the lecture Using the Slope. The glossary has the terms:
glossary.