Cubic Splines
A cubic spline joins data points with a different cubic polynomial on each interval between neighboring points. The pieces are chosen so the curve passes through every point and has continuous first and second derivatives where pieces meet. A natural cubic spline also has second derivative 0 at both ends.
Before this lesson: Interpolation Error, Piecewise Functions and Higher-Order Derivatives
The idea: a flexible ruler through the points
Interpolation error showed that one high-degree polynomial through many points can swing wildly near the ends. A spline avoids this by keeping the degree low: it uses a separate cubic on each interval and glues the pieces together smoothly.
The name comes from drafting. A spline was a thin, flexible strip of wood bent to pass through pins on a drawing. The strip settles into the smoothest shape it can, with no corners and no sudden changes in curvature. A cubic spline copies that: matching the first derivative removes corners, and matching the second derivative keeps the bending (concavity) continuous.
Past the last pin, the loose end of the strip is straight, so it has no bending there. That is the natural end condition: the second derivative is 0 at the first and last points.
Cubic spline formula and conditions
For points (x₀, y₀), …, (xₙ, yₙ) with x₀ < x₁ < ⋯ < xₙ, the spline on the interval [xᵢ, xᵢ₊₁] is
S_i(x) = a_i + b_i(x - x_i) + c_i(x - x_i)^2 + d_i(x - x_i)^3, \qquad x_i \le x \le x_{i+1}The n pieces have 4n unknown coefficients. They are fixed by these conditions:
- Each piece passes through both of its endpoints: 2n conditions.
- First derivatives match at the n − 1 interior points.
- Second derivatives match at the n − 1 interior points.
- Two end conditions, such as the natural condition S″(x₀) = S″(xₙ) = 0.
That makes 2n + 2(n − 1) + 2 = 4n conditions. With hᵢ = xᵢ₊₁ − xᵢ and aᵢ = yᵢ, the conditions reduce to one equation for the cᵢ at each interior point i = 1, …, n − 1:
h_{i-1}c_{i-1} + 2(h_{i-1} + h_i)c_i + h_i c_{i+1} = 3\left(\dfrac{a_{i+1} - a_i}{h_i} - \dfrac{a_i - a_{i-1}}{h_{i-1}}\right)For a natural spline, c₀ = cₙ = 0 (since S″ = 2c). Once the cᵢ are known, the other coefficients follow:
b_i = \dfrac{a_{i+1} - a_i}{h_i} - \dfrac{h_i(2c_i + c_{i+1})}{3}, \qquad d_i = \dfrac{c_{i+1} - c_i}{3h_i}- Set aᵢ = yᵢ and find each spacing hᵢ.
- Set c₀ = cₙ = 0 and solve the system for c₁, …, cₙ₋₁.
- Find each bᵢ and dᵢ from the formulas.
- Write each piece and check it at its endpoints.
Cubic spline examples
Example 1: A natural spline through three points
Find the natural cubic spline through (0, 0), (1, 1) and (2, 0).
h_0 = h_1 = 1,\quad a_0 = 0,\ a_1 = 1,\ a_2 = 0
Read off the spacings and the values.
c_0 = c_2 = 0
Natural end conditions.
2(1 + 1)c_1 = 3\left(\dfrac{0 - 1}{1} - \dfrac{1 - 0}{1}\right)Write the equation for the one interior point, i = 1.
4c_1 = 3(-2) = -6
Simplify both sides.
c_1 = -1.5
Divide both sides by 4.
b_0 = \dfrac{1 - 0}{1} - \dfrac{1(2 \cdot 0 + (-1.5))}{3} = 1 + 0.5 = 1.5Find b₀.
d_0 = \dfrac{-1.5 - 0}{3(1)} = -0.5Find d₀.
b_1 = \dfrac{0 - 1}{1} - \dfrac{1(2(-1.5) + 0)}{3} = -1 + 1 = 0Find b₁.
d_1 = \dfrac{0 - (-1.5)}{3(1)} = 0.5Find d₁.
S_1(2) = 1 + 0 - 1.5(1)^2 + 0.5(1)^3 = 0
Check that the second piece reaches (2, 0).
Answer: S₀(x) = 1.5x − 0.5x³ on [0, 1] and S₁(x) = 1 − 1.5(x − 1)² + 0.5(x − 1)³ on [1, 2]. For example, S(0.5) = 0.75 − 0.0625 = 0.6875.
Example 2: Four points and a 2-by-2 system
Find the natural cubic spline through (0, 1), (1, 2), (2, 0) and (3, 1), and use it to estimate y at x = 1.5.
4c_1 + c_2 = 3\big((0 - 2) - (2 - 1)\big) = -9
Equation for i = 1, with all hᵢ = 1 and c₀ = 0.
c_1 + 4c_2 = 3\big((1 - 0) - (0 - 2)\big) = 9
Equation for i = 2, with c₃ = 0.
16c_1 + 4c_2 = -36
Multiply the first equation by 4.
15c_1 = -45
Subtract the second equation (see elimination method).
c_1 = -3
Divide both sides by 15.
c_2 = -9 - 4(-3) = 3
Substitute into 4c₁ + c₂ = −9 and solve for c₂.
b_0 = 1 - \dfrac{2(0) + (-3)}{3} = 2,\quad d_0 = \dfrac{-3 - 0}{3} = -1First piece.
b_1 = -2 - \dfrac{2(-3) + 3}{3} = -1,\quad d_1 = \dfrac{3 - (-3)}{3} = 2Second piece.
b_2 = 1 - \dfrac{2(3) + 0}{3} = -1,\quad d_2 = \dfrac{0 - 3}{3} = -1Third piece.
S_1(1.5) = 2 - 0.5 - 3(0.25) + 2(0.125)
x = 1.5 is in [1, 2]; substitute x − 1 = 0.5.
S_1(1.5) = 2 - 0.5 - 0.75 + 0.25 = 1
Multiply, then add.
Answer: S₀(x) = 1 + 2x − x³, S₁(x) = 2 − (x − 1) − 3(x − 1)² + 2(x − 1)³ and S₂(x) = −(x − 2) + 3(x − 2)² − (x − 2)³, and the estimate at x = 1.5 is 1. This spline is the one in the graph above.
Example 3: Is it a spline? Is it natural?
Decide whether S₀(x) = 1 + x + x³ on [0, 1] and S₁(x) = 3 + 4(x − 1) + 3(x − 1)² + 2(x − 1)³ on [1, 2] form a cubic spline, and whether it is natural.
S_0(1) = 1 + 1 + 1 = 3 = S_1(1)
The values match at x = 1.
S_0'(x) = 1 + 3x^2,\quad S_0'(1) = 4 = S_1'(1)
S₁′(1) is the coefficient of (x − 1), which is 4.
S_0''(x) = 6x,\quad S_0''(1) = 6 = S_1''(1)
S₁″(1) = 2 · 3 = 6 (see higher-order derivatives).
S_0''(0) = 0
Left end: the natural condition holds.
S_1''(x) = 6 + 12(x - 1),\quad S_1''(2) = 18
Right end: 18 is not 0.
Answer: it is a cubic spline (values, slopes and second derivatives all match at x = 1), but it is not natural, because S″(2) = 18.
Example 4: Unequal spacing
Find the natural cubic spline through (0, 2), (1, 4) and (3, 2), and estimate y at x = 2.
h_0 = 1,\quad h_1 = 2
The spacings are different.
2(1 + 2)c_1 = 3\left(\dfrac{2 - 4}{2} - \dfrac{4 - 2}{1}\right)Equation for i = 1, with c₀ = c₂ = 0.
6c_1 = 3(-1 - 2) = -9
Simplify.
c_1 = -1.5
Divide both sides by 6.
b_0 = 2 - \dfrac{1(0 - 1.5)}{3} = 2.5,\quad d_0 = \dfrac{-1.5}{3} = -0.5First piece.
b_1 = -1 - \dfrac{2(2(-1.5) + 0)}{3} = -1 + 2 = 1,\quad d_1 = \dfrac{0 + 1.5}{3(2)} = 0.25Second piece; note the 3hᵢ = 6 in d₁.
S_1(2) = 4 + 1(1) - 1.5(1)^2 + 0.25(1)^3 = 3.75
x = 2 is in [1, 3]; substitute x − 1 = 1.
Answer: S₀(x) = 2 + 2.5x − 0.5x³ on [0, 1], S₁(x) = 4 + (x − 1) − 1.5(x − 1)² + 0.25(x − 1)³ on [1, 3], and the estimate at x = 2 is 3.75. Check: S₁(3) = 4 + 2 − 6 + 2 = 2.
Example 5: A smooth lane change (word problem)
A self-driving car moves 3.5 m to the left over a 50 m stretch of road. Its path y(x) must start and end with zero slope, so the steering never jerks. Find the single cubic piece with y(0) = 0, y(50) = 3.5, y′(0) = 0 and y′(50) = 0, and find the sideways offset after 20 m.
These are clamped end conditions: the slopes at the ends are given instead of the second derivatives.
y = a + bx + cx^2 + dx^3,\quad a = 0,\ b = 0
y(0) = 0 gives a = 0, and y′(0) = 0 gives b = 0.
2500c + 125000d = 3.5
y(50) = 3.5.
100c + 7500d = 0
y′(50) = 2c(50) + 3d(50)² = 0.
c = -75d
Solve the second equation for c.
-187500d + 125000d = 3.5
Substitute c = −75d into the first equation.
-62500d = 3.5
Combine like terms.
d = -0.000056,\quad c = 0.0042
Divide, then c = −75(−0.000056).
y(20) = 0.0042(400) - 0.000056(8000)
Substitute x = 20.
y(20) = 1.68 - 0.448 = 1.232
Multiply, then subtract.
Answer: y = 0.0042x² − 0.000056x³, and after 20 m the car is about 1.23 m to the left. A clamped spline with more points works the same way, with the given end slopes replacing c₀ = cₙ = 0.
Common mistakes
| Mistake | Example of the error | Fix |
|---|---|---|
| Expanding every piece in powers of x | Writing S₁ as a + bx + cx² + dx³ on [1, 2] | Write each piece in powers of (x − xᵢ), the left endpoint of its interval. |
| Using c instead of 2c for the second derivative | Setting S″(xᵢ) = cᵢ | S″(xᵢ) = 2cᵢ; the natural condition still gives c₀ = cₙ = 0. |
| Forgetting the spacing in dᵢ | d_1 = \dfrac{c_2 - c_1}{3} when h₁ = 2 | Use dᵢ = (cᵢ₊₁ − cᵢ)/(3hᵢ). |
| Evaluating with the wrong piece | Using S₀ to estimate y at x = 1.5 | Find the interval that contains x first. |
| Matching only values and slopes | Checking S(1) and S′(1) only | A cubic spline also needs matching second derivatives. |
Related methods
End conditions
| End condition | What it sets | When to use it |
|---|---|---|
| Natural | S″ = 0 at both ends | No information about the ends |
| Clamped | S′ given at both ends | Known end slopes, as in Example 5 |
| Not-a-knot | S‴ also matches at x₁ and xₙ₋₁ | Default in many software packages |
Splines vs. one high-degree polynomial
For f(x) = 1/(1 + 25x²) with 11 equally spaced nodes on [−1, 1], the degree-10 interpolating polynomial has a largest error of about 1.92 (Runge’s phenomenon), while the natural cubic spline through the same points has a largest error of about 0.022. For a smooth function and equally spaced nodes h apart, a clamped cubic spline’s error shrinks like h⁴. The simpler piecewise linear interpolation, which just joins the points with segments, has error about h² but leaves corners.
Practice problems
- How many unknown coefficients does a cubic spline through 5 points have, and how many conditions fix them?
- Find the natural cubic spline through (0, 0), (1, 2) and (2, 0).
- Use the spline from Problem 2 to estimate y at x = 1.5.
- Show that S₀(x) = x² + x³ on [0, 1] and S₁(x) = 2 + 5(x − 1) + 4(x − 1)² + d(x − 1)³ on [1, 2] form a cubic spline for any d. Find d so that S″(2) = 0. Is the spline natural?
- For the natural spline through (0, 0), (1, 1), (2, 2) and (3, 0), find c₁ and c₂.
- Find the first piece of the natural cubic spline through (0, 1), (2, 5) and (3, 4).
- Find the clamped cubic through (0, 1) and (2, 5) with slope 0 at both ends, and evaluate it at x = 1.
- Why does a natural cubic spline have second derivative 0 at its ends?
- For the points (0, 0), (1, 1) and (2, 0), compare the natural spline estimate at x = 0.5 (Example 1) with the estimate from the interpolating quadratic.
- A hiker’s GPS records elevations of 300 m, 420 m and 360 m at distances 0, 1 and 2 km. Use a natural cubic spline to estimate the elevation at 1.5 km.
Show answers
1.
5 - 1 = 4
Five points make 4 intervals, so there are 4 pieces.
4 \times 4 = 16
Each cubic piece has 4 coefficients.
2(4) + 3 + 3 + 2 = 16
Count conditions: 8 endpoint values, 3 slope matches, 3 second-derivative matches, 2 end conditions.
Answer: 16 unknowns and 16 conditions
2.
4c_1 = 3\big((0 - 2) - (2 - 0)\big) = -12
Equation for i = 1 with h = 1 and c₀ = c₂ = 0.
c_1 = -3
Divide by 4.
b_0 = 2 - \dfrac{0 + (-3)}{3} = 3,\quad d_0 = \dfrac{-3}{3} = -1First piece.
b_1 = -2 - \dfrac{2(-3) + 0}{3} = 0,\quad d_1 = \dfrac{0 - (-3)}{3} = 1Second piece.
Answer: S₀(x) = 3x − x³ on [0, 1] and S₁(x) = 2 − 3(x − 1)² + (x − 1)³ on [1, 2]
3.
S_1(1.5) = 2 - 3(0.5)^2 + (0.5)^3
x = 1.5 is in [1, 2]; substitute x − 1 = 0.5.
= 2 - 0.75 + 0.125 = 1.375
Evaluate.
Answer: about 1.375
4.
S_0(1) = 1 + 1 = 2 = S_1(1)
Values match.
S_0'(1) = 2(1) + 3(1)^2 = 5 = S_1'(1)
Slopes match (the coefficient of (x − 1) is 5).
S_0''(1) = 2 + 6(1) = 8 = S_1''(1)
Second derivatives match: 2 · 4 = 8, for any d.
S_1''(2) = 8 + 6d = 0
S₁″(x) = 8 + 6d(x − 1); set it to 0 at x = 2.
d = -\dfrac{4}{3}Solve for d.
S_0''(0) = 2
Check the left end.
Answer: d = −4/3; the spline is not natural, because S″(0) = 2
5.
4c_1 + c_2 = 3\big((2 - 1) - (1 - 0)\big) = 0
Equation for i = 1.
c_1 + 4c_2 = 3\big((0 - 2) - (2 - 1)\big) = -9
Equation for i = 2.
c_2 = -4c_1
Solve the first equation for c₂.
c_1 - 16c_1 = -9
Substitute into the second equation.
c_1 = 0.6,\quad c_2 = -2.4
Divide by −15, then c₂ = −4(0.6).
Answer: c₁ = 0.6 and c₂ = −2.4
6.
2(2 + 1)c_1 = 3\left(\dfrac{4 - 5}{1} - \dfrac{5 - 1}{2}\right) = -9h₀ = 2, h₁ = 1.
c_1 = -1.5
Divide by 6.
b_0 = \dfrac{5 - 1}{2} - \dfrac{2(0 - 1.5)}{3} = 2 + 1 = 3Find b₀.
d_0 = \dfrac{-1.5 - 0}{3(2)} = -0.25Find d₀ with 3h₀ = 6.
S_0(2) = 1 + 6 - 0.25(8) = 5
Check the right endpoint.
Answer: S₀(x) = 1 + 3x − 0.25x³ on [0, 2]
7.
y = 1 + cx^2 + dx^3
y(0) = 1 and y′(0) = 0.
4c + 8d = 4,\quad 4c + 12d = 0
y(2) = 5 and y′(2) = 4c + 12d = 0.
4d = -4,\quad d = -1,\quad c = 3
Subtract the equations, then 4c = 12.
y(1) = 1 + 3 - 1 = 3
Evaluate at x = 1.
Answer: y = 1 + 3x² − x³, and y(1) = 3
8.
Answer: the natural end condition sets S″ = 0 at the first and last points, like a flexible strip that is straight past its last pin; it is the choice to make when nothing is known about how the data behave at the ends
9.
P(x) = x(2 - x)
The quadratic through (0, 0), (1, 1), (2, 0).
P(0.5) = 0.5(1.5) = 0.75
Evaluate.
S(0.5) = 0.6875
From Example 1.
Answer: the spline gives 0.6875 and the quadratic gives 0.75; they differ because the spline is forced to have S″ = 0 at the ends
10.
4c_1 = 3\big((360 - 420) - (420 - 300)\big) = -540
Equation for i = 1 with h = 1.
c_1 = -135
Divide by 4.
b_1 = -60 - \dfrac{2(-135) + 0}{3} = 30,\quad d_1 = \dfrac{0 - (-135)}{3} = 45Second piece, since 1.5 is in [1, 2].
S_1(1.5) = 420 + 30(0.5) - 135(0.25) + 45(0.125)
Substitute x − 1 = 0.5.
= 420 + 15 - 33.75 + 5.625 = 406.875
Evaluate.
Answer: about 407 m
Cubic spline FAQ
What is a cubic spline?
It is a piecewise function made of cubic polynomials, one for each interval between data points, that passes through every point and has continuous first and second derivatives.
What is a natural cubic spline?
It is a cubic spline whose second derivative is 0 at the first and last data points. These two end conditions, together with the matching conditions, fix every coefficient.
What is the difference between natural and clamped splines?
A natural spline sets the second derivative to 0 at the ends. A clamped spline sets the first derivative at the ends to given values, which is more accurate when those slopes are known.
Why use cubic splines instead of polynomial interpolation?
Each piece has low degree, so splines do not oscillate wildly the way a single high-degree polynomial can (Runge’s phenomenon). They are also smooth enough to look natural in graphics, design and data plots.
Summary
- A cubic spline uses one cubic per interval, written in powers of (x − xᵢ).
- Values, first derivatives and second derivatives match at interior points; two end conditions finish the system.
- Natural splines use c₀ = cₙ = 0; clamped splines use given end slopes.
- Solve the tridiagonal system for the cᵢ, then find bᵢ and dᵢ.
- Splines avoid Runge’s phenomenon and give smooth curves through many points.
Previous lesson: Interpolation Error
Next lesson: Numerical Differentiation
Back to the Numerical Methods Index