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
- For and do three Newton steps. After each step, write the error . What happens to the number of correct digits from step to step?
- For start at and compute , and . What happens, and why does the tangent idea not help here?
- Start the same function at . What changes? What would you add to a program to notice the problem of question 2?
Hint for question 1
The Newton step for simplifies to .
Solutions
- (error ), (error ), (error ). The number of correct digits is , and : the ratio between consecutive steps is and then , so it nearly doubles at each step, because . This is quadratic convergence.
- and , so . Then and , so , and . The iteration jumps between and forever: the tangent at each point points to the other point, and the method never approaches the root.
- From we get and the iteration converges to the root . A program should use the guards of the course conventions: a cap on the number of steps, a check that 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 (the starting volatility of the implied volatility problem) it explodes, and on 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 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
- Do two Newton steps by hand for starting at , and compare with
- Newton’s method on some equation gave the errors in four successive steps. About how many correct digits are there after each step, and what does that say about the order of convergence?
- In the implied volatility problem we look for the volatility at which the model price of the option equals the market price, that is a root of . Why does Newton fail when it starts from ?
- Which method would you choose when (a) is the output of a long simulation, (b) is known and you have a good start, (c) an automated pipeline must always return an answer?
Hint for question 1
For the slope is , so .
Solutions
- and , which differs from by . One more step gives , error : close to the root the error is roughly squared each time.
- The number of correct digits is : 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.
- At the price barely depends on , so vega, the slope , is almost zero. The tangent is almost horizontal and crosses zero very far away.
- (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 and of 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 and compares with . 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 max_steps; abs(step) is ; return x, n gives back two values; math.sqrt(2) is ; 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 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 to see what happens when the slope is zero.
One more run: a tolerance below the gap. Beside the gap between stored binary64 numbers is (see a tolerance cannot be smaller than the gap), so a tolerance of 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 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 differ by 0 or by at least one gap, so is below only when it is 0.
Newton’s step is with , and at the two stored numbers beside we have , never 0, so the step is never below .
Newton’s method runs all 50 steps and returns (1.414213562373095, 50).
At the step is , about 0.7 of a gap. The exact result of subtracting it lies nearer to the stored number one gap below than to , so it is rounded there.
From that number 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 . 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.