Lesson

Using the Slope: the Secant Method and Newton–Raphson

Finding a root faster than bisection: the secant method as regula falsi without a bracket, Newton–Raphson when the slope is known, convergence faster than linear, and when the slope misleads.

Last updated:


Warm-up: what do you already know?

Two points of the graph are given, (a,f(a))(a,f(a)) and (b,f(b))(b,f(b))⁠. Where does the line through them cross the xx-axis?

Solution

The line is y=f(b)+f(b)−f(a)b−a (x−b)y=f(b)+\dfrac{f(b)-f(a)}{b-a}\,(x-b)⁠. Setting y=0y=0 gives x=b−f(b) b−af(b)−f(a)x=b-f(b)\,\dfrac{b-a}{f(b)-f(a)}⁠. This is the point cc of regula falsi.

The secant method: regula falsi without a bracket

A shop offers a laptop for 1200 in cash, or in six monthly instalments of 220, the first one in a month.
The monthly interest rate rr hidden in the offer solves f(r)=∑t=16220(1+r)t−1200=0f(r)=\sum_{t=1}^{6}\dfrac{220}{(1+r)^t}-1200=0⁠.
We solved this equation on the bisection page and the regula falsi page:
for a tolerance of 10−1010^{-10} bisection needs 40 steps and regula falsi 11, and both need a bracket.
In regula falsi one end of the interval may also stay in place forever.
We now meet methods that are even faster.

In regula falsi, at each step we computed the zero of the line joining the two bracketing ends, and the new point came closer and closer to the true root.
To decide which end to replace by the new point, we checked the sign of the function.

Work it out: what happens if we drop the sign check, and instead of replacing an end according to the sign we always replace the older approximation by the new one, that is, we join the two latest points we computed by a line?
What is the next point xn+1x_{n+1}⁠?

Solution

The line through (xn−1,f(xn−1))(x_{n-1},f(x_{n-1})) and (xn,f(xn))(x_n,f(x_n)) crosses the axis at the point from the warm-up, with a=xn−1a=x_{n-1} and b=xnb=x_n⁠:
xn+1=xn−f(xn) xn−xn−1f(xn)−f(xn−1)x_{n+1}=x_n-f(x_n)\,\dfrac{x_n-x_{n-1}}{f(x_n)-f(x_{n-1})}⁠.

The point you found is the formula for cc in regula falsi, with the two latest points in place of the two ends.
This is the secant method.

What it adds to regula falsi: no sign check and no bracket, and it always keeps the two most recent points, so no endpoint stays fixed.
It needs only values of ff⁠. Its cost: it loses the guarantee of the bracket.
The starting points need not enclose the root, nothing forces the root to stay between them, so the method can diverge.
If f(xn)=f(xn−1)f(x_n)=f(x_{n-1}) the denominator is zero: the line is horizontal and never crosses the axis.
Its order of convergence is 1+52≈1.618\tfrac{1+\sqrt5}{2}\approx1.618 (the golden ratio), not linear like regula falsi.
We do the proof in the practice session.

Why x2−2x^2-2⁠? The diagonal of a square with side 1 has length 2\sqrt2⁠, an irrational number:
it has no representation as a fraction or as a finite decimal, so we cannot write it in full, only approach it.
The equation x2−2=0x^2-2=0 defines it, and a numerical method gives at each step a better approximation, to as many digits as we want.

Try it: with the secant method on f(x)=x2−2f(x)=x^2-2 from x0=2x_0=2 and x1=1.5x_1=1.5⁠, what is x2x_2⁠?

Solution

f(2)=2f(2)=2 and f(1.5)=0.25f(1.5)=0.25⁠, so x2=1.5−0.25⋅1.5−20.25−2=107≈1.42857x_2=1.5-0.25\cdot\dfrac{1.5-2}{0.25-2}=\dfrac{10}{7}\approx1.42857⁠, where the root is 2≈1.41421\sqrt2\approx1.41421 (an error of about 0.0140.014⁠).

Newton–Raphson: follow the tangent

The ff of the instalments has a derivative: a sum of six terms we can write down.
When there is a formula for f′f'⁠, we can stop approximating the slope from two points and use the slope itself:
instead of two points at every step we use one, which continues the idea of the secant method.
Instead of a secant line we get a tangent, the secant line when the two points merge.
The tangent to ff at xnx_n is y=f(xn)+f′(xn) (x−xn)y=f(x_n)+f'(x_n)\,(x-x_n)⁠.

Work it out: where does the tangent cross the xx-axis?

Solution

Set y=0y=0⁠: f(xn)+f′(xn) (x−xn)=0f(x_n)+f'(x_n)\,(x-x_n)=0⁠, so x=xn−f(xn)f′(xn)x=x_n-\dfrac{f(x_n)}{f'(x_n)}⁠, provided f′(xn)≠0f'(x_n)\neq0⁠. For a nearly straight curve this point is very close to the true root, and it is cheap:
one value and one slope.

