---
title: "Lab: sequences of functions in code"
heading: Sup norms, Bernstein rates, equicontinuity, M-test tails and radii of convergence
eyebrow: A14 · Lesson 30 · Lab: sequences of functions in code
xp: 60
---

::: hook
Lessons 25 to 29 dealt with sequences of functions: uniform convergence, what it preserves, power series, Weierstrass approximation and equicontinuity. Each comes down to measuring a sup norm or estimating a rate. In this lab you'll compute sup norms on fine grids and compare them with formulas, measure how fast Bernstein polynomials converge on a function with a corner, watch equicontinuity fail, compare the Weierstrass M-test's bound with the true tail, and estimate a radius of convergence from coefficients.
:::

::: play
- The curve is the error of the degree-n Bernstein polynomial of $\lvert x - \frac12\rvert$, at its worst point $x = \frac12$, against n, on log axes.
- It falls like $\frac{1}{\sqrt{2\pi n}}$, much more slowly than for smooth functions, whose Bernstein error falls like $\frac1n$ (dashed, for $x^2$).
- The corner at ½ is what slows it: Bernstein polynomials average the function over a binomial spread of width about $\frac{1}{2\sqrt n}$.
:::

```sim plot
view: graph
x: n = 4..10000 log
y: err = 0.0001..0.3 log
plot: 1/sqrt(2*pi*n) | label: corner, |x − ½|
plot: 1/(4*n) | label: smooth, x² | color: ch3 | dashed
```

::: predict
## The Bernstein approximation of $\lvert x - \frac12\rvert$ with n = 256 has error 0.025 at x = ½. About what n gives 0.0125?

- [ ] 512
- [x] 1024
- [ ] 2048
- [ ] 65 536

::: reveal
About 1024: the error falls like $\frac{1}{\sqrt n}$, so halving it takes four times the degree. For a smooth function, doubling n would have been enough. Bernstein polynomials converge for every continuous function, but they don't speed up to match smoothness the way Chebyshev approximations do.
:::
:::

::: nameit
## Sup norms on a grid

$\lVert f\rVert_\infty$ on [a, b] is approximated by the largest $\lvert f\rvert$ on a fine grid. For smooth f the grid estimate is accurate to about $h^2\max\lvert f''\rvert/8$; near sharp peaks use a finer grid or locate the peak exactly.

## Bernstein polynomials in code

$B_nf(x) = \sum_{k=0}^n f\left(\frac kn\right)\binom nkx^k(1 - x)^{n-k}$. `math.comb` gives the binomial coefficients. For large n, evaluate in floating point with care: the terms are a binomial distribution's probabilities.

## Tails and radii

The Weierstrass M-test bounds the tail $\sum_{k>N}$ by $\sum_{k>N}M_k$; the true sup of the tail is often smaller. A radius of convergence can be estimated from $\lvert c_k\rvert^{-1/k}$, but slowly: factors like $k^2$ in the coefficients contribute $k^{-2/k}$, which approaches 1 only gradually.
:::

::: example
## Compare the grid sup of $x^n(1 - x)$ on [0, 1] with the formula, for n = 5, 10 and 50.

::: solution
1. **Compute both.** On 100 001 points, the grid maxima and the formula $\frac{1}{n + 1}\left(\frac{n}{n + 1}\right)^n$ give:

| n | Grid maximum | Formula |
|---|---|---|
| 5 | 0.0669796 | 0.0669796 |
| 10 | 0.0350494 | 0.0350494 |
| 50 | 0.0072849 | 0.0072849 |

2. **Why they agree.** They agree to the digits shown because the peak is smooth and the grid is fine.
3. **The trend.** The decay, roughly $\frac{1}{e(n + 1)}$, confirms the uniform convergence to 0 from lesson 25.

**Answer:** the grid and the formula agree to the digits shown: 0.0669796, 0.0350494 and 0.0072849.
:::
:::

::: example
## Estimate the radius of convergence of $\sum\frac{k^2x^k}{3^k}$ from $\lvert c_k\rvert^{-1/k}$ at k = 100 and k = 300.

::: solution
1. **The root estimate.**
   $$c_k^{-1/k} = 3k^{-2/k}$$
