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 ;
- 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 :
roughly, each step multiplies the error by it.
- For and compute , and . The fixed point is . What are the errors of the three steps?
- The rule also has the fixed point . Check this, compute and predict how many steps one more correct digit costs. Then run three steps from and compare.
- For use the three points and do one step of inverse quadratic interpolation by hand.
- Do one secant step from the points and and compare with the result of question 3.
- Add to both sides of and write it as . Compute and predict what the iteration does from . Check with two steps.
Hint for question 2
A number is a fixed point if . Multiply both sides by .
For the derivative, use .
Hint for question 3
The formula for inverse quadratic interpolation is on the formula sheet.
Substitute , and .
Solutions
- (error ), (error ), (error ). The error shrinks but the iterates alternate sides, because is negative.
- From we get , so is a fixed point. The derivative is , and . Each step multiplies the error by and gains digits, so a digit costs about steps. The steps: (error ), (error ), (error ). The ratios are and , close to the prediction .
- With , , the first term of the formula is (since ), the second is and the third is . So (error ).
- The secant step from and gives (error ). 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.
- From we get , that is and . So and the factor exceeds 1: the error grows about times per step. From we get (error ) and (error ), and then the iterates run away from the root.
Common mistakes
“Any way of writing as will work.”
Why this is wrong
The three rules for in the lesson all have as a fixed point, and only one converges fast.
What matters is at the fixed point: below 1 converges, above 1 runs away, equal to 1 may cycle.
“If 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 or reach a different fixed point.
“Stop when is small, so the error is as small.”
Why this is incomplete
Small steps are a signal, not a bound. Also check against the tolerance, as in the course conventions, and always cap the number of iterations:
a cycling rule such as (which also follows from ) 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 , 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 tol, counting the first iteration as , 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 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 and the initial error is .
Reaching an error of takes about steps, and the run needs 58 iterations.
The rule with factor needs 14. The rule 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.