---
title: Splines
heading: Smooth curves from short cubic pieces
eyebrow: A13 · Lesson 12 · Splines
xp: 40
---

::: hook
Lesson 11 showed that one high-degree polynomial through many points can swing wildly. The practical answer is to use many low-degree pieces, joined smoothly. A **cubic spline** is a chain of cubics, one between each pair of neighboring points, chosen so the curve, its slope and its curvature are all continuous at every joint. It never shows Runge's wiggles, it's cheap to build (one tridiagonal system), and its error falls as the fourth power of the spacing. Calibration curves, motion profiles for motors, font outlines and the "smooth" option in every plotting program are splines.
:::

::: play
- Drag the three middle points with the sliders (the ends are fixed at 0). The solid curve is the natural cubic spline through all five; the dashed one is the single degree-4 polynomial through the same points.
- Make the data jump (try 2.5, −1.5, 2.5): the polynomial overshoots far beyond the data, while the spline bends only as much as it must.
- The readouts are the spline's second derivatives at the inner knots, the unknowns of its tridiagonal system. At the two ends they're held at 0: that's what "natural" means.
:::
:::

```sim plot
view: graph
x: x = 0..4
y: y = -2..3
slider: y1 = -1.5..2.5 step 0.1 = 1
slider: y2 = -1.5..2.5 step 0.1 = 2
slider: y3 = -1.5..2.5 step 0.1 = 0.2
plot: (step(x - 0) - step(x - 1))*((0)*(1 - x)^3/6 + ((15*6*(y2 - 2*y1 + 0) - 4*6*(y3 - 2*y2 + y1) + 6*(0 - 2*y3 + y2))/56)*(x - 0)^3/6 + (0 - (0)/6)*(1 - x) + (y1 - ((15*6*(y2 - 2*y1 + 0) - 4*6*(y3 - 2*y2 + y1) + 6*(0 - 2*y3 + y2))/56)/6)*(x - 0)) + (step(x - 1) - step(x - 2))*(((15*6*(y2 - 2*y1 + 0) - 4*6*(y3 - 2*y2 + y1) + 6*(0 - 2*y3 + y2))/56)*(2 - x)^3/6 + ((-4*6*(y2 - 2*y1 + 0) + 16*6*(y3 - 2*y2 + y1) - 4*6*(0 - 2*y3 + y2))/56)*(x - 1)^3/6 + (y1 - ((15*6*(y2 - 2*y1 + 0) - 4*6*(y3 - 2*y2 + y1) + 6*(0 - 2*y3 + y2))/56)/6)*(2 - x) + (y2 - ((-4*6*(y2 - 2*y1 + 0) + 16*6*(y3 - 2*y2 + y1) - 4*6*(0 - 2*y3 + y2))/56)/6)*(x - 1)) + (step(x - 2) - step(x - 3))*(((-4*6*(y2 - 2*y1 + 0) + 16*6*(y3 - 2*y2 + y1) - 4*6*(0 - 2*y3 + y2))/56)*(3 - x)^3/6 + ((6*(y2 - 2*y1 + 0) - 4*6*(y3 - 2*y2 + y1) + 15*6*(0 - 2*y3 + y2))/56)*(x - 2)^3/6 + (y2 - ((-4*6*(y2 - 2*y1 + 0) + 16*6*(y3 - 2*y2 + y1) - 4*6*(0 - 2*y3 + y2))/56)/6)*(3 - x) + (y3 - ((6*(y2 - 2*y1 + 0) - 4*6*(y3 - 2*y2 + y1) + 15*6*(0 - 2*y3 + y2))/56)/6)*(x - 2)) + step(x - 3)*(((6*(y2 - 2*y1 + 0) - 4*6*(y3 - 2*y2 + y1) + 15*6*(0 - 2*y3 + y2))/56)*(4 - x)^3/6 + (0)*(x - 3)^3/6 + (y3 - ((6*(y2 - 2*y1 + 0) - 4*6*(y3 - 2*y2 + y1) + 15*6*(0 - 2*y3 + y2))/56)/6)*(4 - x) + (0 - (0)/6)*(x - 3)) | samples: 800 | label: natural cubic spline
plot: y1*(x - 0)*(x - 2)*(x - 3)*(x - 4)/(-6) + y2*(x - 0)*(x - 1)*(x - 3)*(x - 4)/(4) + y3*(x - 0)*(x - 1)*(x - 2)*(x - 4)/(-6) | dashed | color: ch3 | label: the degree-4 polynomial
point: 0, 0
point: 1, y1
point: 2, y2
point: 3, y3
point: 4, 0
readout: S″ at the knot x₁ = (15*6*(y2 - 2*y1 + 0) - 4*6*(y3 - 2*y2 + y1) + 6*(0 - 2*y3 + y2))/56
readout: S″ at the knot x₂ = (-4*6*(y2 - 2*y1 + 0) + 16*6*(y3 - 2*y2 + y1) - 4*6*(0 - 2*y3 + y2))/56
readout: S″ at the knot x₃ = (6*(y2 - 2*y1 + 0) - 4*6*(y3 - 2*y2 + y1) + 15*6*(0 - 2*y3 + y2))/56
```

