---
title: Polynomial interpolation
heading: A curve through the points, and where it goes wrong between them
eyebrow: A13 · Lesson 11 · Polynomial interpolation
xp: 40
---

::: hook
A thermistor's datasheet gives resistance at every 5 °C; your firmware needs it at 23.7 °C. A sine table in a synthesizer has 256 entries; the phase lands between them. A sample-rate converter needs the signal halfway between samples. Each time, you fit a polynomial through nearby points and evaluate it between them. Low-degree pieces work beautifully. One high-degree polynomial through many equally spaced points can fail spectacularly, swinging wildly near the ends; the fix is to choose the points, clustering them toward the ends as the **Chebyshev nodes** do.
:::

::: play
- The black curve is Runge's function. The orange curve is the polynomial through n + 1 equally spaced points; the dashed one goes through n + 1 Chebyshev points, crowded toward ±1.
- At n = 4 both are rough. Raise n to 8 and 12: the equally spaced interpolant gets worse near the ends (its largest error grows to 3.7), while the Chebyshev one gets steadily better.
- More points only help if they're in the right places.
:::
:::

```sim plot
view: graph
x: x = -1..1
y: y = -1..2
slider: n = 4..12 step 4 = 8
plot: 1/(1 + 25*x^2) | label: f(x) = 1/(1 + 25x²)
plot: step(6 - n)*(((((3.3156498673740051)*x + 0)*x + -4.2771883289124668)*x + 0)*x + 1) + step(n - 6)*step(10 - n)*(((((((((53.689300554583632)*x + 0)*x + -102.81501056202765)*x + 0)*x + 61.367206092017554)*x + 0)*x + -13.203034546111995)*x + 0)*x + 1) + step(n - 10)*(((((((((((((909.87557831407082)*x + 0)*x + -2336.3582905375752)*x + 0)*x + 2201.7539934094307)*x + 0)*x + -955.37332748246922)*x + 0)*x + 198.72334356463483)*x + 0)*x + -19.582835729630638)*x + 0)*x + 1) | samples: 800 | color: ch2 | label: through n + 1 equally spaced points
plot: step(6 - n)*(((((2.7464982147761612)*x + -2.7328268243846652e-16)*x + -3.5429826970612472)*x + 2.4718650839977896e-16)*x + 1) + step(n - 6)*step(10 - n)*(((((((((17.620272899262616)*x + -8.0876863075379952e-15)*x + -40.350424939311388)*x + 1.489992576882923e-14)*x + 31.348227515078115)*x + -8.3780771593766125e-15)*x + -9.5134320221324735)*x + 1.4884541269910266e-15)*x + 1) + step(n - 10)*(((((((((((((124.49032848773859)*x + -1.0357261904405375e-13)*x + -409.57318072465995)*x + 3.0349502309359156e-13)*x + 522.12488671042433)*x + -3.3126490600552425e-13)*x + -324.33017115727972)*x + 1.6465272821869701e-13)*x + 101.47804975554283)*x + -3.6115105172627151e-14)*x + -15.122227353878166)*x + 2.7484883411888111e-15)*x + 1) | samples: 800 | color: ch3 | dashed | label: through n + 1 Chebyshev points
readout: largest error, equally spaced = step(6 - n)*0.438 + step(n - 6)*step(10 - n)*1.045 + step(n - 10)*3.663
readout: largest error, Chebyshev = step(6 - n)*0.402 + step(n - 6)*step(10 - n)*0.171 + step(n - 10)*0.069
```

::: predict
## A 256-entry sine table is read with linear interpolation between entries. About how large is the worst error, for a sine of amplitude 1?

- [ ] $10^{-2}$
- [x] $7.5 \times 10^{-5}$ (−82 dB)
- [ ] $10^{-8}$
- [ ] Zero: the table is exact at its entries

::: reveal
$7.5 \times 10^{-5}$: the linear-interpolation error is at most $\frac{h^2}{8}\max\lvert f''\rvert$, with $h = 2\pi/256$ and $\lvert f''\rvert \le 1$, so $\frac{(2\pi/256)^2}{8} = 7.5 \times 10^{-5}$. Doubling the table quarters it.
:::
:::

::: nameit
## The interpolating polynomial

Through n + 1 points $(x_i, y_i)$ with distinct $x_i$ there's exactly one polynomial of degree at most n. In **Lagrange form**

$$p(x) = \sum_{i=0}^{n}y_i\,L_i(x), \qquad L_i(x) = \prod_{j\ne i}\frac{x - x_j}{x_i - x_j}$$

and in **Newton form**, built from divided differences $f[x_i] = y_i$, $f[x_i, \ldots, x_{i+k}] = \frac{f[x_{i+1}, \ldots, x_{i+k}] - f[x_i, \ldots, x_{i+k-1}]}{x_{i+k} - x_i}$:

$$p(x) = f[x_0] + f[x_0, x_1](x - x_0) + f[x_0, x_1, x_2](x - x_0)(x - x_1) + \cdots$$

In practice use the **barycentric** form of Lagrange, which is stable and costs O(n) per evaluation. Never solve the Vandermonde system for the coefficients: its condition number grows exponentially with n.

