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:


On this practice page we practise:

  • Newton and secant steps by hand, following the number of correct digits;
  • recognizing a cycle, divergence and a double root, and what a program should check;
  • avoiding the usual mistakes of Newton and the secant method;
  • proving that the order of convergence of the secant method is the golden ratio, and bounding Newton’s error before the first step;
  • implementing Newton and the secant method in Python and comparing their cost.

This practice follows the lecture Using the Slope: the Secant Method and Newton–Raphson:
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.

A real application: implied volatility

An option’s price depends on a number σ\sigma⁠, the volatility, which measures how much the underlying price is expected to jiggle.
A known formula turns σ\sigma into a price C(σ)C(\sigma)⁠. The market gives us the price, and we want the σ\sigma behind it.
A trading system has to compute it for thousands of options every time prices move, so every step saved in one computation is saved thousands of times.
Take a call option on the S&P 500 index (SPX) with strike 7650 that expires on 15 April 2027, 181 days ahead, so T=181365T=\tfrac{181}{365} years.
On Friday 16 October 2026 it was quoted at 422.8 (bid) and 428.4 (ask); we use the middle, 425.6.
Solve C(σ)=425.6C(\sigma)=425.6⁠, that is, find a root of

f(σ)=C(σ)−425.6,C(σ)=D[F Φ(d1)−K Φ(d2)],f(\sigma)=C(\sigma)-425.6,\qquad C(\sigma)=D\left[F\,\Phi(d_1)-K\,\Phi(d_2)\right], d1=ln⁡(F/K)+12σ2TσT,d2=d1−σT,d_1=\frac{\ln(F/K)+\tfrac12\sigma^2 T}{\sigma\sqrt T},\qquad d_2=d_1-\sigma\sqrt T,

where K=7650K=7650 is the strike price, F=7805.06F=7805.06 is the price for delivery on the expiry date as the market sets it, D=0.97507D=0.97507 is the discount factor (what one unit of currency arriving on the expiry date is worth today), and Φ\Phi is the cumulative distribution function of the standard normal distribution, a library function.
For the numerical task, C(σ)C(\sigma) is a function we can evaluate, with a slope we can write down.

The finance in 20 seconds

An option is the right to buy at the strike price KK on the expiry date.
The higher the expected jiggle σ\sigma⁠, the more the right is worth, so CC grows with σ\sigma⁠.
The σ\sigma that reproduces today’s market price is the implied volatility:
the market’s own estimate of future movement. FF and DD are read from the market itself, from the prices of calls and puts at the same strikes, so no interest rate or dividend has to be assumed.
Quotes are from Interactive Brokers (delayed, frozen), from 16 October 2026.

The slope of CC with respect to σ\sigma⁠, called vega, has a closed formula, D F φ(d1)TD\,F\,\varphi(d_1)\sqrt T⁠, where φ\varphi is the normal density.
So Newton–Raphson fits here. Open the explorer with Newton selected and the implied-volatility example (the start is σ0=0.3\sigma_0=0.3⁠) and step.
It also offers the yield of the bond from the bisection practice and the example “Newton from far”, which starts at σ0=0.005\sigma_0=0.005⁠.

The explorer needs JavaScript. The table below shows the steps of Newton on the same problem.

From σ0=0.3\sigma_0=0.3 the true value is 0.162953752296910.16295375229691 (16.295%), and the errors of Newton are:

StepEstimateff at the estimateError
00.3286.71.4×10−11.4\times10^{-1}
10.163177186075950.4652.2×10−42.2\times10^{-4}
20.162953756463528.7×10−68.7\times10^{-6}4.2×10−94.2\times10^{-9}
30.162953752296913.0×10−153.0\times10^{-15}1.5×10−181.5\times10^{-18}

Steps to reach 10−1010^{-10} by the explorer’s rule, on the bracket [0.05,1][0.05,1] (the secant method starts from the two ends, Newton from 0.30.3⁠):
bisection 43, regula falsi 5, secant 5, Newton–Raphson 3. The answer: the implied volatility of the option is about 16.295%, and three Newton steps are enough to reach it.

Work it out: in the example “Newton from far”, with σ0=0.005\sigma_0=0.005⁠, the first step jumps to σ≈1.5\sigma\approx1.5 million.
Explain why (hint: what is vega when the volatility is almost zero?).

Solution

At σ=0.005\sigma=0.005 the price barely depends on σ\sigma⁠, so vega is almost zero (about 2×10−42\times10^{-4}⁠).
The tangent is almost horizontal, and the Newton step f/f′f/f' divides an ordinary number by a tiny one, so it jumps very far.

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.

Proof: the Order of the Secant Method