::: predict
## A clamped cubic spline's error falls as $h^4$. If you double the number of table points, by what factor does the error shrink?

- [ ] 2
- [ ] 4
- [ ] 8
- [x] 16

::: reveal
16: doubling the points halves h, and $(1/2)^4 = 1/16$. Linear interpolation, with error $\propto h^2$, would improve only 4 times. That's why a short cubic-spline table can match a much longer linear one.
:::
:::

::: nameit
## The cubic spline

On each interval $[x_i, x_{i+1}]$ of width $h_i$, S is a cubic, and at every inner knot S, S′ and S″ are continuous. Writing $M_i = S''(x_i)$:

$$S(x) = M_i\frac{(x_{i+1} - x)^3}{6h_i} + M_{i+1}\frac{(x - x_i)^3}{6h_i} + \left(y_i - \frac{M_ih_i^2}{6}\right)\frac{x_{i+1} - x}{h_i} + \left(y_{i+1} - \frac{M_{i+1}h_i^2}{6}\right)\frac{x - x_i}{h_i}$$

Continuity of S′ at each inner knot gives one equation; with equal spacing h,

$$M_{i-1} + 4M_i + M_{i+1} = \frac{6}{h^2}\left(y_{i+1} - 2y_i + y_{i-1}\right)$$

a **tridiagonal** system, solved in O(n) operations by the Thomas algorithm.

## End conditions

n + 1 points give n cubics, 4n coefficients; interpolation and continuity supply 4n − 2 equations, so two more are needed:

- **natural:** $S''(x_0) = S''(x_n) = 0$ (a thin elastic rod, free at its ends);
- **clamped:** the end slopes are given; error $O(h^4)$;
- **not-a-knot:** the first two pieces are the same cubic, and so are the last two; error $O(h^4)$ without knowing slopes. This is the default in many libraries.

## Shape-preserving pieces

A spline can still overshoot between data points that jump. When the curve must stay monotone, such as a calibration curve or a lookup table that a controller inverts, use **monotone piecewise cubic Hermite** interpolation (PCHIP), which chooses slopes that never overshoot, at the cost of a discontinuous second derivative.
:::

::: example
## Find the natural spline through (0, 0), (1, 1), (2, 0) and evaluate it at 0.5.

::: solution
1. **The one unknown.**
   $$\begin{aligned} 4M_1 &= 6(0 - 2 + 0) \\ &= -12 \end{aligned}$$
   so $M_1 = -3$.
2. **The cubic on [0, 1].**
   $$\begin{aligned} S(x) &= -3\frac{x^3}{6} + \left(1 + \frac36\right)x \\ &= -0.5x^3 + 1.5x \end{aligned}$$
3. **Evaluate at 0.5.**
   $$\begin{aligned} S(0.5) &= -0.0625 + 0.75 \\ &= 0.6875 \end{aligned}$$
4. **Check the end.** Its slope at 0 is 1.5 and its curvature there 0, as a natural spline's must be.

**Answer:** $S(0.5) = 0.6875$, from $S(x) = -0.5x^3 + 1.5x$ on [0, 1].
:::
:::

::: example
## How many operations does building a spline through 10 000 points take?

::: solution
1. **The tridiagonal solve.** It is one forward sweep and one back sweep, about 8 operations per unknown:
   $$8 \times 10\,000 = 80\,000$$
2. **Compare with a dense solve.** A dense solve of the same 10 000 equations would take about
   $$\frac23 \times 10^{12}$$
   operations.

**Answer:** roughly 80 000 operations, against about $\frac23 \times 10^{12}$ for a dense solve.
:::
:::

::: example
## Fit a natural spline through (0, 0), (1, 1), (2, 8), (3, 27), points on $y = x^3$. Compare its second derivatives at the inner points with the true ones.

::: solution
1. **The spline equations.** With h = 1 and $M_0 = M_3 = 0$:
   $$\begin{aligned} 4M_1 + M_2 &= 6(8 - 2 + 0) \\ &= 36 \\ M_1 + 4M_2 &= 6(27 - 16 + 1) \\ &= 72 \end{aligned}$$
2. **Solve.**
   $$M_1 = 4.8, \quad M_2 = 16.8$$
3. **Compare with the truth.** The true values are $6x$: 6 and 12.
4. **Where the error comes from.** The natural end condition forces $M_3 = 0$ where the cube's second derivative is 18, and that error spreads inward.
5. **Better end conditions.** When you know the end slopes, a clamped spline avoids this; otherwise the not-a-knot condition is usually better than natural.

**Answer:** $M_1 = 4.8$ and $M_2 = 16.8$, against the true 6 and 12.
:::
:::

::: check
given: a = 0 | 1 | 2; b = 1.5 | 3.5; c = 0 | 4
answer: 1.5*(a - 2*b + c)
tolerance: 0.1%

