A1312 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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 h4. If you double the number of table points, by what factor does the error shrink?

Name it

The cubic spline

On each interval [xi,xi+1] of width hi, S is a cubic, and at every inner knot S, S′ and S″ are continuous. Writing Mi=S′′(xi):

S(x)=Mi(xi+1−x)36hi+Mi+1(x−xi)36hi+(yi−Mihi26)xi+1−xhi+(yi+1−Mi+1hi26)x−xihi

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

Mi−1+4Mi+Mi+1=6h2(yi+1−2yi+yi−1)

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′′(x0)=S′′(xn)=0 (a thin elastic rod, free at its ends);
  • clamped: the end slopes are given; error O(h4);
  • not-a-knot: the first two pieces are the same cubic, and so are the last two; error O(h4) 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
  1. The one unknown. 4M1=6(0−2+0)=−12 so M1=−3.
  2. The cubic on [0, 1]. S(x)=−3x36+(1+36)x=−0.5x3+1.5x
  3. Evaluate at 0.5. S(0.5)=−0.0625+0.75=0.6875
  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.5x3+1.5x on [0, 1].

Worked example

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

Show the solution
  1. The tridiagonal solve. It is one forward sweep and one back sweep, about 8 operations per unknown: 8×10000=80000
  2. Compare with a dense solve. A dense solve of the same 10 000 equations would take about 23×1012 operations.

Answer roughly 80 000 operations, against about 23×1012 for a dense solve.

Worked example

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

Show the solution
  1. The spline equations. With h = 1 and M0=M3=0: 4M1+M2=6(8−2+0)=36M1+4M2=6(27−16+1)=72
  2. Solve. M1=4.8,M2=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 M3=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 M1=4.8 and M2=16.8, against the true 6 and 12.

Check

A natural spline passes through (0, 1), (1, 1.5), (2, 4). What's its second derivative M1 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 h4. 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 M1, its second derivative at x = 1?

(b)

What is M2?

Check

A natural spline has knots at 0, 1, 2 with values a, b, c, so M0=M2=0.

Step 1

The middle equation is 4M1=6(c−2b+a). What is M1?

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?

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.