## The error

$$f(x) - p(x) = \frac{f^{(n+1)}(\xi)}{(n + 1)!}\prod_{i=0}^{n}(x - x_i)$$

for some ξ in the interval. For linear interpolation with spacing h this gives at most $\frac{h^2}{8}\max\lvert f''\rvert$.

## Runge's phenomenon and Chebyshev nodes

With equally spaced points the product $\prod(x - x_i)$ is enormous near the ends, and for functions like $\frac{1}{1 + 25x^2}$ the error grows without bound as n increases. The **Chebyshev nodes**

$$x_i = \cos\frac{(2i + 1)\pi}{2(n + 1)}, \qquad i = 0, \ldots, n$$

make $\max\lvert\prod(x - x_i)\rvert$ as small as possible on [−1, 1], namely $2^{-n}$, and the interpolant converges for every smooth function. For another interval, scale them linearly.
:::

::: example
## A thermistor table gives 10.00 kΩ at 25 °C and 8.06 kΩ at 30 °C. Estimate R at 27 °C by linear interpolation.

::: solution
1. **Interpolate.**
   $$\begin{aligned} R &= 10.00 + \frac{27 - 25}{30 - 25} \\ &\quad \times (8.06 - 10.00) \\ &= 10.00 - 0.776 \\ &= 9.224\ \text{k}\Omega \end{aligned}$$
