Mathwords logoMathwords

Cubic Spline — Definition, Formula & Examples

A cubic spline is a piecewise function made of cubic polynomials that passes through a set of data points, with the pieces joined so that the overall curve is smooth (continuous first and second derivatives) at every junction.

Given n+1n+1 data points (x0,y0),(x1,y1),,(xn,yn)(x_0, y_0), (x_1, y_1), \ldots, (x_n, y_n) with x0<x1<<xnx_0 < x_1 < \cdots < x_n, a cubic spline S(x)S(x) consists of nn cubic polynomials Si(x)=ai+bi(xxi)+ci(xxi)2+di(xxi)3S_i(x) = a_i + b_i(x - x_i) + c_i(x - x_i)^2 + d_i(x - x_i)^3 on each subinterval [xi,xi+1][x_i, x_{i+1}], satisfying interpolation conditions Si(xi)=yiS_i(x_i) = y_i, continuity of SS, SS', and SS'' at each interior knot, plus two boundary conditions (commonly S(x0)=S(xn)=0S''(x_0) = S''(x_n) = 0 for a natural spline).

Key Formula

Si(x)=ai+bi(xxi)+ci(xxi)2+di(xxi)3S_i(x) = a_i + b_i(x - x_i) + c_i(x - x_i)^2 + d_i(x - x_i)^3
Where:
  • aia_i = Value of the spline at knot $x_i$, so $a_i = y_i$
  • bib_i = First-derivative coefficient on interval $[x_i, x_{i+1}]$
  • cic_i = Second-derivative coefficient (related to $S''(x_i)/2$)
  • did_i = Third-derivative coefficient on the interval

How It Works

You start with a table of data points (called knots). On each interval between consecutive knots, you define a separate cubic polynomial. Then you enforce that adjacent polynomials agree in value, first derivative, and second derivative at shared knots. This gives you a system of linear equations. Solving the system (typically for the second-derivative coefficients cic_i first) yields all the polynomial coefficients. The result is a single smooth curve that passes exactly through every data point without the wild oscillations that a single high-degree polynomial can produce.

Worked Example

Problem: Construct the natural cubic spline through the three points (0,0)(0, 0), (1,1)(1, 1), (2,0)(2, 0).
Set up: There are two subintervals: [0,1][0,1] and [1,2][1,2], so two cubic pieces S0(x)S_0(x) and S1(x)S_1(x). Each interval has width h=1h = 1. The natural boundary conditions give c0=0c_0 = 0 and c2=0c_2 = 0.
Solve for interior second derivative: The standard tridiagonal equation for the single interior knot (i=1i=1) is hc0+4hc1+hc2=3h(y22y1+y0)h\,c_0 + 4h\,c_1 + h\,c_2 = \frac{3}{h}(y_2 - 2y_1 + y_0). Substituting h=1h=1, c0=0c_0=0, c2=0c_2=0:
4c1=3(02+0)=6    c1=324c_1 = 3(0 - 2 + 0) = -6 \implies c_1 = -\tfrac{3}{2}
Compute remaining coefficients: For S0S_0 on [0,1][0,1]: a0=0a_0=0, d0=c1c03h=12d_0 = \frac{c_1 - c_0}{3h} = -\frac{1}{2}, and b0=y1y0hh3(2c0+c1)=1+12=32b_0 = \frac{y_1-y_0}{h} - \frac{h}{3}(2c_0+c_1) = 1 + \frac{1}{2} = \frac{3}{2}. For S1S_1 on [1,2][1,2]: a1=1a_1=1, d1=c2c13h=12d_1 = \frac{c_2 - c_1}{3h} = \frac{1}{2}, and b1=y2y1hh3(2c1+c2)=1+1=0b_1 = \frac{y_2-y_1}{h} - \frac{h}{3}(2c_1+c_2) = -1+1 = 0.
S0(x)=32x12x3,S1(x)=132(x1)2+12(x1)3S_0(x) = \tfrac{3}{2}x - \tfrac{1}{2}x^3, \quad S_1(x) = 1 - \tfrac{3}{2}(x-1)^2 + \tfrac{1}{2}(x-1)^3
Answer: The natural cubic spline is S0(x)=32x12x3S_0(x)=\frac{3}{2}x - \frac{1}{2}x^3 on [0,1][0,1] and S1(x)=132(x1)2+12(x1)3S_1(x)=1 - \frac{3}{2}(x-1)^2 + \frac{1}{2}(x-1)^3 on [1,2][1,2].

Why It Matters

Cubic splines are the standard interpolation tool in computer-aided design (CAD), computer graphics, and data fitting because they produce visually smooth curves without oscillation. In numerical analysis and engineering courses, understanding splines prepares you for finite element methods and signal processing.

Common Mistakes

Mistake: Forgetting that you need boundary conditions to determine a unique spline.
Correction: With n+1n+1 data points there are 4n4n unknown coefficients but only 4n24n-2 equations from interpolation and smoothness. The two extra boundary conditions (e.g., natural: S=0S''=0 at endpoints) close the system.