2. **Evaluate it.** At k = 100 that's 2.736 and at k = 300 it's 2.888.
3. **Why it's low.** Both are still well below the true radius, 3, because $k^{-2/k} = e^{-2\ln k/k}$ approaches 1 slowly.
4. **Faster estimators.** The root test is exact in the limit, but numerically it's a slow estimator. When the coefficients have a known form, ratios
   $$\frac{c_k}{c_{k+1}} = 3\left(\frac{k}{k + 1}\right)^2$$
   converge faster, and extrapolating in $\frac1k$ helps more.

**Answer:** the estimate is 2.736 at k = 100 and 2.888 at k = 300, still well below the true radius, 3.
:::
:::

::: check
given: n = 16 | 100 | 1000
answer: 1/sqrt(2*pi*n)
tolerance: 3%

## About how large is the Bernstein error of $\lvert x - \frac12\rvert$ at x = ½ for degree {n}?

::: right
Right. About $\frac{1}{\sqrt{2\pi n}}$: the mean absolute deviation of a binomial proportion.
:::

::: misses
- [1/(4*n)] That's the rate for a smooth function like $x^2$; the corner slows it to $\frac{1}{\sqrt n}$.
- [any] $E\left\lvert\frac Sn - \frac12\right\rvert$ for S binomial with p = ½.
:::
:::

::: check
given: k = 50 | 100 | 300
answer: 3*k^(-2/k)
tolerance: 0.5%

## What does $\lvert c_k\rvert^{-1/k}$ give for $c_k = \frac{k^2}{3^k}$ at k = {k}?

::: right
Right. $3k^{-2/k}$, approaching the true radius 3 slowly.
:::

::: misses
- [3] That's the limit; at finite k the factor $k^{-2/k}$ still pulls it down.
- [any] $\left(\frac{k^2}{3^k}\right)^{-1/k}$.
:::
:::

::: check
kind: python

## Set `sups` to the grid maxima of $x^n(1 - x)$ on 100 001 points of [0, 1] for n = 5, 10, 50, and `formula` to $\frac{1}{n + 1}\left(\frac{n}{n + 1}\right)^n$ for the same n.

```python starter
import numpy as np

sups = formula = [0.0, 0.0, 0.0]
print(sups, formula)
```

```python solution
import numpy as np

x = np.linspace(0, 1, 100001)
ns = (5, 10, 50)
sups = [np.max(x**n * (1 - x)) for n in ns]
formula = [(n / (n + 1))**n / (n + 1) for n in ns]
print(sups, formula)
```

```python tests
assert all(abs(a - b) < 1e-8 for a, b in zip(sups, formula)), f"The grid sups should match the formula; got {sups} and {formula}."
assert abs(formula[1] - 0.0350494) < 1e-7, "For n = 10 the sup is 0.0350494."
```

::: right
Right. Grid and formula agree to eight digits: 0.06698, 0.03505, 0.00728.
:::
:::

::: check
kind: python

## Write `bern(f, n, xs)` evaluating the degree-n Bernstein polynomial of f at the points xs (use `math.comb`). For $f = \lvert x - \frac12\rvert$, set `errs` to the largest errors on 401 points of [0, 1] for n = 16, 64, 256.

```python starter
import math
import numpy as np

def bern(f, n, xs):
    return f(xs)

xs = np.linspace(0, 1, 401)
errs = [np.max(np.abs(bern(lambda u: np.abs(u - 0.5), n, xs) - np.abs(xs - 0.5))) for n in (16, 64, 256)]
print(errs)
```

```python solution
import math
import numpy as np

def bern(f, n, xs):
    k = np.arange(n + 1)
    c = np.array([math.comb(n, j) for j in k], dtype=float)
    return np.array([np.sum(f(k / n) * c * t**k * (1 - t)**(n - k)) for t in xs])

xs = np.linspace(0, 1, 401)
errs = [np.max(np.abs(bern(lambda u: np.abs(u - 0.5), n, xs) - np.abs(xs - 0.5))) for n in (16, 64, 256)]
print(errs)
```

