Cubic Spline Interpolation

Work in progress. This note is still being written and incomplete.

Given nodes a=x0<x1<⋯<xn=ba = x_0 < x_1 < \cdots < x_n = b and ff defined on [a,b][a, b], a cubic spline interpolant for ff is a function SS satisfying:

  • S(x)S(x) is a cubic polynomial Sj(x)S_j(x) on [xj,xj+1][x_j, x_{j+1}], for each j=0,1,…,n−1j = 0, 1, \ldots, n-1 (piecewise cubic)
  • Sj(xj)=f(xj)S_j(x_j) = f(x_j) and Sj(xj+1)=f(xj+1)S_j(x_{j+1}) = f(x_{j+1}), for each j=0,1,…,n−1j = 0, 1, \ldots, n-1 (interpolation)
  • Sj+1(xj+1)=Sj(xj+1)S_{j+1}(x_{j+1}) = S_j(x_{j+1}), for each j=0,1,…,n−2j = 0, 1, \ldots, n-2 (value continuity, implied by interpolation above)
  • Sj+1′(xj+1)=Sj′(xj+1)S_{j+1}'(x_{j+1}) = S_j'(x_{j+1}), for each j=0,1,…,n−2j = 0, 1, \ldots, n-2 (slope continuity)
  • Sj+1′′(xj+1)=Sj′′(xj+1)S_{j+1}''(x_{j+1}) = S_j''(x_{j+1}), for each j=0,1,…,n−2j = 0, 1, \ldots, n-2 (curvature continuity)
  • one of the following boundary conditions holds:
    • S′′(x0)=S′′(xn)=0S''(x_0) = S''(x_n) = 0 (natural, aka. free, boundary)
    • S′(x0)=f′(x0)S'(x_0) = f'(x_0) and S′(xn)=f′(xn)S'(x_n) = f'(x_n) (clamped boundary)

Each SjS_j is written as

Sj(x)=aj+bj(x−xj)+cj(x−xj)2+dj(x−xj)3S_j(x) = a_j + b_j(x - x_j) + c_j(x - x_j)^2 + d_j(x - x_j)^3

giving 4n4n unknowns aj,bj,cj,dja_j, b_j, c_j, d_j, j=0,1,…,n−1j = 0, 1, \ldots, n-1, for 4n4n equations from the conditions above.

Constructing the Spline

Let hj=xj+1−xjh_j = x_{j+1} - x_j. Solve order:

  1. aj=f(xj)a_j = f(x_j), directly from interpolation.
  2. cjc_j, from the tridiagonal system plus the boundary conditions.
  3. bjb_j and djd_j, by back-substitution from the known aj,cja_j, c_j.

The interpolation condition already gives aja_j for each j=0,1,…,nj = 0, 1, \ldots, n directly, leaving bj,cj,djb_j, c_j, d_j to determine. Applying the continuity conditions to SjS_j gives, for each j=0,1,…,n−1j = 0, 1, \ldots, n-1

aj+1=aj+bjhj+cjhj2+djhj3(value continuity)bj+1=bj+2cjhj+3djhj2(slope continuity)cj+1=cj+3djhj(curvature continuity)\begin{aligned} a_{j+1} &= a_j + b_j h_j + c_j h_j^2 + d_j h_j^3 & \text{(value continuity)} \\ b_{j+1} &= b_j + 2c_j h_j + 3d_j h_j^2 & \text{(slope continuity)} \\ c_{j+1} &= c_j + 3d_j h_j & \text{(curvature continuity)} \end{aligned}

Solving the third equation for djd_j and substituting into the first two, then eliminating bjb_j, leaves a linear system in the cjc_j‘s alone

hj−1cj−1+2(hj−1+hj)cj+hjcj+1=3hj(aj+1−aj)−3hj−1(aj−aj−1),j=1,…,n−1(tridiagonal system)h_{j-1}c_{j-1} + 2(h_{j-1} + h_j)c_j + h_j c_{j+1} = \frac{3}{h_j}(a_{j+1} - a_j) - \frac{3}{h_{j-1}}(a_j - a_{j-1}), \quad j = 1, \ldots, n-1 \quad \text{(tridiagonal system)}

Once {cj}j=0n\{c_j\}_{j=0}^n is known, the remaining coefficients follow directly

bj=1hj(aj+1−aj)−hj3(2cj+cj+1),dj=cj+1−cj3hj(back-substitution)b_j = \frac{1}{h_j}(a_{j+1} - a_j) - \frac{h_j}{3}(2c_j + c_{j+1}), \qquad d_j = \frac{c_{j+1} - c_j}{3h_j} \quad \text{(back-substitution)}

Natural Boundary Conditions

S′′(x0)=S′′(xn)=0S''(x_0) = S''(x_n) = 0 gives c0=cn=0c_0 = c_n = 0 directly. The resulting (n+1)×(n+1)(n+1) \times (n+1) system for c0,…,cnc_0, \ldots, c_n is strictly diagonally dominant, so it has a unique solution, and thus ff has a unique natural spline interpolant on the given nodes.

Clamped Boundary Conditions

