Exercise

Practice: fixed-point iteration and inverse quadratic interpolation

Fixed-point iterations by hand, predicting the convergence factor, one step of inverse quadratic interpolation, the usual mistakes, and a Python implementation: practice for the lesson on repeating a rule.

Last updated:


By the end of the practice session you can:

  • run a fixed-point iteration by hand and predict its speed from g′g'⁠;
  • tell a rule that converges from one that cycles or runs away;
  • do one step of inverse quadratic interpolation by hand;
  • implement fixed-point iteration in Python, with a stopping rule that cannot loop forever.

This practice follows the lecture Repeating a Rule: read it or attend it first.

By hand: predicting the factor

The convergence factor of a fixed-point iteration is ∣g′(x∗)∣|g'(x^*)|⁠:
roughly, each step multiplies the error by it.

  1. For g(x)=cos⁡xg(x)=\cos x and x0=1x_0=1 compute x1x_1⁠, x2x_2 and x3x_3⁠‏. The fixed point is x∗=0.7390851…x^*=0.7390851\ldots⁠‏. What are the errors ∣xn−x∗∣|x_n-x^*| of the three steps?
  2. The rule g(x)=x+2x+1g(x)=\dfrac{x+2}{x+1} also has the fixed point 2\sqrt2⁠‏. Check this, compute g′(2)g'(\sqrt2) and predict how many steps one more correct digit costs. Then run three steps from x0=1x_0=1 and compare.
  3. For f(x)=x2−2f(x)=x^2-2 use the three points 0, 1, 20,\ 1,\ 2 and do one step of inverse quadratic interpolation by hand.
  4. Do one secant step from the points 11 and 22 and compare with the result of question 3.
  5. Add xx to both sides of x2−2=0x^2-2=0 and write it as x=g(x)x=g(x)⁠. Compute g′(2)g'(\sqrt2) and predict what the iteration does from x0=1.5x_0=1.5⁠. Check with two steps.
Hint for question 2

A number is a fixed point if x=x+2x+1x=\dfrac{x+2}{x+1}⁠‏. Multiply both sides by x+1x+1⁠‏.
For the derivative, use ddxx+2x+1=(x+1)−(x+2)(x+1)2\dfrac{d}{dx}\dfrac{x+2}{x+1}=\dfrac{(x+1)-(x+2)}{(x+1)^2}⁠‏.

Hint for question 3

The formula for inverse quadratic interpolation is on the formula sheet.
Substitute (a,fa)=(0,−2)(a,f_a)=(0,-2)⁠‏, (b,fb)=(1,−1)(b,f_b)=(1,-1) and (c,fc)=(2,2)(c,f_c)=(2,2)⁠‏.

Solutions
  1. x1=cos⁡1=0.54030x_1=\cos1=0.54030 (error 0.1990.199⁠)‏, x2=cos⁡0.54030=0.85755x_2=\cos0.54030=0.85755 (error 0.1180.118⁠)‏, x3=cos⁡0.85755=0.65429x_3=\cos0.85755=0.65429 (error 0.08480.0848⁠)‏. The error shrinks but the iterates alternate sides, because g′(x∗)=−sin⁡x∗g'(x^*)=-\sin x^* is negative.
  2. From x(x+1)=x+2x(x+1)=x+2 we get x2=2x^2=2⁠‏, so x=2x=\sqrt2 is a fixed point. The derivative is g′(x)=−1(x+1)2g'(x)=-\dfrac{1}{(x+1)^2}⁠‏, and g′(2)=−1(2+1)2=−0.1716g'(\sqrt2)=-\dfrac{1}{(\sqrt2+1)^2}=-0.1716⁠‏. Each step multiplies the error by 0.17160.1716 and gains −log⁡100.1716=0.77-\log_{10}0.1716=0.77 digits, so a digit costs about 1.31.3 steps. The steps: x1=1.5x_1=1.5 (error 0.08580.0858⁠)‏, x2=1.4x_2=1.4 (error 0.01420.0142⁠)‏, x3=1.41667x_3=1.41667 (error 0.002450.00245⁠)‏. The ratios are 0.1660.166 and 0.1730.173⁠‏, close to the prediction 0.17160.1716⁠‏.
  3. With f(0)=−2f(0)=-2⁠, f(1)=−1f(1)=-1⁠, f(2)=2f(2)=2 the first term of the formula is 00 (since a=0a=0⁠)‏, the second is 1⋅(−2)⋅2(1)(−3)=1.3333\dfrac{1\cdot(-2)\cdot2}{(1)(-3)}=1.3333 and the third is 2⋅(−2)⋅(−1)(4)(3)=0.3333\dfrac{2\cdot(-2)\cdot(-1)}{(4)(3)}=0.3333⁠‏. So x=1.6667x=1.6667 (error 0.250.25⁠)‏.
  4. The secant step from 11 and 22 gives 2−2⋅13=1.33332-2\cdot\dfrac{1}{3}=1.3333 (error 0.0810.081⁠)‏. The first interpolation step is worse here, because the three points are spread widely; the next steps of interpolation catch up, as the lesson shows on the option problem.
  5. From x2−2=0x^2-2=0 we get x=x2+x−2x=x^2+x-2⁠‏, that is g(x)=x2+x−2g(x)=x^2+x-2 and g′(x)=2x+1g'(x)=2x+1⁠‏. So g′(2)=22+1=3.83g'(\sqrt2)=2\sqrt2+1=3.83 and the factor exceeds 1: the error grows about 3.83.8 times per step. From x0=1.5x_0=1.5 we get x1=1.75x_1=1.75 (error 0.3360.336⁠) and x2=2.8125x_2=2.8125 (error 1.401.40⁠), and then the iterates run away from the root.

