Cubic Spline Interpolation

Cubic Spline Conditions A natural cubic spline through the nodes (0, 0), (1, 1), (2, 0) and (3, 1), made of three cubic pieces. At the interior knots x1 and x2 the pieces share the same height, slope and curvature. y x x₀ x₁ x₂ x₃ same slope S″ = 0 S″ = 0 S₀ S₁ S₂
S₀, S₁ and S₂ are the three cubic pieces, one on each interval between nodes. At each interior knot x₁ and x₂ the two adjacent pieces meet at the same height, have the same slope (dashed line) and the same curvature. At the ends the natural condition sets S″ = 0.

A cubic spline is a piecewise cubic function that interpolates ordered data points and has continuous value, slope and curvature at every interior node.

Let

(x0,y0),…,(xn,yn),x0<x1<⋯<xn(x_0,y_0),\ldots,(x_n,y_n), \qquad x_0<x_1<\cdots<x_n

The interpolant is defined on [x0,xn][x_0,x_n]. Use yj=f(xj)y_j=f(x_j) when the data come from a known function ff.

On the interval [xj,xj+1][x_j,x_{j+1}], write

Sj(x)=aj+bj(x−xj)+cj(x−xj)2+dj(x−xj)3,x∈[xj,xj+1],j=0,…,n−1S_j(x)=a_j+b_j(x-x_j)+c_j(x-x_j)^2+d_j(x-x_j)^3, \qquad x\in[x_j,x_{j+1}], \qquad j=0,\ldots,n-1

At every interior node xjx_j, where j=1,…,n−1j=1,\ldots,n-1, adjacent pieces preserve 3 quantities.

  • Value
    Sj−1(xj)=Sj(xj)S_{j-1}(x_j)=S_j(x_j).
  • Slope
    Sj−1′(xj)=Sj′(xj)S_{j-1}'(x_j)=S_j'(x_j).
  • Curvature
    Sj−1′′(xj)=Sj′′(xj)S_{j-1}''(x_j)=S_j''(x_j).

Each piece passes through its 2 endpoint data points.

Sj(xj)=yj,Sj(xj+1)=yj+1S_j(x_j)=y_j, \qquad S_j(x_{j+1})=y_{j+1}

There are 4n4n coefficient unknowns. The n+1n+1 interpolation conditions, the 2(n−1)2(n-1) independent smoothness conditions and 2 boundary conditions determine them. Value continuity is already enforced by interpolation at each node.

Boundary Conditions

Natural Spline

S′′(x0)=S′′(xn)=0S''(x_0)=S''(x_n)=0. Used when endpoint slopes are unavailable.

Clamped Spline

S′(x0)=f′(x0)S'(x_0)=f'(x_0) and S′(xn)=f′(xn)S'(x_n)=f'(x_n). Used when endpoint slopes are known.

Construction

Set

hj=xj+1−xjaj=yjδj=aj+1−ajhjj=0,…,n−1\begin{aligned} h_j &= x_{j+1}-x_j \\ a_j &= y_j \\ \delta_j &= \frac{a_{j+1}-a_j}{h_j} \qquad j=0,\ldots,n-1 \end{aligned}

The system can be solved in this order:

  1. Compute hjh_j which are mostly constant.
  2. Solve for aja_j using the interpolation points.
  3. Calculate δj\delta_j.
  4. Use the boundary condition and the interior smoothness equations to solve for all cjc_j.
  5. Use the slope and curvature equations to compute all bjb_j and djd_j.
  6. Substitute aja_j, bjb_j, cjc_j and djd_j into each Sj(x)S_j(x).

Equations Required

The continuity conditions reduce to a tridiagonal system for the cjc_j values.

For each interior node xjx_j, solve

hj−1cj−1+2(hj−1+hj)cj+hjcj+1=3(δj−δj−1),j=1,…,n−1h_{j-1}c_{j-1}+2(h_{j-1}+h_j)c_j+h_jc_{j+1} =3(\delta_j-\delta_{j-1}), \qquad j=1,\ldots,n-1

The cjc_j values satisfy n−1n-1 interior equations and 2 boundary equations. The boundary equations depend on the spline type.

For a natural spline, c0=cn=0c_0=c_n=0. And for a clamped spline:

2h0c0+h0c1=3(δ0−f′(x0))hn−1cn−1+2hn−1cn=3(f′(xn)−δn−1)\begin{aligned} 2h_0c_0+h_0c_1 &= 3\bigl(\delta_0-f'(x_0)\bigr) \\ h_{n-1}c_{n-1}+2h_{n-1}c_n &=3\bigl(f'(x_n)-\delta_{n-1}\bigr) \end{aligned}

Matrix Forms