```python tests
assert abs(errs[0] - 0.09819) < 1e-4, f"With n = 16 the error is 0.0982; got {errs[0]}."
assert 1.9 < errs[0] / errs[1] < 2.1 and 1.9 < errs[1] / errs[2] < 2.1, f"Quadrupling n should halve the error; got {errs}."
```

::: right
Right. 0.098, 0.050, 0.025: each fourfold increase in degree halves the error.
:::
:::

::: check
kind: python

## For $x^n$ at x = 1 with ε = ½, the largest δ is $1 - 2^{-1/n}$. Set `deltas` for n = 10, 100, 1000, and `prod` to n times δ for n = 1000.

```python starter
deltas = [1.0, 1.0, 1.0]
prod = 0.0
print(deltas, prod)
```

```python solution
import math

deltas = [1 - 0.5**(1 / n) for n in (10, 100, 1000)]
prod = 1000 * deltas[2]
print(deltas, prod)
```

```python tests
import math
assert abs(deltas[0] - 0.066967) < 1e-6, f"For n = 10, delta is 0.06697; got {deltas[0]}."
assert abs(prod - math.log(2)) < 1e-3, f"n times delta approaches ln 2 = 0.693; got {prod}."
```

::: right
Right. δ shrinks like $\frac{\ln 2}{n}$: no single δ serves the whole family.
:::
:::

::: check
kind: python

## Compare the M-test bound with the actual tail of $\sum\frac{\sin kx}{k^2}$. On 2001 points of [0, 2π], set `actual` to the largest $\left\lvert\sum_{k=51}^{5000}\frac{\sin kx}{k^2}\right\rvert$ and `bound` to $\frac{1}{50}$, the bound from $\sum_{k>N}\frac{1}{k^2} < \frac1N$.

```python starter
import numpy as np

actual = bound = 0.0
print(actual, bound)
```

```python solution
import numpy as np

x = np.linspace(0, 2 * np.pi, 2001)
k = np.arange(51, 5001)
actual = max(abs(np.sum(np.sin(k * t) / k**2)) for t in x)
bound = 1 / 50
print(actual, bound)
```

```python tests
assert actual < bound, f"The actual tail must be within the bound; got {actual} against {bound}."
assert 0.008 < actual < 0.014, f"The actual sup is about 0.011; got {actual}."
```

::: right
Right. About 0.011 against the bound 0.02: the M-test ignores cancellation between terms, so it overestimates by about half here.
:::
:::

::: check
kind: python

## Set `r100` and `r300` to $c_k^{-1/k}$ for $c_k = \frac{k^2}{3^k}$ at k = 100 and 300.

```python starter
r100 = r300 = 3.0
print(r100, r300)
```

```python solution
c = lambda k: k**2 / 3.0**k
r100 = c(100)**(-1 / 100)
r300 = c(300)**(-1 / 300)
print(r100, r300)
```

```python tests
assert abs(r100 - 2.73603) < 1e-4 and abs(r300 - 2.88807) < 1e-4, f"Expect 2.736 and 2.888; got {r100}, {r300}."
assert r100 < r300 < 3, "The estimates should climb toward 3 from below."
```

::: right
Right. 2.736 and 2.888, creeping toward the true radius 3.
:::
:::

::: explain
## Why do Bernstein polynomials converge slowly near a corner?

::: model
The Bernstein polynomial at x is an average of the function's values at the points $\frac kn$, weighted by the binomial probabilities for n trials with success probability x. Those weights spread over a width of about $\sqrt{\frac{x(1 - x)}{n}}$ around x. For a smooth function, averaging over that width changes the value by an amount proportional to the curvature times the variance, about $\frac1n$. At a corner the function isn't locally linear, so the average differs from the value by an amount proportional to the width itself, about $\frac{1}{\sqrt n}$. Halving the error then needs four times the degree.
:::
:::

::: practice

::: check
given: n = 10 | 50 | 100 | 400 | 1000 | 2500
answer: 1/sqrt(2*pi*n)
tolerance: 3%

## Bernstein error at the corner, degree {n}?

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

::: check
given: k = 10 | 20 | 50 | 100 | 500 | 1000
answer: 3*k^(-2/k)
tolerance: 0.5%

## Root estimate of the radius at k = {k}?

::: misses
- [any] $3k^{-2/k}$.
:::
:::
:::
