1. Home
  2. Interpolation & calculus

Interpolation · tables to curves

One-Dimensional Interpolation (interp1)

Eigenfield editorial team ·

One-dimensional interpolation estimates the value of a function at a point between known samples. Given a table of x values with matching y values and a query point x, a routine like interp1 returns the estimated y. The central choice is the method, which sets how the points on either side of the query influence the answer.

Tables arise everywhere: calibration curves, measured spectra, yearly data. The question is rarely whether to interpolate but how smooth the connecting curve should be. The answer changes the number, as the example below shows.

Locating the interval

Every method starts by finding the interval [x_k, x_{k+1}] that contains the query. With the table sorted, a binary search finds it in about log₂ n comparisons. For many queries against one table, sort the queries too and walk through both lists once.

Reject unsorted or repeated x values, since a repeated x with two different y values has no function to interpolate. Decide in advance what to do outside the table: return an error, a missing marker, or an extrapolated value.

Linear interpolation

Inside the interval, the straight line through the two end points gives

y = y_k + (x − x_k) / (x_{k+1} − x_k) · (y_{k+1} − y_k)

For the points (2, 3) and (4, 2) and a query at x = 3, the fraction is 1/2 and y = 3 − 0.5 = 2.5. The curve is continuous but has corners at the table points.

How good is it? For a function with second derivative bounded by M, the error is at most M·h²/8, where h is the spacing. Test with f(x) = x² between 1 and 2: the straight line at 1.5 gives (1 + 4)/2 = 2.5 against the true 2.25. The difference is 0.25, and the bound is 2 × 1²/8 = 0.25, so for a parabola the bound is reached exactly.

Polynomial (Lagrange) interpolation

A single polynomial through all n points has degree n − 1 and the form y = ∑ y_i · L_i(x), where L_i(x) equals 1 at x_i and 0 at the other nodes. Take the three points (1, 2), (2, 3) and (4, 2) again and query x = 3:

  • L₁(3) = (3−2)(3−4) / ((1−2)(1−4)) = −1/3
  • L₂(3) = (3−1)(3−4) / ((2−1)(2−4)) = 1
  • L₃(3) = (3−1)(3−2) / ((4−1)(4−2)) = 1/3

The weights add to −1/3 + 1 + 1/3 = 1, as they must. The estimate is 2(−1/3) + 3(1) + 2(1/3) = 3. The quadratic through the three points is y = 2.5x − x²/2. It peaks at x = 2.5 with value 3.125 and returns 3 at x = 3, while the straight segment gave 2.5. Neither is wrong; they are different assumptions about the data.

High-degree polynomials through many equally spaced points can swing wildly near the ends. That is why long tables are interpolated piecewise, with low-degree pieces, rather than with one polynomial.

Splines and nearest values

A cubic spline joins cubic pieces so that the value, slope and curvature agree at every table point. The result looks smooth, which suits plotted curves and positions that must not jerk, and the price is a tridiagonal linear system to solve once per table. The solution of that system is a case of the LU factorization.

Nearest-neighbor interpolation returns the y of the closest table point. It is the right choice for categorical or staircase data such as tax brackets, where averaging two neighbors would produce a value that never occurs.

Extrapolation and checks

Outside the table the same formulas still produce numbers, but they are guesses of rapidly declining quality. A linear extension follows the end slope forever; a polynomial extension diverges quickly. Treat extrapolated values as warnings.

Two tests catch most mistakes: the interpolant must return the table value exactly at each table point, and for linear data every method must reproduce the line. A third test compares slopes by finite differences of the output to see whether a spline really is smooth.

The two-variable version of the problem is covered on the two-dimensional interpolation page, and neighboring numerical topics are gathered under interpolation and calculus. Terms such as node and knot are defined in the glossary.

Back to top