---
title: "Lab: fixed points, operators and Fourier means in code"
heading: Stopping rules, Gronwall against reality, matrix norms, worst-case inputs and Fejér means
eyebrow: A14 · Lesson 51 · Lab: fixed points, operators and Fourier means
xp: 60
---

::: hook
The last third of the course moved into spaces of functions and operators: contractions and Picard iteration, Gronwall's inequality, operator norms, Fourier series and their Fejér means. In this lab you'll run each one. You'll stop a contraction with an a posteriori bound, measure how far two ODE solutions actually separate against Gronwall's guarantee, compute matrix norms three ways, build the input that drives a filter to its worst-case output, and compare Fejér means with plain partial sums at a jump.
:::

::: play
- A filter with impulse response $h[n] = 0.8^n\cos\frac{\pi n}{3}$ is driven by inputs bounded by 1.
- The worst input sets each sample to the sign of the matching tap, $x[n - k] = \text{sign}(h[k])$, so that every term in the convolution adds.
- The readout gives the resulting output, $\sum\lvert h[k]\rvert$, which you'll reproduce in the lab: no bounded input can do better.
:::

```sim plot
view: graph
x: n = 0..30
y: h = -1.05..1.05
plot: 0.8^n*cos(pi*n/3) | stems | label: h[n]
readout: worst-case output for inputs bounded by 1 = 3.5246 | digits: 5
```

::: predict
## Two solutions of $\dot y = \sin y + t$ start 0.01 apart. Gronwall allows their gap to reach 0.20 by t = 3. What does an accurate simulation show?

- [ ] About 0.20
- [x] About 0.012
- [ ] About 0.5
- [ ] Exactly 0.01

::: reveal
About 0.012. Gronwall uses the worst case of the Lipschitz constant, $\lvert\cos y\rvert \le 1$, at every moment. Along these particular solutions $\cos y$ is often small or negative, so the gap grows far less than $e^t$. The bound is guaranteed; the actual behavior is usually much better.
:::
:::

::: nameit
## A posteriori stopping

For a contraction with constant q, stop when $\frac{q}{1 - q}\lvert x_n - x_{n-1}\rvert$ falls below the tolerance: that bounds the true error of $x_n$ (lesson 33).

## Matrix norms in NumPy

`np.linalg.norm(A, 1)`, `np.linalg.norm(A, np.inf)` and `np.linalg.norm(A, 2)` give the induced 1-, ∞- and 2-norms (largest column sum, largest row sum, largest singular value).

## Worst-case inputs

For a convolution, the output sample $y[n] = \sum_kh[k]x[n - k]$ is largest when $x[n - k] = \text{sign}(h[k])$, which achieves $\sum\lvert h[k]\rvert$, the $\ell^1$ norm. That's the operator norm on bounded signals, constructed explicitly.

## Fejér means in code

Weight harmonic k by $1 - \frac kN$ (lesson 42) and sum. Compare maxima near a jump with the plain partial sum.
:::

::: example
## Iterate $T(x) = \frac12\cos x$ from 0 until two successive iterates differ by less than $10^{-10}$. How many steps, and how accurate is the result?

::: solution
1. **Iterate.** Sixteen steps reach $\lvert x_{16} - x_{15}\rvert < 10^{-10}$, at
   $$x = 0.4501836113$$
2. **The a posteriori bound.** With q = ½,
   $$\frac{q}{1 - q}\lvert x_n - x_{n-1}\rvert < 10^{-10}$$
   so the result is guaranteed to that accuracy.
3. **The actual rate.** It's better than q: near the fixed point
   $$\begin{aligned} \lvert T'\rvert &= \frac12\sin(0.45) \\ &= 0.22 \end{aligned}$$
   so each step gains about 0.66 digits.
4. **Compare the a priori bound.** It assumes the worst rate from the start, and would ask for 34 steps.

**Answer:** 16 steps, giving x = 0.4501836113, guaranteed to within $10^{-10}$.
:::
:::

::: example
## Compute $\lVert A\rVert_1$, $\lVert A\rVert_\infty$ and $\lVert A\rVert_2$ for $A = \begin{pmatrix}1 & -2\\ 3 & 4\end{pmatrix}$ with NumPy, and check them against lesson 38.