The above equations for cic_i can be expressed with a n+1n+1-variable tridiagonal matrix system.

[α0β0h02(h0+h1)h1h12(h1+h2)⋱⋱⋱hn−1αnβn]c=[r03(δ1−δ0)⋮ 3(δn−1−δn−2) rn]\begin{bmatrix} \alpha_0 & \beta_0 & & & \\ h_0 & 2(h_0+h_1) & h_1 & & \\ & h_1 & 2(h_1+h_2) & \ddots & \\ & & \ddots & \ddots & h_{n-1} \\ & & & \alpha_n & \beta_n \end{bmatrix} \mathbf{c} = \begin{bmatrix} r_0 \\ 3(\delta_1-\delta_0) \\ \vdots \\ \ 3(\delta_{n-1}-\delta_{n-2}) \\ \ r_n \end{bmatrix}

The c\mathbf{c} is the vector of all cic_i (ii from 00 to nn).

Only (α0,β0,r0)(\alpha_0,\beta_0,r_0) and (αn,βn,rn)(\alpha_n,\beta_n,r_n) change between the 2 spline types.

SplineFirst boundary rowLast boundary row
Naturalα0=1\alpha_0=1, β0=0\beta_0=0, r0=0r_0=0αn=0\alpha_n=0, βn=1\beta_n=1, rn=0r_n=0
Clampedα0=2h0\alpha_0=2h_0, β0=h0\beta_0=h_0, r0=3(δ0−f′(x0))r_0=3\bigl(\delta_0-f'(x_0)\bigr)αn=hn−1\alpha_n=h_{n-1}, βn=2hn−1\beta_n=2h_{n-1}, rn=3(f′(xn)−δn−1)r_n=3\bigl(f'(x_n)-\delta_{n-1}\bigr)

Coefficient Recovery

After solving for c0,…,cnc_0,\ldots,c_n, recover the remaining coefficients on each interval:

bj=δj−hj3(2cj+cj+1)dj=cj+1−cj3hjj=0,…,n−1\begin{aligned} b_j &= \delta_j-\frac{h_j}{3}(2c_j+c_{j+1}) \\ d_j &= \frac{c_{j+1}-c_j}{3h_j} \qquad j=0,\ldots,n-1 \end{aligned}

Error Bound

For f∈C4[x0,xn]f\in C^4[x_0,x_n], let

M=max⁡x0≤x≤xn∣f(4)(x)∣M=\max_{x_0\le x\le x_n}|f^{(4)}(x)|

If SS is the clamped cubic spline interpolant and f∈C4[x0,xn]f\in C^4[x_0,x_n], then

∣f(x)−S(x)∣≤5M384max⁡0≤j≤n−1hj4,x∈[x0,xn]|f(x)-S(x)|\le\frac{5M}{384} \max_{0\le j\le n-1}h_j^4, \qquad x\in[x_0,x_n]

The natural spline also has fourth-order convergence, but its bound has a less simple constant and depends on the endpoint behaviour.

Smaller intervals reduce the clamped error at the 4th-power rate.

Worked Examples

Example 1

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

Solution Reveal Hide
h0=h1=1,a0=2,a1=3,a2=5,c0=c2=0h_0=h_1=1, \qquad a_0=2, a_1=3, a_2=5, \qquad c_0=c_2=0

Direct Method

The only interior equation is

4c1=3((5−3)−(3−2))=34c_1=3\bigl((5-3)-(3-2)\bigr)=3

Therefore

c1=34,c_1=\frac{3}{4},

For [1,2][1,2],

b0=1−13(0+34)=34,d0=3/4−03=14b_0=1-\frac{1}{3}\left(0+\frac{3}{4}\right)=\frac{3}{4}, \qquad d_0=\frac{3/4-0}{3}=\frac{1}{4}

For [2,3][2,3],

b1=2−13(2⋅34+0)=32,d1=0−3/43=−14b_1=2-\frac{1}{3}\left(2\cdot\frac{3}{4}+0\right)=\frac{3}{2}, \qquad d_1=\frac{0-3/4}{3}=-\frac{1}{4}

The spline is

