---
title: The Weierstrass approximation theorem
heading: Every continuous function on an interval is nearly a polynomial
eyebrow: A14 · Lesson 28 · The Weierstrass approximation theorem
xp: 40
---

::: hook
Polynomials are what computers evaluate best: a few multiplications and additions. The **Weierstrass approximation theorem** guarantees they're enough: any continuous function on a closed interval can be approximated by a polynomial to any accuracy you want, uniformly, at every point at once. Bernstein's proof is constructive and has a probabilistic heart: average the function's values at k/n with binomial weights, and the law of large numbers does the rest. It's slow in practice, but the theorem is why polynomial and Chebyshev approximations in function libraries, filter design and sensor calibration can always succeed.
:::

::: play
- The solid curve is $\cos(\omega x)$ on [0, 1]. The dashed one is its Bernstein polynomial of degree n, $B_nf(x) = \sum_{k=0}^nf\!\left(\frac kn\right)\binom nkx^k(1 - x)^{n-k}$.
- Raise n: the polynomial closes in everywhere at once, but slowly. The error behaves like $\frac{\lVert f''\rVert}{8n}$ (readout), so 10 times the degree buys only 10 times the accuracy.
- Raise ω: the function wiggles more, f″ is larger, and much higher degree is needed.
:::
:::

```sim plot
view: graph
x: x = 0..1
y: y = -1.3..1.3
slider: n = 1..200 step 1 = 20
slider: w = 1..12 step 0.5 = 5
plot: cos(w*x) | label: cos ωx
plot: Re((1 - x + x*exp(j*w/n))^n) | samples: 800 | dashed | label: Bernstein polynomial, degree n
readout: error bound ‖f″‖/(8n) = w^2/(8*n)
```

::: predict
## The Bernstein polynomial of $f(x) = x^2$ is $B_n f(x) = x^2 + \frac{x(1 - x)}{n}$. What's its largest error on [0, 1]?

- [ ] $\frac1n$
- [x] $\frac{1}{4n}$
- [ ] $\frac{1}{n^2}$
- [ ] 0

::: reveal
$\frac{1}{4n}$, at x = ½, where $x(1 - x)$ is largest. The error falls only as $\frac1n$, even for a function as smooth as $x^2$: Bernstein polynomials prove the theorem but aren't how you'd build an approximation in practice.
:::
:::

::: nameit
## The theorem

If f is continuous on [a, b], then for every ε > 0 there's a polynomial p with $\lvert f(x) - p(x)\rvert < \varepsilon$ for all x in [a, b].

## Bernstein's proof

On [0, 1], let $B_nf(x) = \sum_{k=0}^nf(k/n)\binom nkx^k(1 - x)^{n-k}$. The weights are the binomial probabilities for n trials with success probability x, so $B_nf(x)$ is the expected value of $f(K/n)$ with K the number of successes. As n grows, K/n concentrates near x (its variance is $\frac{x(1 - x)}{n}$), and uniform continuity of f makes $f(K/n)$ close to f(x). For f with a bounded second derivative,

$$\lvert B_nf(x) - f(x)\rvert \le \frac{\lVert f''\rVert_\infty\,x(1 - x)}{2n} \le \frac{\lVert f''\rVert_\infty}{8n}$$

Bernstein polynomials keep the endpoint values ($B_nf(0) = f(0)$, $B_nf(1) = f(1)$), reproduce straight lines exactly, and never overshoot: if f is between m and M, so is $B_nf$.

## In practice

For a smooth function, Chebyshev interpolation (A13, lesson 11) or the minimax polynomial converge far faster, often exponentially, which is how library functions like sin and exp are computed. **Stone–Weierstrass** generalizes the theorem: trigonometric polynomials approximate any continuous periodic function uniformly, for instance by the averaged (Fejér) Fourier sums of lesson 40.
:::

::: example
## What degree n of Bernstein polynomial approximates $x^2$ to within 0.001 everywhere on [0, 1]?

