Search

Search papers and lessons

Cubic Spline Interpolation

Level 2 · 11 min read

A cubic spline joins neighbouring data points with cubic pieces that meet smoothly, so the curve has no corners, and it is the standard way to draw a smooth curve through measured points.

On this page

  • Colour = topic mix
  • Discrete math
  • Calculus
  • Linear algebra
  • Abstract algebra
  • ReadReady to readNot yet

TL;DR

A cubic spline is a smooth curve that passes through a list of measured points. It is built from one cubic polynomial for each gap between neighbouring points, and the pieces are joined so that the curve, its slope and its curvature all match at every point where two pieces meet. That smoothness is why splines are the standard tool for drawing a curve through data.

Builds on: Polynomial, Linear Interpolation, Derivative, Gaussian Elimination · Leads to: Finite Element Method (piecewise polynomial pieces joined at mesh points) and Empirical Mode Decomposition (spline envelopes)

Where it's used

Computer graphics and fonts. Smooth outlines of letters and shapes are drawn with spline pieces. A designer can move one control point and the curve bends locally, without the rest of the shape changing. Splines of this kind are the basis of many vector drawing tools.

Engineering measurements. A sensor records a quantity, such as the temperature of a part, only at a few times. Engineers use a spline through those readings to estimate the value between samples and to see the trend. Straight lines between samples would show corners that the real process does not have.

Numerical simulation. Solvers for heat flow and structural loads use smooth piecewise curves to represent unknown functions between mesh points. The Finite Element Method, for example, uses low-degree polynomial pieces that join up at mesh points, which is the same piecewise idea a spline uses, though its joins are usually less smooth than a spline's.

How it works

Take three points, measured at times 0, 1 and 2 seconds, for the height of a ball thrown up and caught again: the height is 0 m at time 0, 1 m at time 1 s, and 0 m at time 2 s. We want a smooth curve through them, with two pieces, one for each gap.

  1. Start with the points. The data are (0,0)(0,0), (1,1)(1,1) and (2,0)(2,0). There are two gaps, [0,1][0,1] and [1,2][1,2]. Check: three points give two gaps.
  2. Use a cubic on each gap. Each piece is a cubic Polynomial, a curve of the form a+bx+cx2+dx3a + bx + cx^2 + dx^3. Four unknowns per piece. Check: two pieces, eight unknowns in total.
  3. Force the pieces through the points. Each piece must hit the points at its two ends. This gives four equations, one per end of each piece. Check: the four equations are not enough to fix the eight unknowns, so more conditions are needed.
  4. Force the pieces to join smoothly. At the middle point x=1x = 1 both pieces already hit the height 1 from step 3. They must also have the same slope and the same curvature, which adds two equations. The two outer ends add two more: the curvature there is zero, which is the "natural" choice. Check: four plus two plus two is eight equations for eight unknowns, so the curve is fixed.
  5. Solve the system. The equations reduce to a single unknown, the curvature at the middle point, which equals −3-3. Substituting back gives the piece on [0,1][0,1] as S(x)=1.5x−0.5x3S(x) = 1.5x - 0.5x^3. Check: S(0)=0S(0) = 0 and S(1)=1.5−0.5=1S(1) = 1.5 - 0.5 = 1, so both ends hit their points.
  6. Read off a value between the points. At x=0.5x = 0.5 the spline gives S(0.5)=0.75−0.0625=0.6875S(0.5) = 0.75 - 0.0625 = 0.6875 metres. A straight line between (0,0)(0,0) and (1,1)(1,1) would give 0.50.5. The spline is higher, which matches a thrown object that is still climbing fast at the midpoint.
flowchart LR
  A["Data points"] --> B["One cubic per gap"]
  B --> C["Pieces hit their end points"]
  C --> D["Value, slope and curvature match at each joint"]
  D --> E["Solve a small linear system"]
  E --> F["Smooth curve through the points"]
00.20.40.60.8100.20.40.60.811.2t (seconds)height (m)
  • spline on [0, 1]
  • straight line
On the first gap the spline bends above the straight line between the two measured points. The marked point at t = 0.5 is the spline's value there.

The math

Let the data points be (x0,y0),…,(xn,yn)(x_0, y_0), \ldots, (x_n, y_n) with x0<x1<⋯<xnx_0 \lt x_1 \lt \cdots \lt x_n. Write SiS_i for the cubic piece on the gap [xi,xi+1][x_i, x_{i+1}], and SS for the whole spline.

Definition: Natural cubic spline

A function SS made of cubic pieces SiS_i such that

  • S(xi)=yiS(x_i) = y_i for every data point,
  • Si−1S_{i-1} and SiS_i agree in value, first derivative and second derivative at each interior point xix_i,
  • S′′(x0)=S′′(xn)=0S''(x_0) = S''(x_n) = 0 at the two ends.

The second derivative is the curvature. The last condition says the curve is straight beyond the ends, which is why it is called natural.