Common mistakes

“Any way of writing f(x)=0f(x)=0 as x=g(x)x=g(x) will work.”

Why this is wrong

The three rules for x2−2=0x^2-2=0 in the lesson all have 2\sqrt2 as a fixed point, and only one converges fast.
What matters is ∣g′∣|g'| at the fixed point: below 1 converges, above 1 runs away, equal to 1 may cycle.

“If ∣g′∣<1|g'|<1 somewhere, the iteration converges.”

Why this is wrong

The condition is on the derivative at the fixed point, and the start must be close enough.
From a far start the iterates can leave the region where ∣g′∣<1|g'|<1 or reach a different fixed point.

“Stop when ∣xn−xn−1∣|x_n-x_{n-1}| is small, so the error is as small.”

Why this is incomplete

Small steps are a signal, not a bound. Also check ∣f(xn)∣|f(x_n)| against the tolerance, as in the course conventions, and always cap the number of iterations:
a cycling rule such as g(x)=2/xg(x)=2/x (which also follows from x2=2x^2=2⁠) never makes the step small.

“Inverse quadratic interpolation is always better than the secant method.”

Why this is wrong

Its order is higher (1.84 against 1.62), but it needs three distinct values of ff⁠, divides by their differences, and a badly spread triple can throw the next point far away.
That is why Brent’s method accepts such a step only if it falls inside the bracket and shrinks it enough.

Implement it yourself

Write a function that is called like this:

fixed_point(g, x0, tol=1e-10, max_iter=200)

It returns three values: the final value, the number of iterations, and True if it stopped because the step is at most tol or False if it stopped because of max_iter.
Stop when ∣xn−xn−1∣≤|x_n-x_{n-1}|\le tol, counting the first iteration as n=1n=1⁠‏, and stop after max_iter iterations even if the step is still large.

Python in this task. It asks you to write a whole function. The tools:
def (defines a function), range(1, max_iter + 1) (the numbers 1,2,…,1,2,\dots, max_iter), abs (absolute value), return (gives back values; you can return several, separated by commas), and for your tests lambda x: ... (a one-line function) and math.cos (the cosine, with the angle in radians).

Run the box below: it runs your function on the three rules and prints ok or FAIL for each.
Write the function in place of the TODO line.

import math

def fixed_point(g, x0, tol=1e-10, max_iter=200):
    # TODO: write the function here
    return None


h = lambda x: (x + 2) / (x + 1)
k = lambda x: 2 / x
checks = [
    ("cos from 1", fixed_point(math.cos, 1.0), (0.7390851332, 58, True)),
    ("(x+2)/(x+1) from 1", fixed_point(h, 1.0), (1.4142135624, 14, True)),
    ("2/x from 1, limit 20", fixed_point(k, 1.0, 1e-10, 20), (1.0, 20, False)),
]
for name, got, want in checks:
    good = (
        got is not None and got[1:] == want[1:]
        and abs(got[0] - want[0]) < 1e-9
    )
    print("ok  " if good else "FAIL", name, got)
ok   cos from 1 (0.7390851332451103, 58, True)
ok   (x+2)/(x+1) from 1 (1.4142135623637995, 14, True)
ok   2/x from 1, limit 20 (1.0, 20, False)
A solution
import math

def fixed_point(g, x0, tol=1e-10,
                max_iter=200):
    x = x0
    for n in range(1, max_iter + 1):
        y = g(x)
        if abs(y - x) <= tol:
            return y, n, True
        x = y
    return x, max_iter, False

h = lambda x: (x + 2) / (x + 1)
k = lambda x: 2 / x
print(fixed_point(math.cos, 1.0))
# (0.7390851332..., 58, True)
print(fixed_point(h, 1.0))
# (1.4142135623..., 14, True)
print(fixed_point(k, 1.0, 1e-10, 20))
# (1.0, 20, False)

The cosine’s factor is ∣g′(x∗)∣=sin⁡0.7391=0.6736|g'(x^*)|=\sin0.7391=0.6736 and the initial error is 0.2610.261⁠‏.
Reaching an error of 10−1010^{-10} takes about n≈ln⁡(10−10/0.261)/ln⁡0.6736≈55n\approx\ln(10^{-10}/0.261)/\ln0.6736\approx55 steps, and the run needs 58 iterations.
The rule with factor 0.17160.1716 needs 14. The rule 2/x2/x cycles between 1 and 2, so only the iteration limit stops it.
The last flag says that the iteration did not converge: it returns (1.0, 20, False).
The course conventions ask a program to report that the cap was reached and not to present the last iterate as the answer; the flag is that report, and the code that calls the function should check it before using the value.

Continue learning

Back to the lecture: Repeating a Rule.