A13 · Lesson 12 · Splines
Smooth curves from short cubic pieces
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.
- 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.
Predict
A clamped cubic spline's error falls as . If you double the number of table points, by what factor does the error shrink?
16: doubling the points halves h, and . Linear interpolation, with error , would improve only 4 times. That's why a short cubic-spline table can match a much longer linear one.
Name it
The cubic spline
On each interval of width , S is a cubic, and at every inner knot S, S′ and S″ are continuous. Writing :
Continuity of S′ at each inner knot gives one equation; with equal spacing h,
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: (a thin elastic rod, free at its ends);
- clamped: the end slopes are given; error ;
- not-a-knot: the first two pieces are the same cubic, and so are the last two; error 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.
Worked example
Find the natural spline through (0, 0), (1, 1), (2, 0) and evaluate it at 0.5.
Show the solution
- The one unknown. so .
- The cubic on [0, 1].
- Evaluate at 0.5.
- Check the end. Its slope at 0 is 1.5 and its curvature there 0, as a natural spline's must be.
Answer , from on [0, 1].
Worked example
How many operations does building a spline through 10 000 points take?
Show the solution
- The tridiagonal solve. It is one forward sweep and one back sweep, about 8 operations per unknown:
- Compare with a dense solve. A dense solve of the same 10 000 equations would take about operations.
Answer roughly 80 000 operations, against about for a dense solve.
Worked example
Fit a natural spline through (0, 0), (1, 1), (2, 8), (3, 27), points on . Compare its second derivatives at the inner points with the true ones.
Show the solution
- The spline equations. With h = 1 and :
- Solve.
- Compare with the truth. The true values are : 6 and 12.
- Where the error comes from. The natural end condition forces where the cube's second derivative is 18, and that error spreads inward.
- 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 and , against the true 6 and 12.
Check
A natural spline passes through (0, 1), (1, 1.5), (2, 4). What's its second derivative at x = 1?
Check
For the same kind of spline through (0, 0), (1, 1), (2, 4), what's S(0.5)?
Check
A clamped spline's error goes as . By what factor does it shrink when the table gets 3 times as many intervals?
Check
How many polynomial coefficients does a cubic spline through 11 points have?
Check
About how many arithmetic operations does a tridiagonal solve take for 5000 unknowns (about 8 per unknown)?
Check
A calibration table must be inverted by a controller, so its interpolant must never turn back. Which interpolant should you use?
Check
A natural spline passes through (0, 0), (1, 1), (2, 3), (3, 0), with h = 1.
(a)
What is , its second derivative at x = 1?
(b)
What is ?
Check
A natural spline has knots at 0, 1, 2 with values a, b, c, so .
Step 1
The middle equation is . What is ?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Explain why
Why does a spline avoid the wild swings of a single high-degree interpolating polynomial?
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
Keep practicing
9 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.