Step 2 in symbols: the piece form

Let every gap have the same width h=xi+1−xih = x_{i+1} - x_i. Let Mi=S′′(xi)M_i = S''(x_i) be the unknown curvature at each data point. Each piece is then

Si(x)=Mi(xi+1−x)3+Mi+1(x−xi)36h+(yi−Mih26)xi+1−xh+(yi+1−Mi+1h26)x−xihS_i(x) = \frac{M_i (x_{i+1} - x)^3 + M_{i+1} (x - x_i)^3}{6h} + \left(y_i - \frac{M_i h^2}{6}\right)\frac{x_{i+1} - x}{h} + \left(y_{i+1} - \frac{M_{i+1} h^2}{6}\right)\frac{x - x_i}{h}

Each term is a cubic in xx. Its second derivative is linear, and at each end it equals the matching MM. Check: setting x=xix = x_i in the second derivative gives MiM_i, as required.

Steps 3 and 4 in symbols: the equations for the curvatures

Matching the first derivatives at each interior point gives one equation per interior point. For equal spacing it reads

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

with M0=Mn=0M_0 = M_n = 0 for the natural spline. This is a system of linear equations with a tridiagonal pattern, and Gaussian Elimination solves it. For the three-point example, h=1h = 1 and M0=M2=0M_0 = M_2 = 0, so the single interior equation is 4M1=6(0−2+0)4M_1 = 6(0 - 2 + 0), giving M1=−3M_1 = -3.

Step 5 in symbols: the piece

With M0=0M_0 = 0, M1=−3M_1 = -3, h=1h = 1, y0=0y_0 = 0 and y1=1y_1 = 1, the general formula gives

S0(x)=−3x36+(1+36)x=1.5x−0.5x3S_0(x) = \frac{-3x^3}{6} + \left(1 + \frac{3}{6}\right)x = 1.5x - 0.5x^3

Check the second derivative: S0′′(x)=−3xS_0''(x) = -3x, which equals −3-3 at x=1x = 1, matching M1M_1.

Step 6 in symbols: evaluation

Evaluating the piece at a point between the data gives the estimate. At x=0.5x = 0.5 we get 1.5(0.5)−0.5(0.5)3=0.68751.5(0.5) - 0.5(0.5)^3 = 0.6875.

The cost

Solving the tridiagonal system takes time proportional to the number of points, O(n)O(n), using the elimination pattern of Gaussian Elimination adapted to a band matrix. A dense solver would take O(n3)O(n^3), so the structure matters for large data sets.

Worked examples

Example
Example 1: points on a straight line (tiny)

Take the points (0,0)(0,0), (1,1)(1,1) and (2,2)(2,2).

  1. The equation for the middle point is 4M1=6(0−2⋅1+2)4M_1 = 6(0 - 2 \cdot 1 + 2).
  2. The right side is 6⋅0=06 \cdot 0 = 0, so M1=0M_1 = 0.
  3. Both pieces have zero curvature, so each is a straight line.
  4. The first piece is S0(x)=xS_0(x) = x, which passes through (0,0)(0,0) and (1,1)(1,1).

Interpretation: when the data already lie on a line, the spline is that line. A spline never adds curvature the data do not call for.

Example
Example 2: the thrown ball (standard)

Use the three heights from How it works: 00, 11 and 00 at times 00, 11 and 22 seconds.

  1. Solve the middle equation: 4M1=6(0−2+0)=−124M_1 = 6(0 - 2 + 0) = -12, so M1=−3M_1 = -3.
  2. The first piece is S0(x)=1.5x−0.5x3S_0(x) = 1.5x - 0.5x^3.
  3. The second piece, by symmetry of the data about t=1t = 1, is S1(x)=1.5(2−x)−0.5(2−x)3S_1(x) = 1.5(2 - x) - 0.5(2 - x)^3.
  4. At t=1t = 1, both pieces give 11. Their first derivatives both give 00, so the top of the throw is smooth.
  5. At t=0.5t = 0.5, the spline gives 0.68750.6875 m. The straight line gives 0.50.5 m.

Interpretation: the spline puts the ball higher during the climb than a straight line would, and it reaches the top with zero slope, as a real throw does.

Example
Example 3: a sensor between samples (realistic)

A sensor reports the same thrown ball's height at times 0, 1 and 2 seconds. An engineer wants the height at t=0.25t = 0.25 seconds, where no reading exists.

  1. Use the first piece, since 0.250.25 lies in [0,1][0,1]: S0(x)=1.5x−0.5x3S_0(x) = 1.5x - 0.5x^3.
  2. Compute 1.5⋅0.25=0.3751.5 \cdot 0.25 = 0.375.
  3. Compute 0.253=0.0156250.25^3 = 0.015625, then 0.5⋅0.015625=0.00781250.5 \cdot 0.015625 = 0.0078125.
  4. Subtract: 0.375−0.0078125=0.36718750.375 - 0.0078125 = 0.3671875.
  5. The straight line gives 0.250.25 m, so the spline's estimate is higher by about 0.120.12 m.