::: solution
1. **NumPy.** It gives 6, 7 and 5.11667.
2. **By hand.** The largest column sum is $\lvert -2\rvert + 4 = 6$, the largest row sum $3 + 4 = 7$, and the 2-norm is
   $$\sqrt{15 + \sqrt{125}} = 5.11667$$
3. **Compare with the spectral radius.** All three bound the spectral radius $\sqrt{10} = 3.162$ from above, as any operator norm must, and here the 2-norm is the tightest of them. For a symmetric matrix it would equal the spectral radius exactly.

**Answer:** $\lVert A\rVert_1 = 6$, $\lVert A\rVert_\infty = 7$ and $\lVert A\rVert_2 = 5.11667$, matching lesson 38.
:::
:::

::: check
given: q = 0.6 | 0.8; s = 0.00001 | 0.000001
answer: q/(1 - q)*s
tolerance: 0.5%

## A contraction with q = {q} just took a step of {s}. What bound does that give on the current error?

::: right
Right. $\frac{q}{1 - q}$ times the last step.
:::

::: misses
- [s] The remaining error can exceed the last step when q is near 1.
- [any] $\frac{q}{1 - q}\lvert x_n - x_{n-1}\rvert$.
:::
:::

::: check
given: d = 0.01 | 0.001; L = 1 | 2; t = 2 | 3
answer: d*exp(L*t)
tolerance: 0.5%

## Gronwall's bound for two solutions starting {d} apart, Lipschitz constant {L}, at t = {t}?

::: right
Right. $de^{Lt}$: guaranteed, though real separations are often much smaller.
:::

::: misses
- [any] $de^{Lt}$.
:::
:::

::: check
kind: python

## Iterate $T(x) = \frac12\cos x$ from 0 until $\lvert x_n - x_{n-1}\rvert < 10^{-10}$. Set `steps` and `x`.

```python starter
import math

steps, x = 0, 0.0
print(steps, x)
```

```python solution
import math

x, steps = 0.0, 0
while True:
    new = 0.5 * math.cos(x)
    steps += 1
    if abs(new - x) < 1e-10:
        x = new
        break
    x = new
print(steps, x)
```

```python tests
assert steps == 16, f"Sixteen steps reach a step below 1e-10; got {steps}."
assert abs(x - 0.4501836113) < 1e-9, f"The fixed point is 0.4501836113; got {x}."
```

::: right
Right. Sixteen steps to 0.4501836113, guaranteed by the a posteriori bound.
:::
:::

::: check
kind: python

## Write `rk4(f, y, T, steps)` for a scalar ODE from t = 0. Integrate $\dot y = \sin y + t$ to T = 3 with 3000 steps from y(0) = 0 and from y(0) = 0.01. Set `gap` to their final difference and `bound` to Gronwall's $0.01e^3$.

```python starter
import math

def rk4(f, y, T, steps):
    return y

gap = 0.0
bound = 0.01 * math.e**3
print(gap, bound)
```

```python solution
import math

def rk4(f, y, T, steps):
    h, t = T / steps, 0.0
    for _ in range(steps):
        k1 = f(t, y)
        k2 = f(t + h / 2, y + h / 2 * k1)
        k3 = f(t + h / 2, y + h / 2 * k2)
        k4 = f(t + h, y + h * k3)
        y += h / 6 * (k1 + 2 * k2 + 2 * k3 + k4)
        t += h
    return y

f = lambda t, y: math.sin(y) + t
gap = abs(rk4(f, 0.01, 3, 3000) - rk4(f, 0.0, 3, 3000))
bound = 0.01 * math.e**3
print(gap, bound)
```

```python tests
assert abs(gap - 0.011786) < 1e-4, f"The solutions end about 0.0118 apart; got {gap}."
assert gap < bound, "The actual gap must respect Gronwall's bound."
```

::: right
Right. 0.0118 against the bound 0.2009: the worst case almost never happens along a particular pair of solutions.
:::
:::

::: check
kind: python

## For $A = \begin{pmatrix}1 & -2\\ 3 & 4\end{pmatrix}$, set `n1`, `ninf` and `n2` with `np.linalg.norm`, and `rho` to the largest absolute eigenvalue.