Replace the curve by its tangent at the current point and jump to where the tangent crosses zero.
This is the Newton–Raphson method:

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

Where the formula comes from, step by step, in a short video of mine (in Hebrew):

What Newton adds to the secant method: the exact slope instead of an approximation of it, so there is no need for two starting points, and the method is faster.
Its cost: the derivative f′f'⁠, and a start near the root.

A simple example: f(x)=x2−2f(x)=x^2-2 with f′(x)=2xf'(x)=2x⁠, and the root is 2=1.41421356237…\sqrt2=1.41421356237\ldots⁠.
A Newton step is xn+1=12(xn+2xn)x_{n+1}=\tfrac12\left(x_n+\tfrac{2}{x_n}\right)⁠. From x0=1.5x_0=1.5⁠:

StepEstimateff at the estimateError
01.50.258.58×10−28.58\times10^{-2}
11.4166666666670.0069442.45×10−32.45\times10^{-3}
21.4142156862756.01×10−66.01\times10^{-6}2.12×10−62.12\times10^{-6}
31.4142135623754.5×10−124.5\times10^{-12}1.60×10−121.60\times10^{-12}

Three Newton steps give an error of 10−1210^{-12}⁠. By the explorer’s rule (threshold 10−1010^{-10}⁠) bisection on [0,2][0,2] needs 36 steps, the secant method 8 and Newton 4.

Work it out: Bisection cuts the error by a factor of 2 at each step.
How many steps does it take to gain one decimal digit, that is, to cut the error by a factor of 10?

Solution

After nn steps the error is smaller by a factor of 2n2^n⁠, and 2n≥102^n\ge10 when n≥log⁡210≈3.3n\ge\log_2 10\approx3.3⁠.
Four steps cut the error by 16 and three only by 8, so bisection gains one decimal digit every 3.3 steps on average.

We measure how accurate an estimate is by its number of correct digits:
for an absolute error EE it is −log⁡10E-\log_{10}E⁠, so an error of 10−410^{-4} has 4 correct digits after the decimal point.
Write en=xn−x∗e_n=x_n-x^* for the error at step nn⁠, the difference between the estimate and the root x∗x^*⁠.
In the table, the error at step 0 has 1.1 correct digits and the error at step 1 has 2.6.

Work it out: (1) Write a formula that expresses en+1e_{n+1} in terms of ene_n and explains the correct digits in the table.
(2) Write such a formula for bisection too, which cuts the error by a factor of 2 at each step.

Candidate formulations

Check each formulation against the rows of the table.

  1. “The new error is exactly the square of the previous one”, en+1=en2e_{n+1}=e_n^2⁠.
  2. “The new error is half the previous one”, en+1=12ene_{n+1}=\tfrac12e_n⁠.
  3. “The new error is roughly the square of the previous one, times a constant”, ∣en+1∣≈C∣en∣2\lvert e_{n+1}\rvert\approx C\lvert e_n\rvert^{2}⁠.
Solution
  1. The first formulation is wrong already at the first step: (8.58×10−2)2≈7.4×10−3(8.58\times10^{-2})^2\approx7.4\times10^{-3}⁠, while the error in the table is 2.45×10−32.45\times10^{-3}⁠. The second is bisection, and it does not fit the table: the error drops by far more than half. The third fits: ∣en+1∣≈C∣en∣2\lvert e_{n+1}\rvert\approx C\lvert e_n\rvert^{2} with a constant CC⁠. In the table the ratio ∣en+1∣/en2\lvert e_{n+1}\rvert/e_n^2 is about 0.330.33⁠, 0.350.35 and 0.350.35 at the three steps. The square alone, (2.45×10−3)2≈6.0×10−6(2.45\times10^{-3})^2\approx6.0\times10^{-6}⁠, gives 5.2 digits, and the actual error 2.12×10−62.12\times10^{-6} gives 5.7: about 0.450.45 more digits thanks to C≈0.35C\approx0.35⁠, since −log⁡100.35≈0.45-\log_{10}0.35\approx0.45⁠.
  2. ∣en+1∣≈12∣en∣\lvert e_{n+1}\rvert\approx\tfrac12\lvert e_n\rvert⁠: the same form, with power 1 instead of 2.
The formal definition

A method has order of convergence qq when its errors en=xn−x∗e_n=x_n-x^* satisfy

lim⁡n→∞∣en+1∣∣en∣q=C≠0,\lim_{n\to\infty}\frac{\lvert e_{n+1}\rvert}{\lvert e_n\rvert^{q}}=C\neq0,

