---
title: "Analysis lab: convergence made visible"
heading: Sup norms, Picard iterates and Fourier errors, computed and checked
eyebrow: "A14 · Lesson 50 · Analysis lab"
xp: 60
---

::: hook
Analysis proves things about limits; computation lets you watch them happen. In this lab you'll measure sup norms to tell uniform from pointwise convergence, run Picard's iteration on a grid and watch it build $e^t$, and compute the $L^2$ error of a square wave's Fourier sums two ways, by direct integration and by Parseval's identity, to see them agree while the Gibbs overshoot refuses to shrink. Each task checks the computation against the exact value the theory predicts.
:::

::: predict
## On a grid of 2001 points over [0, 1], how close can a computed sup norm of $x/(1 + 100x^2)$ get to the true value 0.05?

- [x] Very close: the peak at x = 0.1 is smooth, so a fine grid misses it by a tiny amount
- [ ] Only within 10%
- [ ] Exactly, always
- [ ] Not at all: sups can't be computed

::: reveal
Very close. Near a smooth maximum the function is flat, so missing the peak by a grid spacing h costs only about $\frac12\lvert f''\rvert h^2$. Here the grid even contains x = 0.1, so the computed value is exact to rounding. Sharp features (like the Gibbs peak) need finer grids.
:::
:::

::: nameit
## What you'll write

1. `sup_norm(g, a, b, n=2001)`: the largest $\lvert g(x)\rvert$ on n equally spaced points of [a, b], using NumPy.
2. `picard(f, y0, T, iters, n=2001)`: Picard's iteration $y_{k+1}(t) = y_0 + \int_0^tf(s, y_k(s))\,ds$ on a grid of n points over [0, T], starting from the constant $y_0$, with the integral done by the cumulative trapezoid rule. Return the list of iterates (arrays), first one included.
3. `square_errors(K, n=20001)`: for the ±1 square wave on [−π, π] and its Fourier sum over odd harmonics up to K, return `(l2_direct, l2_parseval, peak)`: the $L^2$ error by numerical integration of the squared difference, the same by Parseval, $\sqrt{2\pi - \frac{16}{\pi}\sum_{k\text{ odd}\le K}\frac{1}{k^2}}$, and the largest value of the sum.

## What to expect

- $\sup_{[0, 0.9]}x^n = 0.9^n$, and $\sup\frac{x}{1 + nx^2} = \frac{1}{2\sqrt n}$ (lesson 25).
- Picard's iterates for $\dot y = y$ are the Taylor polynomials of $e^t$ (lesson 34).
- The two $L^2$ errors agree; the peak stays near 1.179 (lesson 40).
:::

::: example
## How accurate is the cumulative trapezoid rule for $\int_0^1e^s\,ds$ on 2001 points?

::: solution
1. **The step.** The step is h = 0.0005.
2. **The trapezoid error.** It's about
   $$\frac{h^2}{12}(e - 1) = 3.6 \times 10^{-8}$$
3. **Compare with the Picard errors.** That's far below the Picard iterates' own errors for the first ten or so iterations, so the grid doesn't spoil the comparison with the theory.

**Answer:** the error is about $3.6 \times 10^{-8}$, too small to spoil the comparison with the theory.
:::
:::

::: example
## What should the $L^2$ error be for the square wave summed through K = 25?

::: solution
1. **The squared error.**
   $$\begin{aligned} \text{error}^2 &= 2\pi - \frac{16}{\pi}\sum_{k\text{ odd}\le 25}\frac{1}{k^2} \\ &= 6.2832 - 5.0930\times 1.2145 \\ &= 0.0979 \end{aligned}$$
2. **Take the root.**
   $$\sqrt{0.0979} = 0.313$$
3. **Check.** Your direct integral should agree to about three digits on a fine grid.

**Answer:** the $L^2$ error should be 0.313.
:::
:::

::: check
kind: python

## Step 1. Write `sup_norm(g, a, b, n=2001)`.

```python starter
import numpy as np

def sup_norm(g, a, b, n=2001):
    x = np.linspace(a, b, n)
    return 0.0

print(sup_norm(lambda x: x / (1 + 100 * x**2), 0, 1))
```

```python solution
import numpy as np

def sup_norm(g, a, b, n=2001):
    x = np.linspace(a, b, n)
    return float(np.max(np.abs(g(x))))

print(sup_norm(lambda x: x / (1 + 100 * x**2), 0, 1))
```

```python tests
import numpy as np
for n in (5, 20, 80):
    s = sup_norm(lambda x: x**n, 0, 0.9)
    assert abs(s - 0.9**n) < 1e-12, f"sup of x^{n} on [0, 0.9] is 0.9^{n} = {0.9**n:.6g}; got {s:.6g}."
for n in (4, 100, 2500):
    s = sup_norm(lambda x: x / (1 + n * x**2), 0, 1, 20001)
    assert abs(s - 1 / (2 * np.sqrt(n))) < 1e-4, f"sup of x/(1 + {n}x²) is {1 / (2 * np.sqrt(n)):.5f}; got {s:.5f}."
s = sup_norm(lambda x: -3 * np.ones_like(x), 0, 1)
assert abs(s - 3) < 1e-12, "Use the absolute value: the sup norm of −3 is 3."
```

::: right
Right. $x^n$ on [0, 0.9] and $\frac{x}{1 + nx^2}$ both converge uniformly, and the computed sups shrink exactly as the theory says.
:::
:::

::: check
kind: python

## Step 2. Write `picard(f, y0, T, iters, n=2001)` returning the list of iterates.

```python starter
import numpy as np

def picard(f, y0, T, iters, n=2001):
    t = np.linspace(0, T, n)
    ys = [np.full(n, float(y0))]
    # each step: integrate f(t, y_k) cumulatively with the trapezoid rule and add y0
    return ys

ys = picard(lambda t, y: y, 1.0, 1.0, 8)
print(len(ys), ys[-1][-1])
```

```python solution
import numpy as np

def picard(f, y0, T, iters, n=2001):
    t = np.linspace(0, T, n)
    h = t[1] - t[0]
    ys = [np.full(n, float(y0))]
    for _ in range(iters):
        g = f(t, ys[-1])
        integral = np.concatenate(([0.0], np.cumsum((g[1:] + g[:-1]) * h / 2)))
        ys.append(y0 + integral)
    return ys

ys = picard(lambda t, y: y, 1.0, 1.0, 8)
print(len(ys), ys[-1][-1])
```

```python tests
import math
import numpy as np
ys = picard(lambda t, y: y, 1.0, 1.0, 8)
assert len(ys) == 9, "Return the starting function and all 8 iterates: 9 arrays."
for k in (1, 2, 3, 6):
    taylor = sum(1 / math.factorial(j) for j in range(k + 1))
    assert abs(ys[k][-1] - taylor) < 1e-6, f"Iterate {k} at t = 1 should be the Taylor sum {taylor:.7f}; got {ys[k][-1]:.7f}."
assert abs(ys[8][-1] - math.e) < 1e-5, f"Iterate 8 should be within 1e-5 of e; got {ys[8][-1]:.7f}."
zs = picard(lambda t, y: -2 * t * y, 1.0, 1.0, 12)
assert abs(zs[-1][-1] - math.exp(-1)) < 1e-5, f"For y' = −2ty the solution is exp(−t²); at t = 1, {math.exp(-1):.6f}. Got {zs[-1][-1]:.6f}."
```

::: right
Right. Each iterate adds the next Taylor term of $e^t$, and the same code solves $\dot y = -2ty$, whose solution $e^{-t^2}$ it reaches in a dozen iterations.
:::
:::

::: check
kind: python

## Step 3. Write `square_errors(K, n=20001)` returning `(l2_direct, l2_parseval, peak)`.

```python starter
import numpy as np

def square_errors(K, n=20001):
    x = np.linspace(-np.pi, np.pi, n)
    f = np.sign(x)
    # partial sum over odd k up to K, then the two L2 errors and the peak
    return 0.0, 0.0, 0.0

print(square_errors(25))
```

```python solution
import numpy as np

def square_errors(K, n=20001):
    x = np.linspace(-np.pi, np.pi, n)
    f = np.sign(x)
    ks = np.arange(1, K + 1, 2)
    s = 4 / np.pi * np.sum(np.sin(np.outer(ks, x)) / ks[:, None], axis=0)
    d2 = (s - f) ** 2
    h = x[1] - x[0]
    direct = np.sqrt(np.sum((d2[1:] + d2[:-1]) * h / 2))
    parseval = np.sqrt(2 * np.pi - 16 / np.pi * np.sum(1.0 / ks**2))
    return float(direct), float(parseval), float(np.max(s))

print(square_errors(25))
```

```python tests
import numpy as np
for K in (5, 25, 99):
    d, p, peak = square_errors(K)
    assert abs(d - p) < 2e-3, f"For K = {K} the direct and Parseval errors should agree; got {d:.5f} and {p:.5f}."
d25, p25, _ = square_errors(25)
assert abs(p25 - 0.3129) < 5e-4, f"Through K = 25 the L2 error should be about 0.313; got {p25:.4f}."
_, _, peak = square_errors(99)
assert abs(peak - 1.179) < 3e-3, f"The Gibbs peak should stay near 1.179; got {peak:.4f}."
d5, _, _ = square_errors(5)
d99, _, _ = square_errors(99)
assert d99 < d5 / 3, "The L2 error should shrink as K grows."
```

::: right
Right. The energy of the error shrinks steadily and matches Parseval, while the peak sits at 1.179 whatever K is: convergence in $L^2$ without uniform convergence.
:::
:::

::: check
given: n = 9 | 49 | 400
answer: 1/(2*sqrt(n))
tolerance: 0.1%

## What sup norm should your `sup_norm` report for $\frac{x}{1 + \given{n}x^2}$ on [0, 1]?

::: right
Right. $\frac{1}{2\sqrt n}$ = {answer}, at $x = 1/\sqrt n$.
:::

::: misses
- [1/sqrt(n)] That's where the peak is; its height is half of that.
- [any] Not yet. The maximum of $\frac{x}{1 + nx^2}$.
:::
:::

::: check
given: K = 3 | 7 | 11
answer: sqrt(2*pi - 16/pi*(1 + 1/9 + step(K - 4)/25 + step(K - 6)/49 + step(K - 8)/81 + step(K - 10)/121))
tolerance: 0.1%

## By Parseval, what's the $L^2$ error of the square wave's sum through harmonic K = {K}?

::: right
Right. {answer}.
:::

::: misses
- [any] Not yet. $\sqrt{2\pi - \frac{16}{\pi}\sum_{k\text{ odd}\le K}\frac1{k^2}}$.
:::
:::

::: explain
## Why do the $L^2$ error and the Gibbs peak tell different stories about the same Fourier sums?

::: model
The $L^2$ error adds up the squared difference over the whole interval, so it measures how much energy is missing; the peak measures the single worst value. As more harmonics are added, the partial sums match the square wave better almost everywhere, and the ringing near the jumps squeezes into narrower regions, so the integrated error shrinks toward zero. But in each of those narrowing regions the sum still overshoots by about 9% of the jump, so the worst-case error never shrinks. Computing both shows directly that the series converges in energy but not uniformly.
:::
:::

::: practice

::: check
given: n = 4 | 16 | 100 | 400 | 1600 | 10000
answer: 1/(2*sqrt(n))
tolerance: 0.1%

## Sup of $\frac{x}{1 + \given{n}x^2}$?

::: misses
- [any] $\frac{1}{2\sqrt n}$.
:::
:::

::: check
given: k = 2..10 int
answer: 1/k!
tolerance: 0.1%

## The Picard iterate $y_{\given{k}}$ for $\dot y = y$ adds which last term at t = 1?

::: misses
- [any] $\frac{t^k}{k!}$ at t = 1.
:::
:::

::: check
given: K = 1 | 3 | 5 | 7 | 9 | 11 | 13
answer: sqrt(2*pi - 16/pi*(1 + step(K - 2)/9 + step(K - 4)/25 + step(K - 6)/49 + step(K - 8)/81 + step(K - 10)/121 + step(K - 12)/169))
tolerance: 0.1%

## Parseval $L^2$ error through K = {K}?

::: misses
- [any] $\sqrt{2\pi - \frac{16}{\pi}\sum\frac{1}{k^2}}$.
:::
:::
:::