```python starter
import numpy as np

A = np.array([[1.0, -2.0], [3.0, 4.0]])
n1 = ninf = n2 = rho = 0.0
print(n1, ninf, n2, rho)
```

```python solution
import numpy as np

A = np.array([[1.0, -2.0], [3.0, 4.0]])
n1 = np.linalg.norm(A, 1)
ninf = np.linalg.norm(A, np.inf)
n2 = np.linalg.norm(A, 2)
rho = max(abs(np.linalg.eigvals(A)))
print(n1, ninf, n2, rho)
```

```python tests
import math
assert (n1, ninf) == (6.0, 7.0), f"Column and row sums give 6 and 7; got {n1}, {ninf}."
assert abs(n2 - 5.116673) < 1e-6, f"The 2-norm is 5.11667; got {n2}."
assert abs(rho - math.sqrt(10)) < 1e-12 and rho < min(n1, ninf, n2), "The spectral radius sqrt(10) is below every operator norm."
```

::: right
Right. 6, 7 and 5.117, all above the spectral radius 3.162.
:::
:::

::: check
kind: python

## For $h[n] = 0.8^n\cos\frac{\pi n}{3}$, n = 0 to 59, build the worst-case input (the signs of h, reversed in time) and set `y` to the output it produces at the last moment, $\sum_kh[k]\,\text{sign}(h[k])$; set `l1` to $\sum\lvert h\rvert$.

```python starter
import numpy as np

h = 0.8**np.arange(60) * np.cos(np.pi * np.arange(60) / 3)
y = l1 = 0.0
print(y, l1)
```

```python solution
import numpy as np

h = 0.8**np.arange(60) * np.cos(np.pi * np.arange(60) / 3)
x = np.sign(h[::-1])
y = np.sum(h * x[::-1])
l1 = np.sum(np.abs(h))
print(y, l1)
```

```python tests
assert abs(y - l1) < 1e-12, "The sign-matched input achieves the l1 norm exactly."
assert abs(l1 - 3.524585) < 1e-5, f"The l1 norm is 3.5246; got {l1}."
```

::: right
Right. 3.5246: the worst case is achieved, so it's the exact operator norm on bounded inputs.
:::
:::

::: check
kind: python

## On 20 001 points of (0, π], set `partial` to the largest value of the square wave's partial sum $\frac4\pi\sum_{k\text{ odd} < 50}\frac{\sin kx}{k}$ and `fejer` to the largest value with each term weighted by $1 - \frac{k}{50}$.

```python starter
import numpy as np

partial = fejer = 1.0
print(partial, fejer)
```

```python solution
import numpy as np

x = np.linspace(0.001, np.pi, 20001)
partial = max(sum(4 / (np.pi * k) * np.sin(k * x) for k in range(1, 50, 2)))
fejer = max(sum((1 - k / 50) * 4 / (np.pi * k) * np.sin(k * x) for k in range(1, 50, 2)))
print(partial, fejer)
```

```python tests
assert abs(partial - 1.1791) < 1e-3, f"The partial sum overshoots to 1.179; got {partial}."
assert fejer < 1, f"The Fejer mean stays below 1; got {fejer}."
```

::: right
Right. 1.179 against 0.987: the tapered sum doesn't overshoot.
:::
:::

::: explain
## Why is Gronwall's bound usually far from the actual separation of two solutions?

::: model
Gronwall's inequality replaces the rate at which two solutions separate by its largest possible value, the Lipschitz constant, at every instant and along every path. Real solutions spend much of their time where the local rate, here $\cos y$, is smaller, and sometimes where it's negative, so that neighboring solutions are pulled together. The exponential bound compounds the worst case over the whole interval, so the overestimate itself grows exponentially. The bound is valuable because it's always true and needs only one number; sharper estimates come from looking at the actual local rates, as the contracting version and Lyapunov exponents do.
:::
:::

::: practice

::: check
given: q = 0.3 | 0.9; s = 0.0001 | 0.00000001
answer: q/(1 - q)*s
tolerance: 0.5%

## A posteriori bound, q = {q}, last step {s}?

::: misses
- [any] $\frac{q}{1 - q}s$.
:::
:::
:::