Interpretation: the estimate is in the gap between two samples, and the smooth curve is the more plausible guess for a ball under gravity. Straight lines underestimate the height during a steady climb.

Try it: at t=1.5t = 1.5 seconds, the second piece S1S_1 applies. Using S1(x)=1.5(2−x)−0.5(2−x)3S_1(x) = 1.5(2 - x) - 0.5(2 - x)^3, find the height at t=1.5t = 1.5, and say whether it matches the value at t=0.5t = 0.5 by symmetry.

Answer: 2−1.5=0.52 - 1.5 = 0.5, so S1=1.5⋅0.5−0.5⋅0.125=0.75−0.0625=0.6875S_1 = 1.5 \cdot 0.5 - 0.5 \cdot 0.125 = 0.75 - 0.0625 = 0.6875 m. It matches t=0.5t = 0.5 because the data are symmetric about t=1t = 1.

Common mistakes and tradeoffs

It is tempting to think that a cubic spline is the same as one high-degree polynomial through all the points. In fact a single polynomial through many equally spaced points can swing wildly near the ends, a problem known as Runge's phenomenon. A spline keeps each piece low-degree and local, so a change in one point affects only nearby pieces.

It is tempting to think that a spline never overshoots. In fact it can. Near a sudden jump in the data, the curve may bulge above or below the nearby points before settling. Splines are smooth, not guaranteed monotone; shape-preserving variants exist when overshoot matters.

It is tempting to think that the natural end condition is always correct. In fact it forces zero curvature at the ends, which is wrong if the true function is curved there. If you know the slope at the ends, a clamped spline fixes the slope instead.

When to use it: to draw a smooth curve through measured or sampled points, to estimate values between them, and whenever you need continuous slope and curvature. When not to use it: when the data are noisy and you want a trend rather than an exact fit, where a smoothing spline or a least-squares fit is better; and when the data have sharp jumps. Alternatives: Linear Interpolation for the simplest and most robust estimate; a single polynomial for very few points; least-squares fitting for noisy data.

History

Before computers, draftsmen drew smooth curves through points with thin flexible strips of wood or metal, called splines, held in place by weights called ducks. The strip takes the shape that bends least, which is close to the mathematical spline.

In the 1940s and 1950s, Isaac Schoenberg and others formalised the piecewise-polynomial idea mathematically, and the name "spline" was adopted for this class of functions. Solving for the curvatures became practical once digital computers could handle the linear system quickly.

Splines caught on in engineering and then in computer graphics because they combine a simple local form with smoothness at the joints. They are now a standard part of numerical libraries and drawing software.

Recap

  • A cubic spline uses one cubic piece per gap between data points.
  • The pieces are joined so that value, slope and curvature match at every interior point, which gives a smooth curve.
  • The unknown curvatures at the data points satisfy a small tridiagonal linear system, solved by Gaussian Elimination.
  • Once the curvatures are known, each piece is evaluated like any cubic.
  • Splines avoid the wild swings of one high-degree polynomial, though they can still overshoot near sharp changes.

Next, read Finite Element Method to see piecewise polynomial functions used to solve equations, or Empirical Mode Decomposition to see a related use in signal analysis.

Check yourself

QuestionRecall: what three things match at each interior point where two cubic pieces of a cubic spline meet?Show answer

The value, the first derivative (slope) and the second derivative (curvature) of the two pieces all match there. Matching the slope and curvature is what makes the joint smooth.

QuestionCompute: a natural spline on the points (0,0)(0,0), (1,2)(1,2) and (2,0)(2,0) has middle curvature given by 4M1=6(y0−2y1+y2)4M_1 = 6(y_0 - 2y_1 + y_2) with h=1h = 1. What is M1M_1?Show answer

y0−2y1+y2=0−4+0=−4y_0 - 2y_1 + y_2 = 0 - 4 + 0 = -4, so 4M1=6⋅(−4)=−244M_1 = 6 \cdot (-4) = -24, and M1=−6M_1 = -6.

QuestionWhy: why does a cubic spline avoid the large swings that a single high-degree polynomial can show between many equally spaced points?Show answer

A spline uses low-degree pieces that each depend mostly on nearby points. A single high-degree polynomial must pass through every point at once, so its shape is forced everywhere and can oscillate strongly near the ends of the interval.

QuestionApplied: a lab records a chemical concentration every 10 minutes, and the readings rise and then drop sharply after a reaction starts. A colleague wants a spline through the readings to estimate the concentration at minute 25. Is a cubic spline a good choice?Show answer

It is a reasonable choice for an exact interpolation of clean readings, since it gives a smooth estimate between samples. But because the readings change sharply, the spline may overshoot near the change, so the estimate could be off by more than expected. A shape-preserving method, or a check against linear interpolation, is worth doing.

Sources