that is, ∣en+1∣≈C∣en∣q\lvert e_{n+1}\rvert\approx C\lvert e_n\rvert^{q} near the root. q=1q=1 is linear convergence and q=2q=2 is quadratic.

  • Bisection: the error bound εn\varepsilon_n is halved at each step, so q=1q=1 and C=12C=\tfrac12 for the bound, not for the error itself. The error itself need not shrink at every step: for x2−125x^2-\tfrac1{25} on [0,1][0,1] the errors at the first midpoints are 0.30.3⁠, 0.050.05 and 0.0750.075⁠, and the ratios of the errors are 16\tfrac16 and then 32\tfrac32⁠.
  • Secant: q=1+52≈1.618q=\tfrac{1+\sqrt5}{2}\approx1.618 (at a root where f′(x∗)≠0f'(x^*)\neq0⁠).
  • Newton–Raphson: the convergence is at least quadratic; of order exactly 2 when f′′(x∗)≠0f''(x^*)\neq0 (at a root where f′(x∗)≠0f'(x^*)\neq0⁠).

In the definition CC is finite; for linear convergence (q=1q=1⁠) also C<1C<1⁠.

Continuing with the table: the correct digits are 1.1, 2.6, 5.7 and 11.8, and the ratios between consecutive steps are 2.4, 2.2 and 2.1, approaching 2 as the iterates approach the root.
This is quadratic convergence, against the steady gain of a fixed number of digits per step of linear methods.

The errors in the table are computed in high-precision arithmetic.
A computer working in double precision keeps about 16 digits, and the evaluation of ff is itself rounded, so one more step would stop at an absolute error of the order of 10−1610^{-16}⁠.
Below that level the limit is the arithmetic, not the method.

Steps to reach 10−1010^{-10} on the two problems of the page, by the explorer’s rule:

MethodMonthly instalmentsx2−2x^2-2
Bisection4036
Regula falsi1115
Secant58
Newton–Raphson44

(Brackets [0,0.05][0,0.05] and [0,2][0,2]⁠; the secant method starts from the two ends of the bracket, and Newton from 0.0250.025 and 1.51.5⁠.) In the explorer further down press Compare all methods to see the four error curves together.

Steps are not the whole cost. Newton evaluates ff and f′f' at every step, the secant method only ff⁠, so a secant step can be cheaper.
By how much depends on the problem: if f′f' costs as much as ff the saving is about half, but when f′f' reuses work already done for ff it costs little extra and the saving is small.
To compare methods fairly, we count evaluations of ff and of f′f' and measure the running time on our own function.
A method of order qq multiplies the number of correct digits by qq in each step; if a step needs cc evaluations, the factor per evaluation is q1/cq^{1/c}⁠.

Calculate: when f′f' costs as much as ff⁠, how many evaluations does a step of Newton need, and how many a step of the secant method?
What is the factor per evaluation of each, and which method gains digits faster per evaluation of ff⁠?

Solution

Newton has c=2c=2 and q=2q=2⁠, so its factor is 21/2≈1.4142^{1/2}\approx1.414⁠. The secant method has c=1c=1 and q=1+52q=\tfrac{1+\sqrt5}{2}⁠, so its factor is ≈1.618\approx1.618⁠, larger:
per evaluation of ff⁠, the secant method gains digits faster.

The explorer below holds all the examples of the page as tabs, and in each one you can choose any of the four methods.
The monthly instalments and x2−2x^2-2 compare the methods. The other three tabs show that Newton’s cost is the conditions it needs:
the derivative f′f'⁠, and a start near the root. The explanation of each example appears under the row of examples.

The explorer needs JavaScript. The table above shows the number of steps of the four methods, and the three ways Newton fails are: an almost zero slope (the tangent is nearly horizontal and the next point is flung far away), a cycle between two points, and divergence.

A full tutorial on the Newton–Raphson method, and a short video on running the iteration with the ANS and CALC keys of a scientific calculator:

Run Newton’s iteration yourself on the emulator, with the Ans key (the last answer), for f(x)=x2−2f(x)=x^2-2⁠:

A scientific calculator also has a built-in equation solver, the SOLVE function.
According to the manufacturer’s documentation it solves with Newton’s method, from the starting value you give it, so a good start matters here too:

Where does Newton land? The conditions Newton needs are about the start.
Try it: choose a function, drag the start along the axis, predict where the tangents lead (to which root, to a cycle, or away to infinity), and watch.
After a few predictions open the map of all starts: the boundaries between its bands are where a tiny change of start changes the outcome.

The lab needs JavaScript. The explorer above shows Newton’s method from a start of your choice.

The ladder so far

MethodUsesConvergenceNeedsMain risk
Bisectionsignslinear, halves the errora bracketslow
Regula falsivalues, a chordusually fastera bracketan end gets stuck
Secantvalues, two latest pointsorder 1.62two startsmay diverge
Newton–Raphsonvalues and slopeorder 2, digits doublef′f' and a good startflat slope, cycles, divergence

In practice, scientific libraries combine them: a safe bracket for the guarantee and a fast method for the speed, as in Brent’s method (SciPy’s brentq).

Continue learning

The next step is the practice session for this lecture: a table by hand, an implied volatility of a real option as a real problem, the usual mistakes, transfer questions and a Python implementation.

Repeating a Rule looks at fixed-point iteration, the idea behind many iterative methods, and at inverse quadratic interpolation, which together with bisection forms the Brent method used in practice.

My tutorial on fixed-point iteration, and on the multidimensional Newton–Raphson method: