Skip to content
Tutorials Center
My learning

Topics

Lesson 21 of 37 ยท Interpolation and Curve Fitting

Piecewise Interpolation and Cubic Splines

Connect data points with linear pieces or smooth cubic splines. Derive the tridiagonal system, solve it from scratch and compare with SciPy's CubicSpline.

One high-degree polynomial through many points misbehaves, as the Runge phenomenon shows. The fix used almost everywhere in practice is piecewise interpolation: use a different low-degree polynomial on each interval between neighbouring data points and make the pieces join nicely. Plotting libraries, animation curves, CAD and data tools all do this.

We start with straight lines, then move to cubic splines, which join cubic pieces so smoothly that the eye cannot see the joints.

Piecewise linear interpolation

Connect consecutive points with straight segments. On [xi, x(i+1)] the value is

s(x) = yi + (y(i+1) - yi) / (x(i+1) - xi) * (x - xi)

It is continuous, never overshoots the data and is trivial to compute. The cost is corners at every node, and an error of order h^2 where h is the spacing. NumPy implements it as np.interp.

Python Piecewise linear interpolation

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

What a cubic spline is

A cubic spline is a function S(x) that on each interval is a cubic polynomial and, at the interior nodes, has matching value, first derivative and second derivative. With n + 1 points there are n cubics, so 4n unknown coefficients. The conditions give n + 1 (pass through the points) + n - 1 (continuity of S') + n - 1 (continuity of S'') = 3n - 1 equations. Two more are needed, called boundary conditions:

  • Natural: S'' = 0 at both ends (the curve straightens out).
  • Clamped: prescribe S' at both ends (when you know the slopes).
  • Not-a-knot: the third derivative is also continuous at the second and second-to-last nodes. This is SciPy's default and usually the most accurate when you know nothing about the ends.

The tridiagonal system

Let Mi = S''(xi) be the unknown second derivatives and hi = x(i+1) - xi. Writing each cubic in terms of Mi and enforcing continuity of S' gives, for interior nodes i = 1 ... n-1:

h(i-1) M(i-1) + 2 (h(i-1) + hi) Mi + hi M(i+1) = 6 * ( (y(i+1) - yi)/hi - (yi - y(i-1))/h(i-1) )

For the natural spline, M0 = Mn = 0. The matrix is tridiagonal and diagonally dominant, so it is cheap and safe to solve (O(n) with the Thomas algorithm, as in linear systems). Once the M values are known, on interval i:

S(x) = Mi (x(i+1) - x)^3 / (6 hi) + M(i+1) (x - xi)^3 / (6 hi) + (yi/hi - Mi hi/6)(x(i+1) - x) + (y(i+1)/hi - M(i+1) hi/6)(x - xi)

Python Natural cubic spline from scratch

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The two columns agree, which confirms the derivation. The key properties are worth stating plainly: the result is C^2 (continuous curvature), the error is of order h^4 for smooth functions, and a change to one data point affects the curve everywhere, but the influence dies off geometrically with distance.

Comparing linear, spline and a single polynomial

Python Error comparison on sin(x)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Going from 9 to 17 nodes halves the spacing: the linear error drops by about 4 (order h^2) and the spline error by about 30 (order h^4, roughly 16 or better). Here the global polynomial wins, but only because the sine is an exceptionally gentle function and the nodes are few. On the Runge function from the previous lesson, or on data with a kink, the polynomial fails and the spline stays well behaved.

Splines can still wiggle if the data themselves jump. When monotone data must stay monotone (cumulative totals, say), use scipy.interpolate.PchipInterpolator, which preserves shape at the cost of a lower smoothness (C^1).

Using SciPy in practice

Python
from scipy.interpolate import CubicSpline
cs = CubicSpline(x, y)          # bc_type="not-a-knot" by default
cs(2.5)                         # value
cs(2.5, 1)                      # first derivative
cs.integrate(0, 4)              # definite integral

The spline object also gives derivatives and integrals for free, which links to numerical differentiation and quadrature later in the course. Remember that splines are for interpolation: they pass through every point, so on noisy data use a smoothing spline or a least-squares fit instead.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which continuity does a cubic spline have at its interior nodes?

    A cubic spline is C^2: S, S' and S'' are all continuous across interior nodes.
  2. 2. What does the natural boundary condition impose?

    Natural means S''(x0) = S''(xn) = 0, so the curve straightens out at the ends.
  3. 3. What type of linear system gives the second derivatives of a spline?

    Each equation involves only M(i-1), Mi and M(i+1), so the matrix is tridiagonal.
  4. 4. If you halve the node spacing, roughly how much does the cubic spline error shrink?

    The error is O(h^4), so halving h divides it by about 2^4 = 16.

Summary

  • Piecewise interpolation avoids the oscillations of a single high-degree polynomial.
  • Linear pieces are simple, continuous and error O(h^2); cubic splines are C^2 with error O(h^4).
  • A spline needs two boundary conditions: natural, clamped or not-a-knot.
  • The unknown second derivatives solve a tridiagonal system.
  • Use CubicSpline for smooth data and PchipInterpolator when shape must be preserved.

Next: Least squares curve fitting