## A natural spline passes through (0, {a}), (1, {b}), (2, {c}). What's its second derivative $M_1$ at x = 1?

::: right
Right. $4M_1 = 6(a - 2b + c)$, so $M_1$ = {answer}.
:::

::: misses
- [6*(a - 2*b + c)] Divide by the 4 on the left side.
- [any] Not yet. With $M_0 = M_2 = 0$: $4M_1 = 6(y_0 - 2y_1 + y_2)$.
:::
:::

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

## For the same kind of spline through (0, {a}), (1, {b}), (2, {c}), what's S(0.5)?

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

::: misses
- [(a + b)/2] That's the straight line; the spline bends.
- [any] Not yet. On [0, 1]: $S = M_1\frac{x^3}{6} + y_0(1 - x) + \left(y_1 - \frac{M_1}{6}\right)x$.
:::
:::

::: check
given: k = 2 | 3 | 10
answer: k^4
tolerance: 0.5%

## A clamped spline's error goes as $h^4$. By what factor does it shrink when the table gets {k} times as many intervals?

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

::: misses
- [k^2] That's linear interpolation's rate.
- [any] Not yet. $(1/k)^4$.
:::
:::

::: check
given: n = 5 | 11 | 101
answer: 4*(n - 1)
tolerance: 0

## How many polynomial coefficients does a cubic spline through {n} points have?

::: right
Right. n − 1 cubics with 4 coefficients each: {answer}.
:::

::: misses
- [4*n] There are n − 1 intervals, not n.
- [any] Not yet. One cubic per interval.
:::
:::

::: check
given: n = 100 | 1000 | 5000 | 10000 | 100000 | 1000000
answer: 8*n
tolerance: 1%

## About how many arithmetic operations does a tridiagonal solve take for {n} unknowns (about 8 per unknown)?

::: right
Right. {answer}: linear in n.
:::

::: misses
- [2*n^3/3] That's a dense solve; the tridiagonal structure makes it linear.
- [any] Not yet. About 8n.
:::
:::

::: check
kind: choice

## A calibration table must be inverted by a controller, so its interpolant must never turn back. Which interpolant should you use?

- [ ] A natural cubic spline
- [ ] One polynomial through all the points
- [x] A monotone piecewise cubic Hermite (PCHIP)
- [ ] A not-a-knot spline

::: misses
- [1] Splines can overshoot between points that change sharply, and so lose monotonicity.
- [2] That's the worst choice: Runge's wiggles.
- [4] It's smoother, but it can still overshoot.
:::
:::

::: check
kind: parts
given: a = 1 | 2; b = 3 | -1

## A natural spline passes through (0, 0), (1, {a}), (2, {b}), (3, 0), with h = 1.

::: part
answer: (4*6*(b - 2*a) - 6*(a - 2*b))/15
tolerance: 0.5%

### What is $M_1$, its second derivative at x = 1?

::: misses
- [6*(b - 2*a)/4] The two inner equations are coupled: $4M_1 + M_2 = 6(b - 2a)$ and $M_1 + 4M_2 = 6(a - 2b)$.
- [any] Solve the 2 × 2 system.
:::
:::

::: part
answer: (4*6*(a - 2*b) - 6*(b - 2*a))/15
tolerance: 0.5%

### What is $M_2$?

::: misses
- [any] The same system, other unknown.
:::
:::
:::

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

## A natural spline has knots at 0, 1, 2 with values a, b, c, so $M_0 = M_2 = 0$.

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

### The middle equation is $4M_1 = 6(c - 2b + a)$. What is $M_1$?
:::

::: right
Right. With one interior knot, the spline's only free curvature is set by the second difference of the data.
:::
:::

::: explain
## Why does a spline avoid the wild swings of a single high-degree interpolating polynomial?

::: model
A single polynomial through all the points has one set of coefficients, so every data point affects the curve everywhere, and forcing it through many points can make it oscillate between them, especially near the ends. A spline uses a separate cubic on each interval, joined so the curve, slope and curvature match at the knots. Each piece only has to bend enough to connect its two neighbors smoothly, and the influence of a data point dies away quickly with distance along the chain. With low degree and local influence, there's no room for large oscillations, and the error is controlled by the spacing rather than by high derivatives across the whole interval.
:::
:::

::: practice

::: check
given: a = 2 | 5; b = 0 | 1; c = 3 | 6
answer: 1.5*(a - 2*b + c)
tolerance: 0.1%

## $M_1$ for the natural spline through (0, {a}), (1, {b}), (2, {c})?

::: misses
- [any] $1.5(a - 2b + c)$.
:::
:::

::: check
given: k = 2..10 int
answer: k^4
tolerance: 0.5%

## Error reduction for {k} times as many intervals, $O(h^4)$?

::: misses
- [any] $k^4$.
:::
:::

::: check
given: n = 3..60 int
answer: 4*(n - 1)
tolerance: 0

## Coefficients in a cubic spline through {n} points?

::: misses
- [any] $4(n - 1)$.
:::
:::
:::