::: solution
1. **The largest error.** For $x^2$ it is $\frac{1}{4n}$.
2. **Solve for n.** That's below 0.001 when n > 250, so n = 251.
3. **Compare with interpolation.** A degree-2 interpolating polynomial would be exact, which shows how conservative Bernstein's method is.

**Answer:** degree n = 251.
:::
:::

::: example
## Bound the Bernstein error for $\cos 5x$ with n = 50.

::: solution
1. **Size of the second derivative.**
   $$\lVert f''\rVert_\infty = 25$$
2. **Bound the error.** With n = 50,
   $$\begin{aligned} \frac{\lVert f''\rVert_\infty}{8n} &= \frac{25}{400} \\ &= 0.0625 \end{aligned}$$
3. **Compare with the actual error.** Evaluating on a fine grid gives 0.058: the bound is nearly tight.

**Answer:** the error is at most 0.0625, and it's actually 0.058.
:::
:::

::: example
## Compute the Bernstein polynomial of $x^3$ of degree 2 at $x = \frac12$, and the general error.

::: solution
1. **Write out $B_2$.**
   $$\begin{aligned} B_2(x^3)(x) &= \sum_{k=0}^{2}\left(\frac k2\right)^3\binom2k \\ &\qquad \times x^k(1 - x)^{2-k} \\ &= \frac18 \cdot 2x(1 - x) + x^2 \end{aligned}$$
2. **Put in x = ½.**
   $$0.0625 + 0.25 = 0.3125$$
   against the true 0.125.
3. **The general error.**
   $$\begin{aligned} B_n(x^3) &= x^3 + \frac{3x^2(1 - x)}{n} \\ &\quad + \frac{x(1 - x)(1 - 2x)}{n^2} \end{aligned}$$
   so the error falls as $\frac1n$.
4. **What it shows.** Bernstein polynomials converge for every continuous function, but slowly. That's the price of a construction that works without any smoothness at all.

**Answer:** $B_2(x^3)$ is 0.3125 at ½, against the true 0.125, and in general the error falls as $\frac1n$.
:::
:::

::: check
given: n = 10 | 100 | 1000
answer: 1/(4*n)
tolerance: 0.1%

## What's the largest error of the degree-{n} Bernstein polynomial of $x^2$ on [0, 1]?

::: right
Right. $\frac{1}{4n}$ = {answer}, at x = ½.
:::

::: misses
- [1/n] The error is $\frac{x(1 - x)}{n}$, largest at x = ½, where $x(1 - x) = \frac14$.
- [any] Not yet. Maximize $\frac{x(1 - x)}{n}$.
:::
:::

::: check
given: e = 0.013 | 0.003 | 0.0023 | 0.0007 | 0.00031 | 0.00007
answer: floor(1/(4*e)) + 1
tolerance: 0

## Smallest Bernstein degree that approximates $x^2$ within {e} everywhere on [0, 1]?

::: right
Right. Need $\frac{1}{4n} < \varepsilon$: n = {answer}.
:::

::: misses
- [any] Not yet. Solve $\frac{1}{4n} < \varepsilon$.
:::
:::

::: check
given: w = 2 | 5 | 10; n = 20 | 500
answer: w^2/(8*n)
tolerance: 0.1%

## Bound the Bernstein error for $\cos(\given{w}x)$ on [0, 1] with degree {n}.

::: right
Right. $\frac{\lVert f''\rVert}{8n} = \frac{\omega^2}{8n}$ = {answer}.
:::

::: misses
- [w/(8*n)] The bound uses the second derivative, $\omega^2$ in size.
- [any] Not yet. $\lVert f''\rVert_\infty = \omega^2$.
:::
:::

::: check
given: x = 0.3 | 0.6; n = 5 | 40
answer: (1 - x + x*exp(1/n))^n
tolerance: 0.01%

## The Bernstein polynomial of $e^x$ has the closed form $(1 - x + xe^{1/n})^n$. What's its value at x = {x} for n = {n}?

::: right
Right. {answer}, a little above $e^{\given{x}}$, since $e^x$ curves upward.
:::