S(x)={2+34(x−1)+14(x−1)3,1≤x≤2,3+32(x−2)+34(x−2)2−14(x−2)3,2≤x≤3.S(x)= \begin{cases} 2+\frac{3}{4}(x-1)+\frac{1}{4}(x-1)^3, & 1\le x\le2,\\[4pt] 3+\frac{3}{2}(x-2)+\frac{3}{4}(x-2)^2-\frac{1}{4}(x-2)^3, & 2\le x\le3. \end{cases}

Matrix Method

The natural matrix system is

[100141001][c0c1c2]=[030]\begin{bmatrix} 1 & 0 & 0 \\ 1 & 4 & 1 \\ 0 & 0 & 1 \end{bmatrix} \begin{bmatrix} c_0\\c_1\\c_2 \end{bmatrix} = \begin{bmatrix} 0\\3\\0 \end{bmatrix}

Therefore

c0=0,c1=34,c2=0c_0=0, \qquad c_1=\frac{3}{4}, \qquad c_2=0

The coefficient recovery gives

b0=34,d0=14,b1=32,d1=−14b_0=\frac{3}{4}, \qquad d_0=\frac{1}{4}, \qquad b_1=\frac{3}{2}, \qquad d_1=-\frac{1}{4}

Therefore

S(x)={2+34(x−1)+14(x−1)3,1≤x≤2,3+32(x−2)+34(x−2)2−14(x−2)3,2≤x≤3.S(x)= \begin{cases} 2+\frac{3}{4}(x-1)+\frac{1}{4}(x-1)^3, & 1\le x\le2,\\[4pt] 3+\frac{3}{2}(x-2)+\frac{3}{4}(x-2)^2-\frac{1}{4}(x-2)^3, & 2\le x\le3. \end{cases}

Example 2

Construct the clamped cubic spline through (0,1)(0,1), (1,2)(1,2) and (2,1)(2,1), given the endpoint slopes S′(0)=1S'(0)=1 and S′(2)=−1S'(2)=-1.

Solution Reveal Hide
h0=h1=1,a0=1,a1=2,a2=1,δ0=1,δ1=−1h_0=h_1=1, \qquad a_0=1, a_1=2, a_2=1, \qquad \delta_0=1, \qquad \delta_1=-1

Direct Method

The clamped boundary equations are

2c0+c1=3(1−1)=0,c1+2c2=3(−1+1)=02c_0+c_1=3(1-1)=0, \qquad c_1+2c_2=3(-1+1)=0

The interior equation is

c0+4c1+c2=3(−1−1)=−6c_0+4c_1+c_2=3(-1-1)=-6

Therefore

c0=1,c1=−2,c2=1c_0=1, \qquad c_1=-2, \qquad c_2=1

The remaining coefficients are

b0=1,d0=−1,b1=0,d1=1b_0=1, \qquad d_0=-1, \qquad b_1=0, \qquad d_1=1

The spline is

S(x)={1+x+x2−x3,0≤x≤1,2−2(x−1)2+(x−1)3,1≤x≤2.S(x)= \begin{cases} 1+x+x^2-x^3, & 0\le x\le1,\\[4pt] 2-2(x-1)^2+(x-1)^3, & 1\le x\le2. \end{cases}

Matrix Method

The clamped matrix system is

[210141012][c0c1c2]=[0−60]\begin{bmatrix} 2 & 1 & 0 \\ 1 & 4 & 1 \\ 0 & 1 & 2 \end{bmatrix} \begin{bmatrix} c_0\\c_1\\c_2 \end{bmatrix} = \begin{bmatrix} 0\\-6\\0 \end{bmatrix}

Therefore

c0=1,c1=−2,c2=1c_0=1, \qquad c_1=-2, \qquad c_2=1

The remaining coefficients are

b0=1,d0=−1,b1=0,d1=1b_0=1, \qquad d_0=-1, \qquad b_1=0, \qquad d_1=1

The spline is

S(x)={1+x+x2−x3,0≤x≤1,2−2(x−1)2+(x−1)3,1≤x≤2.S(x)= \begin{cases} 1+x+x^2-x^3, & 0\le x\le1,\\[4pt] 2-2(x-1)^2+(x-1)^3, & 1\le x\le2. \end{cases}

Example 3

For f(x)=x4f(x)=x^4 on [0,1][0,1], use a clamped cubic spline with nodes x0=0x_0=0, x1=12x_1=\tfrac12 and x2=1x_2=1. Find the error bound.

Solution Reveal Hide

Since

f(4)(x)=24,f^{(4)}(x)=24,

we have

M=24,h=max⁡(h0,h1)=12M=24, \qquad h=\max(h_0,h_1)=\frac12

The clamped error bound is

∣f(x)−S(x)∣≤5M384h4=5(24)384(12)4=5256\lvert f(x)-S(x)\rvert \le\frac{5M}{384}h^4 =\frac{5(24)}{384}\left(\frac12\right)^4 =\frac{5}{256}

Wolfram Mathematica notebooks

A companion notebook for this note. Open it in Mathematica and evaluate the cells to reproduce the worked example.

The summary notebook defines every function used in this module, with documentation and no examples, as a starting point for your own problems.

Written by November 5, 2026 3 min read
Was this helpful?