2. **Compare with the curve.** The true curve is convex (it's roughly exponential), so the line overestimates slightly: the exponential through both points gives 9.166 kΩ.
3. **How good that is.** A thermistor table every 5 °C with linear interpolation is good to within about 1%.

**Answer:** linear interpolation gives 9.224 kΩ, slightly high; the exponential through both points gives 9.166 kΩ.
:::
:::

::: example
## Find the quadratic through (0, 1), (1, 3), (3, 2) in Newton form.

::: solution
1. **First divided differences.**
   $$\begin{aligned} f[x_0, x_1] &= \frac{3 - 1}{1 - 0} \\ &= 2 \\ f[x_1, x_2] &= \frac{2 - 3}{3 - 1} \\ &= -0.5 \end{aligned}$$
2. **Second divided difference.**
   $$\begin{aligned} f[x_0, x_1, x_2] &= \frac{-0.5 - 2}{3 - 0} \\ &= -0.8333 \end{aligned}$$
3. **The Newton form.**
   $$p(x) = 1 + 2x - 0.8333x(x - 1)$$
4. **Evaluate at 2.**
   $$\begin{aligned} p(2) &= 1 + 4 - 1.667 \\ &= 3.333 \end{aligned}$$

**Answer:** $p(x) = 1 + 2x - 0.8333x(x - 1)$, with $p(2) = 3.333$.
:::
:::

::: example
## Interpolate $f(x) = \frac{1}{1 + 25x^2}$ on [−1, 1] with a degree-10 polynomial, once through equally spaced nodes and once through Chebyshev nodes. How large are the errors?

::: solution
1. **Equally spaced nodes.** With 11 equally spaced nodes the interpolant swings wildly near the ends: the largest error is 1.92, larger than the function itself.
2. **Chebyshev nodes.** With the 11 Chebyshev nodes $\cos\frac{(2i + 1)\pi}{22}$ the largest error is 0.109.
3. **More equally spaced nodes.** Adding them makes it worse, which is Runge's phenomenon.
4. **Why Chebyshev nodes work.** They cluster near the ends, where the node polynomial $\prod(x - x_i)$ would otherwise be largest, and their error keeps shrinking as nodes are added.

**Answer:** the largest error is 1.92 with equally spaced nodes and 0.109 with Chebyshev nodes.
:::
:::

::: check
given: x0 = 20 | 40; y0 = 12.49 | 5.33; h = 5 | 10; s = 0.3 | 0.7; dy = -1.8 | -0.9
answer: y0 + s*dy
unit: kΩ
tolerance: 0.1%

## A table gives {y0} kΩ at {x0} °C, and the value changes by {dy} kΩ over the next {h} °C. By linear interpolation, what's the resistance a fraction {s} of the way across that step?

::: right
Right. $y_0 + s\,\Delta y$ = {answer} kΩ.
:::

::: misses
- [y0 + s*h*dy] The fraction s already accounts for the spacing.
- [any] Not yet. $y = y_0 + s(y_1 - y_0)$.
:::
:::

::: check
given: a = 1 | 2; b = 3 | 5; c = 2 | 4; x = 1.5 | 0.5
answer: a*(x - 1)*(x - 2)/2 - b*x*(x - 2) + c*x*(x - 1)/2
tolerance: 0.1%

## The quadratic through (0, {a}), (1, {b}), (2, {c}): what's its value at x = {x}?

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

::: misses
- [any] Not yet. $L_0 = \frac{(x - 1)(x - 2)}{2}$, $L_1 = -x(x - 2)$, $L_2 = \frac{x(x - 1)}{2}$.
:::
:::

::: check
given: a = 1 | 2; b = 3 | 6; c = 2 | 9
answer: ((c - b)/2 - (b - a))/3
tolerance: 0.1%

## For the points (0, {a}), (1, {b}), (3, {c}), what's the divided difference $f[x_0, x_1, x_2]$?

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

::: misses
- [(c - b)/2 - (b - a)] Divide by $x_2 - x_0 = 3$.
- [any] Not yet. $\frac{f[x_1, x_2] - f[x_0, x_1]}{x_2 - x_0}$.
:::
:::

::: check
given: n = 4 | 8 | 12; i = 0 | 1 | 2
answer: cos((2*i + 1)*pi/(2*(n + 1)))
tolerance: 0.1%

## What's Chebyshev node $x_{\given{i}}$ for n = {n} (n + 1 nodes on [−1, 1])?

::: right
Right. $\cos\frac{(2i + 1)\pi}{2(n + 1)}$ = {answer}.
:::

::: misses
- [cos((2*i + 1)*pi/(2*n))] There are n + 1 nodes, so the denominator is $2(n + 1)$.
- [any] Not yet. $x_i = \cos\frac{(2i + 1)\pi}{2(n + 1)}$.
:::
:::

::: check
given: N = 64 | 256 | 1024
answer: (2*pi/N)^2/8
tolerance: 0.5%

## A sine table has {N} entries per cycle, read with linear interpolation. What's the worst-case error, for amplitude 1?

::: right
Right. $\frac{(2\pi/N)^2}{8}$ = {answer}.
:::

::: misses
- [(2*pi/N)^2/2] The bound for linear interpolation is $h^2/8$ times $\max\lvert f''\rvert$.
- [any] Not yet. $\frac{h^2}{8}\max\lvert f''\rvert$ with $h = 2\pi/N$.
:::
:::

::: check
kind: choice

## Why do Chebyshev nodes cluster near the ends of the interval?

- [ ] Because functions are steeper there
- [x] To shrink the product $\prod(x - x_i)$ in the error formula where it's largest, near the ends
- [ ] So the table is easier to store
- [ ] They don't; they're equally spaced in x

::: misses
- [1] The choice doesn't depend on f at all.
- [3] Storage isn't the reason.
- [4] They're equally spaced in angle, $\theta_i$, which crowds them in $x = \cos\theta$ near ±1.
:::
:::

::: check
kind: parts
given: x = 0.5 | 1.5 | 2.5

## The quadratic through nodes 0, 1, 2 uses the Lagrange basis polynomials.

::: part
answer: (x - 1)*(x - 2)/2
tolerance: 0.5%

### What is $L_0(x)$ at x = {x}?

::: misses
- [any] $L_0 = \frac{(x - 1)(x - 2)}{(0 - 1)(0 - 2)}$.
:::
:::

::: part
answer: -x*(x - 2)
tolerance: 0.5%

### And $L_1(x)$?

::: misses
- [x*(x - 2)] The denominator is $(1 - 0)(1 - 2) = -1$.
- [any] $L_1 = \frac{x(x - 2)}{(1 - 0)(1 - 2)}$.
:::
:::
:::

::: check
kind: derivation
vars: a = -3..3, b = -3..3, c = -3..3

## Data $f(0) = a$, $f(1) = b$, $f(2) = c$.

::: step
answer: b - a

### What is the divided difference $f[x_0, x_1]$?
:::

::: step
answer: (c - 2*b + a)/2

### What is $f[x_0, x_1, x_2]$?
:::

::: right
Right. The Newton form is $a + (b - a)x + \frac{c - 2b + a}{2}x(x - 1)$.
:::
:::

::: explain
## Why can adding more equally spaced points make a polynomial interpolant worse?

::: model
The interpolant must pass through every point, and a high-degree polynomial pinned at equally spaced points has the freedom to swing between them. The error formula shows how much: it's the (n + 1)th derivative of f over (n + 1)! times the product of the distances to all the nodes. For equally spaced nodes that product is many times larger near the ends of the interval than in the middle, and for functions like Runge's the derivatives grow fast enough that the error near the ends increases with n. Chebyshev nodes crowd toward the ends exactly enough to keep the product uniformly small, so the error then shrinks as n grows.
:::
:::

::: practice

::: check
given: N = 64 | 128 | 256 | 512 | 1024 | 4096
answer: (2*pi/N)^2/8
tolerance: 0.5%

## Worst linear-interpolation error, {N}-entry sine table?

::: misses
- [any] $(2\pi/N)^2/8$.
:::
:::

::: check
given: n = 5 | 10; i = 0 | 3
answer: cos((2*i + 1)*pi/(2*(n + 1)))
tolerance: 0.1%

## Chebyshev node $x_{\given{i}}$, n = {n}?

::: misses
- [any] $\cos\frac{(2i + 1)\pi}{2(n + 1)}$.
:::
:::

::: check
given: a = 4 | 0; b = 1 | 2; c = 7 | 5
answer: ((c - b)/2 - (b - a))/3
tolerance: 0.1%

## $f[x_0, x_1, x_2]$ for (0, {a}), (1, {b}), (3, {c})?

::: misses
- [any] $\frac{(c - b)/2 - (b - a)}{3}$.
:::
:::
:::
