TExcelTAD
Home › MATLAB / Octave › Interpolation

Interpolation

Interpolation estimates values between known data points. You measured a temperature every hour but need it at 10:20; a table lists values at round numbers but you need one in between. MATLAB's interp1 handles one-dimensional data with several methods.

interp1: linear interpolation

interp1(x, y, xq) takes known points (x, y) and returns estimates at the query points xq. The default method draws straight lines between neighbouring points:

matlab · try it yourself
hour = [6 9 12 15 18];
temp = [11 16 22 21 15];
fprintf('at 10:00 -> %.2f\n', interp1(hour, temp, 10));
fprintf('at 13:30 -> %.2f\n', interp1(hour, temp, 13.5));
q = 6:1.5:18;
fprintf('%5.1f h: %5.2f\n', [q; interp1(hour, temp, q)]);
at 10:00 -> 18.00
at 13:30 -> 21.50
  6.0 h: 11.00
  7.5 h: 13.50
  9.0 h: 16.00
 10.5 h: 19.00
 12.0 h: 22.00
 13.5 h: 21.50
 15.0 h: 21.00
 16.5 h: 18.00
 18.0 h: 15.00

The x-values must be distinct and are usually sorted. At the known points, interpolation returns the known values exactly.

Methods

Method Result
'linear' (default) Straight lines between points
'nearest' Value of the closest point (a step function)
'previous', 'next' Value of the point before / after
'pchip' Smooth curve that preserves the shape (no overshoot)
'spline' Very smooth cubic spline (may overshoot)
matlab · try it yourself
x = [0 1 2 3];
y = [0 1 8 27];          % samples of x^3
xq = 1.4;
methods = {'nearest', 'linear', 'pchip', 'spline'};
for k = 1:numel(methods)
    fprintf('%-8s %.4f\n', methods{k}, interp1(x, y, xq, methods{k}));
end
fprintf('exact    %.4f\n', xq^3);
nearest  1.0000
linear   3.8000
pchip    2.7338
spline   2.7440
exact    2.7440

Here the spline reproduces x³ exactly, because a cubic spline through samples of a cubic polynomial is that polynomial (with the default "not-a-knot" end conditions).

Outside the data: extrapolation

By default interp1 returns NaN for query points outside the range of x. Pass 'extrap' to extend the method beyond the ends, but be careful: extrapolated values are guesses.

matlab · try it yourself
x = [1 2 3];
y = [10 20 30];
v = interp1(x, y, [0 2.5 5]);
disp(isnan(v))
disp(interp1(x, y, [0 2.5 5], 'linear', 'extrap'))
disp(interp1(x, y, [0 5], 'nearest', 0))     % a fixed value outside instead
  1  0  1
    0   25   50
   0   0

Practical example: resampling

Sensors rarely sample at the times you want. Interpolation puts two series on a common time grid so you can combine them:

matlab · try it yourself
tA = [0 2 4 6 8];      vA = [1.0 1.4 1.9 2.1 2.6];
tB = [0 3 6 9];        vB = [5.0 4.1 3.5 3.3];
t = 0:2:8;                                  % common grid
vBonA = interp1(tB, vB, t);
fprintf('%d s: A = %.2f, B = %.2f, A+B = %.2f\n', [t; vA; vBonA; vA + vBonA]);
0 s: A = 1.00, B = 5.00, A+B = 6.00
2 s: A = 1.40, B = 4.40, A+B = 5.80
4 s: A = 1.90, B = 3.90, A+B = 5.80
6 s: A = 2.10, B = 3.50, A+B = 5.60
8 s: A = 2.60, B = 3.37, A+B = 5.97

Two dimensions

For gridded data (values on a rectangle of x and y), interp2(X, Y, Z, xq, yq) works the same way; with a matrix alone it uses row/column indices as coordinates:

matlab · try it yourself
Z = [10 20
     30 40];
fprintf('centre of the square: %g\n', interp2(Z, 1.5, 1.5));
fprintf('middle of the top edge: %g\n', interp2(Z, 1.5, 1));
centre of the square: 25
middle of the top edge: 15

interp2 takes the x (column) coordinate first and the y (row) coordinate second. For scattered, non-gridded points, MATLAB and Octave offer griddata.

Test yourself

What is interp1([0 10], [0 100], 2.5)?

  1. 2.5
  2. 25
  3. 100
  4. NaN

What does interp1 return by default for a query point outside the data range?

  1. 0
  2. The nearest end value
  3. NaN
  4. An error

Which method gives a smooth curve without overshooting the data?

  1. 'nearest'
  2. 'linear'
  3. 'pchip'
  4. 'previous'