Suppose ff is twice continuously differentiable on an interval around a root rr⁠, that rr is a simple root (f′(r)≠0f'(r)\neq0⁠) with f′′(r)≠0f''(r)\neq0⁠, and that the sequence xnx_n of the secant method is defined for every nn (f(xn)≠f(xn−1)f(x_n)\neq f(x_{n-1})⁠), converges to rr⁠, and has no term equal to rr⁠.
Write en=xn−re_n=x_n-r⁠. Prove that the order of convergence is φ=1+52≈1.618\varphi=\dfrac{1+\sqrt5}{2}\approx1.618⁠, that is,

lim⁡n→∞∣en+1∣∣en∣φ=c\lim_{n\to\infty}\frac{\lvert e_{n+1}\rvert}{\lvert e_n\rvert^{\varphi}}=c

for a constant c>0c>0⁠.

Hint: the error relation

For the two latest points the errors satisfy

en+1=f′′(ξn)2 sn en−1 ene_{n+1}=\frac{f''(\xi_n)}{2\,s_n}\,e_{n-1}\,e_n

where sn=f(xn)−f(xn−1)xn−xn−1s_n=\dfrac{f(x_n)-f(x_{n-1})}{x_n-x_{n-1}} is the slope of the secant line and ξn\xi_n is a point between the smallest and the largest of xn−1x_{n-1}⁠, xnx_n and rr (the block “An alternative proof with divided differences”, after the solution, shows where the relation comes from).
Try to deduce the order from it: (1) what is the limit of the ratio Kn=en+1en−1enK_n=\dfrac{e_{n+1}}{e_{n-1}e_n}⁠?
(2) what does a logarithm turn the product en−1ene_{n-1}e_n into? (3) which well-known recurrence appears, and what are the roots of p2=p+1p^2=p+1⁠?

Solution
  1. The coefficient has a limit. ξn\xi_n lies between the smallest and the largest of xn−1x_{n-1}⁠, xnx_n and rr⁠, and lim⁡n→∞xn−1=lim⁡n→∞xn=r\lim_{n\to\infty}x_{n-1}=\lim_{n\to\infty}x_n=r⁠, so by the squeeze theorem lim⁡n→∞ξn=r\lim_{n\to\infty}\xi_n=r⁠.
    By the mean value theorem sn=f′(ηn)s_n=f'(\eta_n) for some ηn\eta_n between xn−1x_{n-1} and xnx_n⁠; in the same way lim⁡n→∞ηn=r\lim_{n\to\infty}\eta_n=r⁠, and since f′f' is continuous, lim⁡n→∞sn=f′(r)\lim_{n\to\infty}s_n=f'(r)⁠.
    Since f′′f'' is continuous too,

    lim⁡n→∞Kn=lim⁡n→∞f′′(ξn)2sn=C=f′′(r)2f′(r)≠0\lim_{n\to\infty}K_n=\lim_{n\to\infty}\frac{f''(\xi_n)}{2s_n}=C=\frac{f''(r)}{2f'(r)}\neq0

    Moreover Kn≠0K_n\neq0 for every nn⁠, because en+1≠0e_{n+1}\neq0⁠.

  2. A logarithm turns the product into a sum. Let cn=∣C∣ ∣en∣c_n=\lvert C\rvert\,\lvert e_n\rvert and un=−ln⁡cnu_n=-\ln c_n⁠.
    Taking absolute values in en+1=Kn en−1 ene_{n+1}=K_n\,e_{n-1}\,e_n and multiplying by ∣C∣\lvert C\rvert gives cn+1=∣Kn∣∣C∣ cn cn−1c_{n+1}=\dfrac{\lvert K_n\rvert}{\lvert C\rvert}\,c_n\,c_{n-1}⁠, and taking −ln⁡-\ln⁠:

    un+1=un+un−1+δn,δn=−ln⁡∣Kn∣∣C∣u_{n+1}=u_n+u_{n-1}+\delta_n,\qquad \delta_n=-\ln\frac{\lvert K_n\rvert}{\lvert C\rvert}

    By step 1, lim⁡n→∞δn=−ln⁡1=0\lim_{n\to\infty}\delta_n=-\ln1=0⁠. Since lim⁡n→∞en=0\lim_{n\to\infty}e_n=0⁠, lim⁡n→∞un=∞\lim_{n\to\infty}u_n=\infty⁠. Up to a constant, unu_n is ln⁡10\ln10 times the number of correct digits, because un=ln⁡10⋅(−log⁡10∣en∣)−ln⁡∣C∣u_n=\ln10\cdot\bigl(-\log_{10}\lvert e_n\rvert\bigr)-\ln\lvert C\rvert⁠.

  3. The Fibonacci recurrence. Without δn\delta_n this is the recurrence of the Fibonacci sequence, tn+1=tn+tn−1t_{n+1}=t_n+t_{n-1}⁠.
    A sequence tn=pnt_n=p^n with p≠0p\neq0 satisfies it exactly when pn+1=pn+pn−1p^{n+1}=p^n+p^{n-1}⁠, that is, p2=p+1p^2=p+1⁠.
    The roots are φ=1+52\varphi=\tfrac{1+\sqrt5}{2} and ψ=1−52≈−0.618\psi=\tfrac{1-\sqrt5}{2}\approx-0.618⁠, with φ+ψ=1\varphi+\psi=1 and φψ=1−54=−1\varphi\psi=\tfrac{1-5}{4}=-1⁠. So we expect un+1≈φ unu_{n+1}\approx\varphi\,u_n⁠:
    the number of correct digits is multiplied by about φ\varphi in each step.
    The next step proves this, with δn\delta_n⁠.

  4. The deviation tends to 0. Let wn=un+1−φ unw_n=u_{n+1}-\varphi\,u_n⁠. By step 2 and 1−φ=ψ1-\varphi=\psi⁠,

    wn=un+un−1+δn−φ un=ψ un+un−1+δnw_n=u_n+u_{n-1}+\delta_n-\varphi\,u_n=\psi\,u_n+u_{n-1}+\delta_n

    Since −ψφ=1-\psi\varphi=1⁠, un−1=−ψφ un−1u_{n-1}=-\psi\varphi\,u_{n-1}⁠, so ψ un+un−1=ψ (un−φ un−1)=ψ wn−1\psi\,u_n+u_{n-1}=\psi\,(u_n-\varphi\,u_{n-1})=\psi\,w_{n-1}⁠, and therefore wn=ψ wn−1+δnw_n=\psi\,w_{n-1}+\delta_n⁠.

    Take ε>0\varepsilon>0 and NN such that ∣δn∣≤ε\lvert\delta_n\rvert\le\varepsilon for every n>Nn>N⁠. We prove by induction on k≥0k\ge0 that ∣wN+k∣≤∣ψ∣k∣wN∣+ε1−∣ψ∣\lvert w_{N+k}\rvert\le\lvert\psi\rvert^k\lvert w_N\rvert+\dfrac{\varepsilon}{1-\lvert\psi\rvert}⁠.
    Base case, k=0k=0⁠: ∣wN∣≤∣wN∣+ε1−∣ψ∣\lvert w_N\rvert\le\lvert w_N\rvert+\dfrac{\varepsilon}{1-\lvert\psi\rvert}⁠. Induction step: assume the bound for kk⁠; then

    ∣wN+k+1∣≤∣ψ∣ ∣wN+k∣+ε≤∣ψ∣k+1∣wN∣+∣ψ∣ ε1−∣ψ∣+ε=∣ψ∣k+1∣wN∣+ε1−∣ψ∣\begin{aligned} \lvert w_{N+k+1}\rvert&\le\lvert\psi\rvert\,\lvert w_{N+k}\rvert+\varepsilon\\ &\le\lvert\psi\rvert^{k+1}\lvert w_N\rvert+\frac{\lvert\psi\rvert\,\varepsilon}{1-\lvert\psi\rvert}+\varepsilon\\ &=\lvert\psi\rvert^{k+1}\lvert w_N\rvert+\frac{\varepsilon}{1-\lvert\psi\rvert} \end{aligned}

    Since ∣ψ∣<1\lvert\psi\rvert<1⁠, lim⁡k→∞∣ψ∣k=0\lim_{k\to\infty}\lvert\psi\rvert^k=0⁠, so ∣wn∣≤2ε1−∣ψ∣\lvert w_n\rvert\le\dfrac{2\varepsilon}{1-\lvert\psi\rvert} for every large enough nn⁠.
    This holds for every ε>0\varepsilon>0⁠, so lim⁡n→∞wn=0\lim_{n\to\infty}w_n=0⁠.

  5. Back to the errors. wn=−ln⁡cn+1+φln⁡cn=−ln⁡cn+1cn φw_n=-\ln c_{n+1}+\varphi\ln c_n=-\ln\dfrac{c_{n+1}}{c_n^{\,\varphi}}⁠, so lim⁡n→∞cn+1cn φ=e0=1\lim_{n\to\infty}\dfrac{c_{n+1}}{c_n^{\,\varphi}}=e^0=1⁠. Substituting cn=∣C∣ ∣en∣c_n=\lvert C\rvert\,\lvert e_n\rvert gives cn+1cn φ=∣C∣1−φ ∣en+1∣∣en∣φ\dfrac{c_{n+1}}{c_n^{\,\varphi}}=\lvert C\rvert^{1-\varphi}\,\dfrac{\lvert e_{n+1}\rvert}{\lvert e_n\rvert^{\varphi}}⁠, and therefore

    lim⁡n→∞∣en+1∣∣en∣φ=∣C∣φ−1>0\lim_{n\to\infty}\frac{\lvert e_{n+1}\rvert}{\lvert e_n\rvert^{\varphi}}=\lvert C\rvert^{\varphi-1}>0

    This is order φ\varphi with c=∣C∣φ−1c=\lvert C\rvert^{\varphi-1}⁠. In addition, un+1un=φ+wnun\dfrac{u_{n+1}}{u_n}=\varphi+\dfrac{w_n}{u_n} and lim⁡n→∞un=∞\lim_{n\to\infty}u_n=\infty⁠, so lim⁡n→∞un+1un=φ\lim_{n\to\infty}\dfrac{u_{n+1}}{u_n}=\varphi⁠.

Note: matching powers alone (assume ∣en+1∣=c ∣en∣p\lvert e_{n+1}\rvert=c\,\lvert e_n\rvert^{p}⁠, substitute, and get p2=p+1p^2=p+1⁠) assumes that the limit exists.
Step 4 proves that it does.

An alternative proof with divided differences

This proof derives the error relation of the hint, and then reaches the order with approximations.
We use the divided difference, f[x,y]=f(x)−f(y)x−yf[x,y]=\dfrac{f(x)-f(y)}{x-y} and f[x,y,z]=f[x,y]−f[y,z]x−zf[x,y,z]=\dfrac{f[x,y]-f[y,z]}{x-z}⁠, and the identity from the proof on the convergence-rate page:
for the line LL through (a,f(a))(a,f(a)) and (b,f(b))(b,f(b))⁠, f(x)−L(x)=f[a,b,x] (x−a) (x−b)f(x)-L(x)=f[a,b,x]\,(x-a)\,(x-b)⁠. Here a=xn−1a=x_{n-1} and b=xnb=x_n⁠, so LL is the secant line, its slope is f[xn−1,xn]=snf[x_{n-1},x_n]=s_n⁠, and xn+1x_{n+1} is its zero.
Write rr for the root and ek=xk−re_k=x_k-r⁠.

  1. Put x=rx=r and f(r)=0f(r)=0⁠: −L(r)=f[xn−1,xn,r] (r−xn−1) (r−xn)=f[xn−1,xn,r] en−1 en-L(r)=f[x_{n-1},x_n,r]\,(r-x_{n-1})\,(r-x_n)=f[x_{n-1},x_n,r]\,e_{n-1}\,e_n⁠.
  2. LL is a line with slope sns_n and L(xn+1)=0L(x_{n+1})=0⁠, so L(r)=L(r)−L(xn+1)=sn (r−xn+1)=−sn en+1L(r)=L(r)-L(x_{n+1})=s_n\,(r-x_{n+1})=-s_n\,e_{n+1}⁠.
  3. Equate (1) and (2) and divide by sns_n (nonzero when the line is not horizontal): en+1=Kn en−1 ene_{n+1}=K_n\,e_{n-1}\,e_n with Kn=f[xn−1,xn,r]snK_n=\dfrac{f[x_{n-1},x_n,r]}{s_n}⁠.
  4. f[xn−1,xn,r]=12f′′(ξn)f[x_{n-1},x_n,r]=\tfrac12f''(\xi_n) for some ξn\xi_n between the smallest and the largest of the three points. Indeed, the function g(t)=f(t)−L(t)−f[xn−1,xn,r] (t−xn−1) (t−xn)g(t)=f(t)-L(t)-f[x_{n-1},x_n,r]\,(t-x_{n-1})\,(t-x_n) is zero at xn−1x_{n-1} and at xnx_n (where LL agrees with ff⁠) and at rr (by step 1). By Rolle’s theorem (the mean value theorem when the values at the two ends are equal), g′g' has a zero between each two neighbouring points of the three, and g′′g'' has a zero ξn\xi_n between those two zeros. LL is a line and the product is a parabola with leading coefficient 1, so g′′(t)=f′′(t)−2f[xn−1,xn,r]g''(t)=f''(t)-2f[x_{n-1},x_n,r]⁠, and g′′(ξn)=0g''(\xi_n)=0 gives the claim. With it, step 3 is exactly the relation of the hint.
  5. Since lim⁡n→∞xn=r\lim_{n\to\infty}x_n=r⁠, lim⁡n→∞Kn=C=f′′(r)2f′(r)\lim_{n\to\infty}K_n=C=\dfrac{f''(r)}{2f'(r)}⁠, assuming f′(r)≠0f'(r)\neq0 and f′′(r)≠0f''(r)\neq0⁠. That is, ∣en+1∣≈∣C∣ ∣en∣ ∣en−1∣\lvert e_{n+1}\rvert\approx\lvert C\rvert\,\lvert e_n\rvert\,\lvert e_{n-1}\rvert⁠.
  6. Let cn=∣C∣ ∣en∣c_n=\lvert C\rvert\,\lvert e_n\rvert⁠, so cn+1≈cn cn−1c_{n+1}\approx c_n\,c_{n-1}⁠. Taking logarithms, un=−ln⁡cnu_n=-\ln c_n satisfies un+1≈un+un−1u_{n+1}\approx u_n+u_{n-1}⁠, like the Fibonacci sequence.
  7. Every solution of tn+1=tn+tn−1t_{n+1}=t_n+t_{n-1} is Aφn+BψnA\varphi^n+B\psi^n⁠, with φ=1+52\varphi=\tfrac{1+\sqrt5}{2} and ψ=1−52\psi=\tfrac{1-\sqrt5}{2}⁠: such a combination satisfies the recurrence, AA and BB can be chosen to match t0t_0 and t1t_1⁠, and these two terms determine all the others. Since ∣ψ∣<1\lvert\psi\rvert<1⁠, the term ψn\psi^n fades away, so unu_n grows like φn\varphi^n⁠, and un+1≈φ unu_{n+1}\approx\varphi\,u_n⁠.
  8. un+1≈φ unu_{n+1}\approx\varphi\,u_n means cn+1≈cn φc_{n+1}\approx c_n^{\,\varphi}⁠, that is, ∣en+1∣≈∣C∣φ−1 ∣en∣φ\lvert e_{n+1}\rvert\approx\lvert C\rvert^{\varphi-1}\,\lvert e_n\rvert^{\varphi}⁠: order of convergence φ≈1.618\varphi\approx1.618⁠.

Steps 6 to 8 treat the approximate recurrence as if it were exact; this is the usual textbook argument.
Step 4 of the solution above shows that the conclusion holds with the error term δn\delta_n⁠.

Check the relation on an example. For f(x)=x2−2f(x)=x^2-2 with the secant method from x0=2x_0=2 and x1=1.5x_1=1.5⁠:
compute e2e1e0\dfrac{e_2}{e_1e_0} and compare it with 1x0+x1\dfrac{1}{x_0+x_1}⁠.

Solution

e0=2−2≈0.5858e_0=2-\sqrt2\approx0.5858⁠, e1=1.5−2≈0.0858e_1=1.5-\sqrt2\approx0.0858⁠, x2=107x_2=\tfrac{10}{7} so e2≈0.01436e_2\approx0.01436⁠. The ratio is e2e1e0≈0.2857=13.5=1x0+x1\dfrac{e_2}{e_1e_0}\approx0.2857=\dfrac1{3.5}=\dfrac1{x_0+x_1}⁠. For x2−2x^2-2 the slope of the secant line is sn=xn2−xn−12xn−xn−1=xn−1+xns_n=\dfrac{x_n^2-x_{n-1}^2}{x_n-x_{n-1}}=x_{n-1}+x_n and f′′=2f''=2⁠, so the relation is exact with coefficient Kn=1xn−1+xnK_n=\dfrac1{x_{n-1}+x_n}⁠.

The same run continues with x3=5841x_3=\tfrac{58}{41}⁠, x4=577408x_4=\tfrac{577}{408} and so on: for x2−2x^2-2 every xnx_n is a fraction, computed exactly.
The errors and two ratios:

nnene_nen+1/(en−1en)e_{n+1}/(e_{n-1}e_n)∣en+1∣/∣en∣φ\lvert e_{n+1}\rvert/\lvert e_n\rvert^{\varphi}
05.86×10−15.86\times10^{-1}0.204
18.58×10−28.58\times10^{-2}0.28570.764
21.44×10−21.44\times10^{-2}0.34150.403
34.21×10−44.21\times10^{-4}0.35170.616
42.12×10−62.12\times10^{-6}0.35350.477

Which numbers do the last two columns approach, according to the solution of the proof?
Why does the last column jump above and below its limit, with jumps that shrink?

Hint

The limit of the last column is ∣C∣φ−1\lvert C\rvert^{\varphi-1} with C=f′′(r)2f′(r)C=\dfrac{f''(r)}{2f'(r)}⁠. For the jumps, look at step 4 of the solution:
by step 5, the last column divided by its limit is e−wne^{-w_n}⁠.

Solution

C=f′′(2)2f′(2)=242=122≈0.3536C=\dfrac{f''(\sqrt2)}{2f'(\sqrt2)}=\dfrac{2}{4\sqrt2}=\dfrac1{2\sqrt2}\approx0.3536⁠, and the third column, 1xn−1+xn\dfrac1{x_{n-1}+x_n}⁠, approaches it. The last column approaches ∣C∣φ−1=0.35360.618≈0.526\lvert C\rvert^{\varphi-1}=0.3536^{0.618}\approx0.526⁠.

The last column divided by 0.5260.526 is e−wne^{-w_n}⁠, and wn=ψ wn−1+δnw_n=\psi\,w_{n-1}+\delta_n with ψ≈−0.618\psi\approx-0.618⁠.
Near the root δn\delta_n is tiny, so each wnw_n is about −0.618-0.618 times the one before:
its sign flips and its size shrinks by a factor of about 0.6180.618 in each step.
Indeed ln⁡0.2040.526\ln\dfrac{0.204}{0.526}⁠, ln⁡0.7640.526\ln\dfrac{0.764}{0.526} and the following ones are −0.95-0.95⁠, 0.370.37⁠, −0.27-0.27⁠, 0.160.16 and −0.10-0.10⁠, and the ratios between neighbours are −0.39-0.39⁠, −0.71-0.71⁠, −0.60-0.60 and −0.62-0.62⁠.

More exercises

Newton and the secant method on cos⁡x−x\cos x-x

The equation cos⁡x=x\cos x=x (xx in radians) has one root, r=0.739085133215…r=0.739085133215\ldots Let f(x)=cos⁡x−xf(x)=\cos x-x⁠.

  1. Do three Newton steps from x0=1x_0=1⁠. Write a table with nn⁠, xnx_n⁠, f(xn)f(x_n) and the number of correct digits −log⁡10∣xn−r∣-\log_{10}\lvert x_n-r\rvert⁠.
  2. Run the secant method from x0=0x_0=0 and x1=1x_1=1 up to x6x_6⁠, with the same columns.
  3. Suppose an evaluation of f′f' costs as much as one of ff⁠. How many evaluations does each method use until it has at least 9 correct digits, and how many correct digits does it have then? Which number does the ratio between the numbers of correct digits of consecutive steps approach in each method?
Hint

f′(x)=−sin⁡x−1f'(x)=-\sin x-1⁠. Set the calculator to radians, keep the full value in its memory (with Ans) and round only when you write.
The secant step is 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})}⁠. Newton’s step nn needs ff and f′f' at xn−1x_{n-1}⁠; the secant step that gives xn+1x_{n+1} needs only the new value f(xn)f(x_n)⁠, because f(xn−1)f(x_{n-1}) is known from the step before.

Solution
  1. Newton:
nnxnx_nf(xn)f(x_n)−log⁡10∣en∣-\log_{10}\lvert e_n\rvert
01−0.4597-0.45970.58
10.750363867840−1.89×10−2-1.89\times10^{-2}1.95
20.739112890911−4.65×10−5-4.65\times10^{-5}4.56
30.739085133385−2.85×10−10-2.85\times10^{-10}9.77

For example x1=1−−0.459698−1.841471=0.750364x_1=1-\dfrac{-0.459698}{-1.841471}=0.750364⁠. The ratio of the error to the square of the previous error is 0.1660.166⁠, 0.2180.218 and 0.2210.221⁠, and it approaches f′′(r)2f′(r)=cos⁡r2(1+sin⁡r)≈0.2208\dfrac{f''(r)}{2f'(r)}=\dfrac{\cos r}{2(1+\sin r)}\approx0.2208⁠.

  1. The secant method:
nnxnx_nf(xn)f(x_n)−log⁡10∣en∣-\log_{10}\lvert e_n\rvert
0010.13
11−0.4597-0.45970.58
20.6850733573268.93×10−28.93\times10^{-2}1.27
30.7362989976144.66×10−34.66\times10^{-3}2.56
40.739119361912−5.73×10−5-5.73\times10^{-5}4.47
50.7390851121273.53×10−83.53\times10^{-8}7.68
60.7390851332152.67×10−132.67\times10^{-13}12.80
  1. Newton reaches 9.77 digits at x3x_3⁠, after ff and f′f' at x0x_0⁠, x1x_1 and x2x_2⁠: 6 evaluations. The secant method first passes 9 digits at x6x_6⁠, after ff at x0,…,x5x_0,\dots,x_5⁠: also 6 evaluations, but with 12.80 digits. After 4 evaluations the two are level: Newton’s x2x_2 has 4.56 digits and the secant method’s x4x_4 has 4.47. The ratios of digits are 3.33.3⁠, 2.32.3 and 2.12.1 for Newton, approaching 2, and 2.022.02⁠, 1.751.75⁠, 1.721.72 and 1.671.67 for the secant method (from x3x_3 on), approaching φ≈1.618\varphi\approx1.618⁠. Per evaluation the secant method gains more here, as the factors φ≈1.618\varphi\approx1.618 against 2≈1.414\sqrt2\approx1.414 predict.

Two roots: ex−3xe^x-3x

The function f(x)=ex−3xf(x)=e^x-3x has two roots, r1≈0.619061r_1\approx0.619061 and r2≈1.512135r_2\approx1.512135⁠.

  1. Run Newton from x0=0x_0=0⁠, 11⁠, 1.21.2 and 22⁠. For each start write x1x_1 and x2x_2⁠, and say which root the iteration reaches.
  2. Without computing, which root does Newton reach from any start x0<ln⁡3≈1.0986x_0<\ln3\approx1.0986⁠, and which from any start x0>ln⁡3x_0>\ln3⁠? Justify with the identity en+1=f′′(ξn)2f′(xn) en2e_{n+1}=\dfrac{f''(\xi_n)}{2f'(x_n)}\,e_n^2 from how wrong is one step and with the condition that holds from any start on the right side.
  3. From x0=1.1x_0=1.1 the first step jumps to x1≈72.1x_1\approx72.1⁠. Why, and how does the iteration come back?
Hint

f′(x)=ex−3f'(x)=e^x-3 is zero at ln⁡3\ln3⁠, negative to its left and positive to its right, and f′′(x)=ex>0f''(x)=e^x>0⁠.
At x=1x=1⁠, f(1)=f′(1)f(1)=f'(1)⁠. In part 2, apply the identity with the root on the same side of ln⁡3\ln3 as x0x_0⁠, and look at the sign of e1e_1⁠.

Solution
  1. The steps:
x0x_0x1x_1x2x_2lim⁡xn\lim x_n
00.50.610060r1r_1
100.5r1r_1
1.22.0743151.724221r2r_2
21.6835181.543482r2r_2

From 00⁠: f(0)=1f(0)=1 and f′(0)=−2f'(0)=-2⁠, so x1=0.5x_1=0.5⁠. From 11⁠: f(1)=f′(1)=e−3f(1)=f'(1)=e-3⁠, so x1=1−1=0x_1=1-1=0 exactly, and from there the run is the run from 00⁠, one step later.
From 0 and from 2 the error is below 10−1010^{-10} after 5 steps.

  1. Take x0<ln⁡3x_0<\ln3 and the root r1r_1⁠, which is also left of ln⁡3\ln3⁠.
    The identity gives x1−r1=f′′(ξ0)2f′(x0) (x0−r1)2x_1-r_1=\dfrac{f''(\xi_0)}{2f'(x_0)}\,(x_0-r_1)^2 with f′′(ξ0)>0f''(\xi_0)>0 and f′(x0)<0f'(x_0)<0⁠, so x1≤r1x_1\le r_1⁠.
    If x1=r1x_1=r_1 we are done. Otherwise, on the interval [x1,1][x_1,1] we have f(x1)>0>f(1)=e−3f(x_1)>0>f(1)=e-3⁠, f′<0f'<0 and f′′>0f''>0⁠, and f(x1) f′′(x1)>0f(x_1)\,f''(x_1)>0⁠, so by the condition for any start on the right side Newton from x1x_1 converges to r1r_1⁠.
    In the same way, for x0>ln⁡3x_0>\ln3 and the root r2r_2⁠: f′(x0)>0f'(x_0)>0⁠, so x1≥r2x_1\ge r_2⁠, and on [1.2,x1][1.2,x_1] we have f(1.2)<0<f(x1)f(1.2)<0<f(x_1)⁠, f′>0f'>0 and f′′>0f''>0⁠, so the iteration converges to r2r_2⁠.
    At x0=ln⁡3x_0=\ln3 the tangent is horizontal and the step is undefined.
    The table agrees: 0 and 1 are left of ln⁡3\ln3⁠, and 1.2 and 2 are right of it.

  2. f′(1.1)=e1.1−3≈0.00417f'(1.1)=e^{1.1}-3\approx0.00417⁠, almost zero, and f(1.1)≈−0.296f(1.1)\approx-0.296⁠, so the step is −f(1.1)f′(1.1)≈71.0-\dfrac{f(1.1)}{f'(1.1)}\approx71.0 and x1≈72.11x_1\approx72.11⁠.
    Far to the right exe^x dominates: the step is ex−3xex−3=1−3x−3ex−3≈1\dfrac{e^x-3x}{e^x-3}=1-\dfrac{3x-3}{e^x-3}\approx1⁠, so each step moves left by about 1:
    71.1171.11⁠, 70.1170.11⁠, and so on. Since 1.1>ln⁡31.1>\ln3 the iteration reaches r2r_2⁠, as part 2 says, but only after 78 steps does the error drop below 10−1010^{-10}⁠, against 5 steps from 2.

A double root: (x−1)2(x+2)(x-1)^2(x+2)

The polynomial f(x)=(x−1)2(x+2)=x3−3x+2f(x)=(x-1)^2(x+2)=x^3-3x+2 has a double root at 11 (f(1)=f′(1)=0f(1)=f'(1)=0⁠) and a simple root at −2-2⁠.

  1. Show that Newton’s step is xn+1=xn−(xn−1)(xn+2)3(xn+1)x_{n+1}=x_n-\dfrac{(x_n-1)(x_n+2)}{3(x_n+1)}⁠, and that the error en=xn−1e_n=x_n-1 satisfies en+1=en (2en+3)3 (en+2)e_{n+1}=\dfrac{e_n\,(2e_n+3)}{3\,(e_n+2)}⁠. What is lim⁡n→∞en+1en\lim_{n\to\infty}\dfrac{e_{n+1}}{e_n} when the iteration converges to 1?
  2. From x0=2x_0=2 compute x1x_1⁠, x2x_2 and x3x_3 and the ratios en+1en\dfrac{e_{n+1}}{e_n}⁠. How many steps does one more correct digit cost near the root?
  3. From x0=−3x_0=-3 the distances from the root −2-2 are 11⁠, 0.3330.333⁠, 0.05560.0556⁠, 1.95×10−31.95\times10^{-3} and 2.53×10−62.53\times10^{-6}⁠. What is different, and why?
  4. When en=10−8e_n=10^{-8}⁠, about how large is f(xn)f(x_n)⁠? Computing x3−3x+2x^3-3x+2 near 1 in binary64 has a rounding error of a few times 10−1610^{-16}⁠. What does this mean for the stopping test on δn=∣f(xn)∣\delta_n=\lvert f(x_n)\rvert⁠?
Hint

f′(x)=3x2−3=3(x−1)(x+1)f'(x)=3x^2-3=3(x-1)(x+1)⁠, so (x−1)(x-1) cancels in f(x)f′(x)\dfrac{f(x)}{f'(x)}⁠. For the error put xn=1+enx_n=1+e_n⁠.
In part 4, f(xn)=en2 (en+3)f(x_n)=e_n^2\,(e_n+3)⁠.

Solution
  1. f(x)f′(x)=(x−1)2(x+2)3(x−1)(x+1)=(x−1)(x+2)3(x+1)\dfrac{f(x)}{f'(x)}=\dfrac{(x-1)^2(x+2)}{3(x-1)(x+1)}=\dfrac{(x-1)(x+2)}{3(x+1)} for x≠1x\neq1⁠. With xn=1+enx_n=1+e_n⁠: en+1=en−en (en+3)3 (en+2)=en(3en+6−en−3)3 (en+2)=en (2en+3)3 (en+2)e_{n+1}=e_n-\dfrac{e_n\,(e_n+3)}{3\,(e_n+2)}=\dfrac{e_n\bigl(3e_n+6-e_n-3\bigr)}{3\,(e_n+2)}=\dfrac{e_n\,(2e_n+3)}{3\,(e_n+2)}⁠. So en+1en=2en+33(en+2)\dfrac{e_{n+1}}{e_n}=\dfrac{2e_n+3}{3(e_n+2)}⁠, and when lim⁡n→∞en=0\lim_{n\to\infty}e_n=0 the limit is 36=12\dfrac36=\dfrac12⁠: linear convergence with ratio 12\tfrac12⁠.
  2. x1=2−1⋅43⋅3=149≈1.555556x_1=2-\dfrac{1\cdot4}{3\cdot3}=\dfrac{14}{9}\approx1.555556⁠, x2=1+185621≈1.297907x_2=1+\dfrac{185}{621}\approx1.297907 and x3≈1.155390x_3\approx1.155390⁠. The ratios en+1en\dfrac{e_{n+1}}{e_n} are 0.55560.5556⁠, 0.53620.5362 and 0.52160.5216⁠, and they approach 0.50.5⁠. Halving the error adds log⁡102≈0.301\log_{10}2\approx0.301 digits, so a digit costs about 3.33.3 steps, as in bisection: from e0=1e_0=1 it takes 27 steps to reach e27≈9.7×10−9e_{27}\approx9.7\times10^{-9}⁠.
  3. At −2-2 the root is simple, f′(−2)=9≠0f'(-2)=9\neq0⁠, and the convergence is quadratic: the numbers of correct digits are 00⁠, 0.480.48⁠, 1.261.26⁠, 2.712.71 and 5.605.60⁠, almost doubling. At 11 the derivative is zero at the root, so on every interval around it there is no m>0m>0⁠, as in the example x2x^2 in a double root.
  4. f(xn)=en2 (en+3)≈3×10−16f(x_n)=e_n^2\,(e_n+3)\approx3\times10^{-16}⁠, below the rounding error of the computation. So in binary64 the computed ff is rounding noise while the error is still about 10−810^{-8}⁠: Newton from x0=2x_0=2 in binary64 computes f(xn)=0f(x_n)=0 exactly after about thirty steps (the exact count depends on how ff is written), with an error of a few times 10−910^{-9}⁠, and stays there. The test on δn\delta_n is passed with only about 8 correct digits. Near a double root a small residual does not mean a small error: the bound ∣en∣≤∣f(xn)∣m\lvert e_n\rvert\le\dfrac{\lvert f(x_n)\rvert}{m} from when to stop needs m>0m>0⁠.

Newton on a cube root

The function f(x)=x3f(x)=\sqrt[3]{x} (the real cube root, negative for negative xx⁠) is increasing, and its only root is 00⁠.

  1. Show that for x≠0x\neq0 Newton’s step is xn+1=−2xnx_{n+1}=-2x_n⁠. Compute x1,…,x4x_1,\dots,x_4 from x0=0.1x_0=0.1⁠, and the values ∣f(xn)∣\lvert f(x_n)\rvert⁠.
  2. Which condition of the local convergence statement fails, although the slope is far from zero?
  3. A program computes εn=∣xn−xn−1∣\varepsilon_n=\lvert x_n-x_{n-1}\rvert and δn=∣f(xn)∣\delta_n=\lvert f(x_n)\rvert in every step. What do they show here in the first steps, and what should the program do?
Hint

f′(x)=13x23f'(x)=\dfrac{1}{3\sqrt[3]{x^2}} for x≠0x\neq0⁠, so f(x)f′(x)=3x3⋅x23\dfrac{f(x)}{f'(x)}=3\sqrt[3]{x}\cdot\sqrt[3]{x^2}⁠.

Solution
  1. f(x)f′(x)=3x3 x23=3x\dfrac{f(x)}{f'(x)}=3\sqrt[3]{x}\,\sqrt[3]{x^2}=3x⁠, so xn+1=xn−3xn=−2xnx_{n+1}=x_n-3x_n=-2x_n⁠. From 0.10.1⁠: −0.2-0.2⁠, 0.40.4⁠, −0.8-0.8⁠, 1.61.6⁠, and ∣f(xn)∣\lvert f(x_n)\rvert is 0.4640.464⁠, 0.5850.585⁠, 0.7370.737⁠, 0.9280.928⁠, 1.1701.170⁠, multiplied by 23≈1.26\sqrt[3]{2}\approx1.26 in each step. The error doubles and changes sign in every step, from every start x0≠0x_0\neq0⁠, however close to the root.
  2. Near 00 the slope f′(x)f'(x) is large, so a bound m>0m>0 exists, but f′(0)f'(0) does not exist (the tangent at the root is vertical) and f′′(x)=−29x53f''(x)=-\dfrac{2}{9\sqrt[3]{x^5}} is unbounded near 00⁠. No interval around the root has ∣f′′∣≤M\lvert f''\rvert\le M⁠, so K=M2mK=\dfrac{M}{2m} does not exist. The tangent at xx is so steep that it crosses the axis at −2x-2x⁠, on the other side and twice as far.
  3. εn=3∣xn−1∣\varepsilon_n=3\lvert x_{n-1}\rvert is 0.30.3⁠, 0.60.6⁠, 1.21.2⁠, 2.42.4⁠, and δn\delta_n grows: neither becomes small, and both grow in every step. The guards of the course conventions protect the program: the cap on the number of steps ends the loop, and a check that δn\delta_n decreases notices the growth already in the first step; then the program should fall back to bisection on a bracket, for example [−0.5,1][-0.5,1]⁠, where f(−0.5)<0<f(1)f(-0.5)<0<f(1)⁠.

Which method gains more per evaluation

Near the root a method of order qq multiplies the number of correct digits by qq in each step, and when a step costs cc evaluations of ff⁠, the factor per evaluation is q1/cq^{1/c}⁠.
Suppose an evaluation of f′f' costs a fraction tt of an evaluation of ff⁠, so a Newton step costs 1+t1+t⁠.

  1. For t=1t=1⁠, t=0.5t=0.5 and t=0.1t=0.1 compute Newton’s factor 21/(1+t)2^{1/(1+t)} and compare it with the secant method’s factor φ≈1.618\varphi\approx1.618⁠.
  2. For which tt do the two methods gain digits equally fast per evaluation?
  3. For t=1t=1⁠, about how many evaluations does each method need to go from 1 correct digit to 12, if the factors hold from the start?
Hint

After kk evaluations, 1 correct digit becomes about gkg^k digits, where gg is the factor per evaluation.
In part 2 take logarithms of 21/(1+t)=φ2^{1/(1+t)}=\varphi⁠.

Solution
  1. t=1t=1⁠: 21/2≈1.414<1.6182^{1/2}\approx1.414<1.618⁠, the secant method gains more. t=0.5t=0.5⁠: 21/1.5=22/3≈1.587<1.6182^{1/1.5}=2^{2/3}\approx1.587<1.618⁠, the secant method still gains a little more. t=0.1t=0.1⁠: 21/1.1≈1.878>1.6182^{1/1.1}\approx1.878>1.618⁠, Newton gains more.
  2. 21/(1+t)=φ2^{1/(1+t)}=\varphi means ln⁡21+t=ln⁡φ\dfrac{\ln2}{1+t}=\ln\varphi⁠, so t=ln⁡2ln⁡φ−1≈1.440−1=0.440t=\dfrac{\ln2}{\ln\varphi}-1\approx1.440-1=0.440⁠. Newton gains more per evaluation when f′f' costs less than about 44% of an evaluation of ff⁠, for example when f′f' reuses most of the work already done for ff⁠.
  3. gk=12g^k=12 gives k=ln⁡12ln⁡gk=\dfrac{\ln12}{\ln g}⁠: for Newton ln⁡12ln⁡1.414≈7.2\dfrac{\ln12}{\ln1.414}\approx7.2⁠, for the secant method ln⁡12ln⁡1.618≈5.2\dfrac{\ln12}{\ln1.618}\approx5.2⁠. So about 7 against 5 evaluations. The factors describe the methods near the root; far from it they say nothing.

A bound before the first step: x3−2x^3-2

The root of f(x)=x3−2f(x)=x^3-2 is r=23≈1.259921r=\sqrt[3]{2}\approx1.259921⁠. Follow the procedure for bounding Newton’s error with ρ=0.25\rho=0.25⁠.

  1. Find II⁠, MM⁠, mm and KK⁠, and check that Kρ<1K\rho<1⁠.
  2. Is x0=1.5x_0=1.5 in II⁠? Compute the bound 1K(K∣e0∣)2n\dfrac1K\bigl(K\lvert e_0\rvert\bigr)^{2^n} for n=1,2,3n=1,2,3⁠, and compare it with the actual errors of Newton from 1.51.5⁠, which are 3.64×10−23.64\times10^{-2}⁠, 1.01×10−31.01\times10^{-3} and 8.11×10−78.11\times10^{-7}⁠.
  3. Compute also the one-step bound K en−12K\,e_{n-1}^2 with the actual error of the previous step, for n=2,3n=2,3⁠. Why is it much closer to the actual error?
Hint

f′(x)=3x2f'(x)=3x^2 and f′′(x)=6xf''(x)=6x both increase on II⁠, and f(3)=6f^{(3)}=6 is never zero, so the extreme values are at the ends of II⁠.

Solution
  1. I=[r−0.25, r+0.25]≈[1.0099, 1.5099]I=[r-0.25,\,r+0.25]\approx[1.0099,\,1.5099]⁠. M=6 (r+0.25)≈9.06M=6\,(r+0.25)\approx9.06⁠, m=3 (r−0.25)2≈3.06>0m=3\,(r-0.25)^2\approx3.06>0⁠, K=M2m≈1.480K=\dfrac{M}{2m}\approx1.480 and Kρ≈0.370<1K\rho\approx0.370<1⁠.
  2. 1.5<1.50991.5<1.5099⁠, so x0∈Ix_0\in I⁠, with e0≈0.2401e_0\approx0.2401 and K∣e0∣≈0.355K\lvert e_0\rvert\approx0.355⁠. The bound gives 8.53×10−28.53\times10^{-2}⁠, 1.08×10−21.08\times10^{-2} and 1.72×10−41.72\times10^{-4} for n=1,2,3n=1,2,3⁠: above the actual errors in every step, but at n=3n=3 about 200 times larger. The bound squares K∣e0∣=0.355K\lvert e_0\rvert=0.355 again and again, while the actual errors shrink faster.
  3. K e12≈1.96×10−3K\,e_1^2\approx1.96\times10^{-3} against 1.01×10−31.01\times10^{-3}⁠, and K e22≈1.51×10−6K\,e_2^2\approx1.51\times10^{-6} against 8.11×10−78.11\times10^{-7}⁠: within a factor of 2. The actual coefficient approaches f′′(r)2f′(r)=6r6r2=1r≈0.794\dfrac{f''(r)}{2f'(r)}=\dfrac{6r}{6r^2}=\dfrac1r\approx0.794⁠, and K≈1.48K\approx1.48 is about 1.91.9 times larger.

Root finding, cell by cell

The secant method and Newton–Raphson 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 Secant Method and Newton–Raphson.
The glossary has the terms: glossary.