Cubic spline interpolation connects data points with a separate cubic curve between each pair of neighbours, and chooses those cubics so the joins are smooth: the curve, its slope and its curvature all match at every data point. You get a smooth curve through all the points without the wild swings that one high-degree polynomial can produce.
Linear interpolation, the method in our interpolation calculator, joins the points with straight segments. A spline is the smooth version of the same idea. Below is a full natural spline worked by hand, then the same result in Python.
What makes a curve a cubic spline
For points x0 < x1 < … < xn, a cubic spline S(x) is:
- A cubic on each interval [xi, xi+1].
- Through every data point: S(xi) = yi.
- Smooth at the joins: neighbouring cubics have the same first derivative (slope) and the same second derivative (curvature) at each interior point.
- Plus two end conditions, because the rules above leave two degrees of freedom. The most common choice is the natural spline: zero curvature at both ends, S″(x0) = S″(xn) = 0.
The equations for a natural spline
Let Mi = S″(xi), the curvature at each point. With equal spacing h, smoothness gives one equation for each interior point:
M(i−1) + 4 Mi + M(i+1) = 6 / h² × (y(i+1) − 2 yi + y(i−1))
For a natural spline, M0 = Mn = 0. Solve for the interior M values, then evaluate any x in the interval [xi, xi+1] with
S(x) = A yi + B y(i+1) + [(A³ − A) Mi + (B³ − B) M(i+1)] × h² / 6
where A = (x(i+1) − x)/h and B = (x − xi)/h. The first part, A yi + B y(i+1), is exactly linear interpolation. The bracket is the curvature correction.
Worked example: temperature during the day
A weather station records the air temperature every three hours:
| Time (hour) | 6 | 9 | 12 | 15 |
|---|---|---|---|---|
| Temperature (°C) | 12 | 18 | 24 | 22 |
Estimate the temperature at 10:30 (x = 10.5) and 13:30 (x = 13.5).
Step 1: set up the equations
Here h = 3, and M0 = M3 = 0 (natural spline). There are two unknowns, M1 and M2.
At x = 9: 4 M1 + M2 = 6/9 × (24 − 2 × 18 + 12) = 6/9 × 0 = 0
At x = 12: M1 + 4 M2 = 6/9 × (22 − 2 × 24 + 18) = 6/9 × (−8) = −5.333333
Step 2: solve for the curvatures
From the first equation, M2 = −4 M1. Substitute into the second: M1 − 16 M1 = −5.333333, so
- M1 = 0.355556
- M2 = −1.422222
M2 is negative because the temperature peaks around midday: the curve bends downward there.
Step 3: evaluate at 10:30
10.5 lies between 9 and 12, so A = (12 − 10.5)/3 = 0.5 and B = 0.5.
- Linear part: 0.5 × 18 + 0.5 × 24 = 21.0
- A³ − A = B³ − B = 0.125 − 0.5 = −0.375
- Curvature part: [−0.375 × 0.355556 + (−0.375) × (−1.422222)] × 9/6 = −0.375 × (−1.066667) × 1.5 = 0.6
S(10.5) = 21.0 + 0.6 = 21.6 °C
Step 4: evaluate at 13:30
Between 12 and 15, again A = B = 0.5, with M2 = −1.422222 and M3 = 0.
- Linear part: 0.5 × 24 + 0.5 × 22 = 23.0
- Curvature part: −0.375 × (−1.422222) × 1.5 = 0.8
S(13.5) = 23.0 + 0.8 = 23.8 °C
Spline vs straight lines
| Time | Linear interpolation | Natural cubic spline |
|---|---|---|
| 7:30 | 15.0 °C | 14.8 °C |
| 10:30 | 21.0 °C | 21.6 °C |
| 13:30 | 23.0 °C | 23.8 °C |
The straight lines put the warmest moment exactly at the 12:00 reading. The spline lets the curve keep rising a little after noon and peak at about 24.2 °C around 12:35, which matches how a real temperature curve rounds off at the top.
Check it with the calculator The linear values come straight from the linear interpolation calculator. For 13:30, enter X1 = 12, Y1 = 24, X2 = 15, Y2 = 22, X3 = 13.5 and leave Y3 empty: the result is 23.00. The spline adds 0.8 °C of curvature correction on top of that.
The spline as ordinary cubics
If you need the pieces in the usual form S(x) = a + b(x − xi) + c(x − xi)² + d(x − xi)³, the coefficients are:
| Interval | a | b | c | d |
|---|---|---|---|---|
| 6 to 9 | 12 | 1.822222 | 0 | 0.019753 |
| 9 to 12 | 18 | 2.355556 | 0.177778 | −0.098765 |
| 12 to 15 | 24 | 0.755556 | −0.711111 | 0.079012 |
Here c = Mi/2, d = (M(i+1) − Mi)/(6h) and b = (y(i+1) − yi)/h − h(2Mi + M(i+1))/6. Check: on the middle piece at x = 10.5, 18 + 2.355556 × 1.5 + 0.177778 × 2.25 − 0.098765 × 3.375 = 21.6.
The same spline in Python
SciPy’s CubicSpline solves the same system:
from scipy.interpolate import CubicSpline
hours = [6, 9, 12, 15]
temps = [12, 18, 24, 22]
spline = CubicSpline(hours, temps, bc_type="natural")
print(spline(10.5)) # 21.6
print(spline(13.5)) # 23.8
Leave out bc_type and SciPy uses the “not-a-knot” end condition instead. That changes the result: 24.5 °C at 13:30 and a peak of about 24.6 °C near 13:04. With only four points the end condition matters a lot. With many points it mainly affects the first and last intervals.
Choosing the end condition
| End condition | What it assumes | When to use it |
|---|---|---|
| Natural | Zero curvature at both ends | Default when you know nothing about the ends |
| Clamped | A known slope at each end | When you know the start and end rates of change |
| Not-a-knot | First two pieces are one cubic, same at the other end | SciPy and MATLAB default, good for smooth functions |
| Periodic | Ends join smoothly | Repeating data such as angles or daily cycles |
When to use a cubic spline
- Smooth physical data such as temperatures, sensor curves and calibration data, where kinks from linear interpolation are unrealistic.
- Curves you need to differentiate, because the slope and curvature are continuous.
- Many data points, where a single high-degree polynomial would oscillate.
When not to use one
- Noisy data. A spline goes through every point, including the noise. Use a smoothing spline or a least-squares fit.
- Data with sharp corners or steps. A spline rounds them off and can overshoot. Linear or monotone interpolation (for example PCHIP) is safer.
- Reading engineering and finance tables. Tables are designed for linear interpolation between neighbouring rows. See how to interpolate steam tables.
- Extrapolation. The spline’s last cubic keeps bending beyond the data. See linear extrapolation for the risks.
For a quick overview of all the methods, read types of interpolation. For two-input grids, see bilinear interpolation.
FAQ
What is the difference between a cubic spline and cubic interpolation? Cubic interpolation fits one cubic through four points. A cubic spline uses a different cubic for each interval and joins them smoothly, so it can cover any number of points.
Why is it called a natural spline? Setting the end curvature to zero mimics a thin flexible strip (a draftsman’s spline) that is free at both ends and naturally straightens there.
Does a cubic spline pass through all the data points? Yes. Interpolating splines always pass through every point. Smoothing splines do not.
Can I calculate a cubic spline in Excel? Not with a built-in function. You can set up the curvature equations above in cells and solve them, or use a scripting language. For linear interpolation in Excel, see our Excel guide.
Where can I read the formal definition? Wolfram MathWorld’s cubic spline page gives the derivation of the system solved above.