S′(x0)=f′(x0)S'(x_0) = f'(x_0) and S′(xn)=f′(xn)S'(x_n) = f'(x_n) give 2 more equations

2h0c0+h0c1=3h0(a1−a0)−3f′(x0)(left clamp)2h_0 c_0 + h_0 c_1 = \frac{3}{h_0}(a_1 - a_0) - 3f'(x_0) \quad \text{(left clamp)} hn−1cn−1+2hn−1cn=3f′(xn)−3hn−1(an−an−1)(right clamp)h_{n-1}c_{n-1} + 2h_{n-1}c_n = 3f'(x_n) - \frac{3}{h_{n-1}}(a_n - a_{n-1}) \quad \text{(right clamp)}

This (n+1)×(n+1)(n+1) \times (n+1) system is also strictly diagonally dominant, so ff has a unique clamped spline interpolant on the given nodes.

Example

Construct a natural cubic spline through (1,2),(2,3),(3,5)(1, 2), (2, 3), (3, 5).

Nodes x0=1,x1=2,x2=3x_0 = 1, x_1 = 2, x_2 = 3, so n=2n = 2, h0=h1=1h_0 = h_1 = 1, and a0=2,a1=3,a2=5a_0 = 2, a_1 = 3, a_2 = 5 directly from interpolation. Natural boundary conditions give c0=c2=0c_0 = c_2 = 0. The system for c1c_1 (the single interior equation, j=1j = 1) is