::: misses
- [exp(x)] That's the function itself; the polynomial is slightly larger, since $e^x$ is convex.
- [any] Not yet. Evaluate $(1 - x + xe^{1/n})^n$.
:::
:::

::: check
given: a = 2 | -3; b = 1 | 4; x = 0.25 | 0.8
answer: a*x + b
tolerance: 0.01%

## What's the Bernstein polynomial of $f(x) = \given{a}x + \given{b}$, of any degree, at x = {x}?

::: right
Right. Bernstein polynomials reproduce lines exactly: {answer}.
:::

::: misses
- [any] Not yet. The binomial mean of K/n is x.
:::
:::

::: check
kind: choice

## Why does $B_nf(x) \to f(x)$?

- [ ] Because it interpolates f at n + 1 points
- [x] Because it averages f(k/n) with binomial weights, which concentrate near k/n = x as n grows
- [ ] Because its coefficients are Taylor coefficients
- [ ] Because it's the best polynomial approximation

::: misses
- [1] It doesn't interpolate; except at the ends, $B_nf(k/n) \ne f(k/n)$ in general.
- [3] It uses values of f, not derivatives.
- [4] It's far from the best; the minimax polynomial is much closer.
:::
:::

::: check
kind: parts
given: n = 2 | 10; x = 0.25 | 0.5

## The Bernstein polynomial of $x^3$ is $B_n = x^3 + \frac{3x^2(1 - x)}{n} + \frac{x(1 - x)(1 - 2x)}{n^2}$.

::: part
answer: x^3 + 3*x^2*(1 - x)/n + x*(1 - x)*(1 - 2*x)/n^2
tolerance: 0.5%

### What is $B_{\given{n}}$ at x = {x}?

::: misses
- [x^3] Bernstein polynomials of degree n don't reproduce cubics exactly.
- [any] Evaluate the formula.
:::
:::

::: part
answer: 3*x^2*(1 - x)/n + x*(1 - x)*(1 - 2*x)/n^2
tolerance: 0.5%

### What is its error there?

::: misses
- [any] Subtract $x^3$.
:::
:::
:::

::: check
kind: derivation
vars: n = 1..100, x = 0..1

## The Bernstein polynomial of $x^2$.

::: step
answer: x^2 + x*(1 - x)/n

### What is $B_n(x^2)$?
:::

::: step
answer: 1/(4*n)

### What is its largest error on [0, 1]?
:::

::: right
Right. The error $\frac{x(1 - x)}{n}$ peaks at x = ½: convergence like $\frac1n$.
:::
:::

::: explain
## How does the law of large numbers prove that polynomials can approximate any continuous function?

::: model
Toss a coin with heads probability x, n times, and let K be the number of heads. Then K/n is close to x with high probability: its variance is $\frac{x(1 - x)}{n}$, which shrinks as n grows. The Bernstein polynomial is exactly the average of $f(K/n)$ over this experiment, and it's a polynomial in x because each binomial probability is. Where K/n is near x, $f(K/n)$ is near f(x) by uniform continuity; the chance that K/n is far from x is small, and f is bounded, so those cases contribute little. Together they make $B_nf$ uniformly close to f for large n.
:::
:::

::: practice

::: check
given: n = 5 | 10 | 25 | 100 | 400 | 1000
answer: 1/(4*n)
tolerance: 0.1%

## Largest Bernstein error for $x^2$, degree {n}?

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

::: check
given: w = 3 | 8; n = 50 | 2000
answer: w^2/(8*n)
tolerance: 0.1%

## Bernstein bound for $\cos(\given{w}x)$, degree {n}?

::: misses
- [any] $\frac{\omega^2}{8n}$.
:::
:::

::: check
given: e = 0.017 | 0.007 | 0.0013 | 0.00041 | 0.0003 | 0.00003
answer: floor(1/(4*e)) + 1
tolerance: 0

## Degree for $x^2$ within {e}?

::: misses
- [any] $n > \frac{1}{4\varepsilon}$.
:::
:::
:::