h0c0+2(h0+h1)c1+h1c2=3h1(a2−a1)−3h0(a1−a0)=3(5−3)−3(3−2)=3h_0 c_0 + 2(h_0 + h_1)c_1 + h_1 c_2 = \frac{3}{h_1}(a_2 - a_1) - \frac{3}{h_0}(a_1 - a_0) = 3(5-3) - 3(3-2) = 3 4c1=3  ⟹  c1=344c_1 = 3 \implies c_1 = \tfrac{3}{4} b0=(a1−a0)−13(2c0+c1)=1−14=34,d0=c1−c03=14b_0 = (a_1 - a_0) - \tfrac{1}{3}(2c_0 + c_1) = 1 - \tfrac{1}{4} = \tfrac{3}{4}, \qquad d_0 = \tfrac{c_1 - c_0}{3} = \tfrac{1}{4} b1=(a2−a1)−13(2c1+c2)=2−12=32,d1=c2−c13=−14b_1 = (a_2 - a_1) - \tfrac{1}{3}(2c_1 + c_2) = 2 - \tfrac{1}{2} = \tfrac{3}{2}, \qquad d_1 = \tfrac{c_2 - c_1}{3} = -\tfrac{1}{4} S(x)={2+34(x−1)+14(x−1)3,x∈[1,2]3+32(x−2)+34(x−2)2−14(x−2)3,x∈[2,3]S(x) = \begin{cases} 2 + \tfrac{3}{4}(x-1) + \tfrac{1}{4}(x-1)^3, & x \in [1, 2] \\[4pt] 3 + \tfrac{3}{2}(x-2) + \tfrac{3}{4}(x-2)^2 - \tfrac{1}{4}(x-2)^3, & x \in [2, 3] \end{cases}

With Multiple Interior Nodes

The 3-point example above has only 1 interior node, so its tridiagonal system is a single equation. With 4 nodes, there are 2 interior equations, and solving for c1,c2c_1, c_2 requires actual elimination.

Construct a natural cubic spline through (0,0),(1,1),(2,0),(3,1)(0, 0), (1, 1), (2, 0), (3, 1).

Nodes give n=3n = 3, h0=h1=h2=1h_0 = h_1 = h_2 = 1, and a0=0,a1=1,a2=0,a3=1a_0 = 0, a_1 = 1, a_2 = 0, a_3 = 1. Natural boundary conditions give c0=c3=0c_0 = c_3 = 0. The tridiagonal system for j=1,2j = 1, 2 is

4c1+c2=3h1(a2−a1)−3h0(a1−a0)=3(0−1)−3(1−0)=−6c1+4c2=3h2(a3−a2)−3h1(a2−a1)=3(1−0)−3(0−1)=6\begin{aligned} 4c_1 + c_2 &= \tfrac{3}{h_1}(a_2-a_1) - \tfrac{3}{h_0}(a_1-a_0) = 3(0-1) - 3(1-0) = -6 \\ c_1 + 4c_2 &= \tfrac{3}{h_2}(a_3-a_2) - \tfrac{3}{h_1}(a_2-a_1) = 3(1-0) - 3(0-1) = 6 \end{aligned}

Eliminating c2c_2 (multiply the second row by 44 and subtract the first)

4(c1+4c2)−(4c1+c2)=4(6)−(−6)  ⟹  15c2=30  ⟹  c2=24(c_1 + 4c_2) - (4c_1 + c_2) = 4(6) - (-6) \implies 15c_2 = 30 \implies c_2 = 2

Back-substituting into the first row

4c1+2=−6  ⟹  c1=−24c_1 + 2 = -6 \implies c_1 = -2 b0=1−13(−2)=53,d0=c1−c03=−23b_0 = 1 - \tfrac{1}{3}(-2) = \tfrac{5}{3}, \quad d_0 = \tfrac{c_1 - c_0}{3} = -\tfrac{2}{3} b1=−1−13(−4+2)=−13,d1=c2−c13=43b_1 = -1 - \tfrac{1}{3}(-4+2) = -\tfrac{1}{3}, \quad d_1 = \tfrac{c_2 - c_1}{3} = \tfrac{4}{3} b2=1−13(4)=−13,d2=c3−c23=−23b_2 = 1 - \tfrac{1}{3}(4) = -\tfrac{1}{3}, \quad d_2 = \tfrac{c_3 - c_2}{3} = -\tfrac{2}{3} S(x)={53x−23x3,x∈[0,1]1−13(x−1)−2(x−1)2+43(x−1)3,x∈[1,2]−13(x−2)+2(x−2)2−23(x−2)3,x∈[2,3]S(x) = \begin{cases} \tfrac{5}{3}x - \tfrac{2}{3}x^3, & x \in [0, 1] \\[4pt] 1 - \tfrac{1}{3}(x-1) - 2(x-1)^2 + \tfrac{4}{3}(x-1)^3, & x \in [1, 2] \\[4pt] -\tfrac{1}{3}(x-2) + 2(x-2)^2 - \tfrac{2}{3}(x-2)^3, & x \in [2, 3] \end{cases}

Clamped Boundary Example

Construct a clamped cubic spline through (0,0),(1,1),(2,0)(0, 0), (1, 1), (2, 0) with f′(0)=1f'(0) = 1, f′(2)=−1f'(2) = -1.

Nodes give n=2n = 2, h0=h1=1h_0 = h_1 = 1, and a0=0,a1=1,a2=0a_0 = 0, a_1 = 1, a_2 = 0. The interior equation (j=1j = 1) plus the 2 clamp equations give 3 equations in c0,c1,c2c_0, c_1, c_2

c0+4c1+c2=3h1(a2−a1)−3h0(a1−a0)=3(−1)−3(1)=−6(interior)2c0+c1=3h0(a1−a0)−3f′(x0)=3(1)−3(1)=0(left clamp)c1+2c2=3f′(x2)−3h1(a2−a1)=3(−1)−3(−1)=0(right clamp)\begin{aligned} c_0 + 4c_1 + c_2 &= \tfrac{3}{h_1}(a_2-a_1) - \tfrac{3}{h_0}(a_1-a_0) = 3(-1) - 3(1) = -6 \quad \text{(interior)} \\ 2c_0 + c_1 &= \tfrac{3}{h_0}(a_1-a_0) - 3f'(x_0) = 3(1) - 3(1) = 0 \quad \text{(left clamp)} \\ c_1 + 2c_2 &= 3f'(x_2) - \tfrac{3}{h_1}(a_2-a_1) = 3(-1) - 3(-1) = 0 \quad \text{(right clamp)} \end{aligned}

From the clamp equations, c1=−2c0c_1 = -2c_0 and c2=−12c1=c0c_2 = -\tfrac{1}{2}c_1 = c_0. Substituting into the interior equation

c0+4(−2c0)+c0=−6  ⟹  −6c0=−6  ⟹  c0=1,c1=−2,c2=1c_0 + 4(-2c_0) + c_0 = -6 \implies -6c_0 = -6 \implies c_0 = 1, \quad c_1 = -2, \quad c_2 = 1 b0=1−13(0)=1,d0=c1−c03=−1b_0 = 1 - \tfrac{1}{3}(0) = 1, \quad d_0 = \tfrac{c_1-c_0}{3} = -1 b1=−1−13(−3)=0,d1=c2−c13=1b_1 = -1 - \tfrac{1}{3}(-3) = 0, \quad d_1 = \tfrac{c_2-c_1}{3} = 1 S(x)={x+x2−x3,x∈[0,1]1−2(x−1)2+(x−1)3,x∈[1,2]S(x) = \begin{cases} x + x^2 - x^3, & x \in [0, 1] \\[4pt] 1 - 2(x-1)^2 + (x-1)^3, & x \in [1, 2] \end{cases}

Checking the endpoints confirms the clamp held: S0′(0)=b0=1=f′(0)S_0'(0) = b_0 = 1 = f'(0), and S1′(2)=b1+2c1h1+3d1h12=0−4+3=−1=f′(2)S_1'(2) = b_1 + 2c_1h_1 + 3d_1h_1^2 = 0 - 4 + 3 = -1 = f'(2).

Error

For f∈C4[a,b]f \in C^4[a,b] with max⁡a≤x≤b∣f(4)(x)∣=M\max_{a \leq x \leq b} |f^{(4)}(x)| = M, the unique clamped cubic spline interpolant SS satisfies, for all x∈[a,b]x \in [a, b]

∣f(x)−S(x)∣≤5M384max⁡0≤j≤n−1(xj+1−xj)4(error bound)|f(x) - S(x)| \leq \frac{5M}{384} \max_{0 \leq j \leq n-1} (x_{j+1} - x_j)^4 \quad \text{(error bound)}
Written by September 28, 2026 3 min read
Was this helpful?