Skip to content

MATLAB - Linear Interpolation

Interpolation is the process of estimating unknown values that fall between known data points. One-dimensional (1-D) interpolation is used when you have a set of data points (x, v) and you want to find the value vq at a new query point xq. MATLAB’s primary function for this is interp1.

% Basic syntax
vq = interp1(x, v, xq)
% Specify interpolation method
vq = interp1(x, v, xq, method)
% Specify method and extrapolation behavior
vq = interp1(x, v, xq, method, extrapolation)
% Use default sample points [1, 2, 3, ...]
vq = interp1(v, xq, ...)
  • x: A vector of sample point locations (the independent variable). Must be monotonic.
  • v: A vector or matrix of corresponding sample values (the dependent variable). If v is a matrix, interpolation is performed on each column independently.
  • xq: A vector or matrix of query points where you want to estimate values.
  • vq: The output, containing the interpolated values corresponding to xq.

The method option significantly changes the result. Here are the most common choices:

MethodDescription & Best Use Case
'linear' (Default)Connects points with straight lines. Fast and simple. Best for dense or nearly-linear data.
'spline'Cubic spline interpolation. Produces a very smooth curve that passes through all points. Good for smooth, continuous signals.
'pchip'Shape-preserving piecewise cubic interpolation. Prevents overshoots and preserves monotonicity. Excellent for data that should not oscillate between points.
'makima'Modified Akima cubic Hermite interpolation. Similar to 'pchip' but often more accurate at capturing the shape of the data without overshoots. A recommended modern alternative to 'spline' and 'pchip'.
'nearest'”Nearest neighbor” interpolation. The output is the value of the nearest sample point. Useful for discrete or categorical data.

Example 1: Comparing Interpolation Methods

Section titled “Example 1: Comparing Interpolation Methods”
%% --- Define Original and Query Data ---
x = 0:10; % Original sample points
v = sin(x); % Original values
xq = 0:0.25:10; % Query points (higher resolution)
%% --- Perform Interpolation ---
vq_linear = interp1(x, v, xq, 'linear');
vq_spline = interp1(x, v, xq, 'spline');
vq_makima = interp1(x, v, xq, 'makima');
%% --- Plotting for Comparison ---
figure;
hold on; % Allow multiple plots on the same axes
% Plot original coarse data
plot(x, v, 'o', 'MarkerSize', 10, 'DisplayName', 'Original Samples');
% Plot interpolated results
plot(xq, vq_linear, ':', 'LineWidth', 1.5, 'DisplayName', 'Linear');
plot(xq, vq_spline, '--', 'LineWidth', 1.5, 'DisplayName', 'Spline');
plot(xq, vq_makima, '-', 'LineWidth', 2, 'DisplayName', 'Makima');
hold off;
% --- Final Touches for a Professional Plot ---
title('Comparison of Interpolation Methods');
xlabel('X');
ylabel('sin(X)');
legend('show', 'Location', 'southwest');
grid on;

A plot will be generated. It will show the original sin(x) values as large blue circles. Three lines will trace the paths between these points: a green dotted line showing the straight connections of linear interpolation; a red dashed line showing the smooth, sometimes overshooting curve of spline interpolation; and a thick blue solid line showing the smooth but non-overshooting curve from the ‘makima’ method.

What happens if a query point in xq is outside the range of x? By default, interp1 returns NaN. You can change this behavior with the extrapolation argument.

x = 1:5;
v = [12, 16, 10, 18, 15];
xq = 0:0.5:6; % Query points include values outside [1, 5]
% Perform linear interpolation and allow extrapolation
vq_extrap = interp1(x, v, xq, 'linear', 'extrap');
% Perform interpolation and return 0 for points outside the range
vq_no_extrap = interp1(x, v, xq, 'linear', 0);
figure;
hold on;
plot(x, v, 'bo-', 'LineWidth', 2, 'MarkerSize', 8, 'DisplayName', 'Original Data');
plot(xq, vq_extrap, 'rx--', 'LineWidth', 1.5, 'DisplayName', 'With Extrapolation (''extrap'')');
plot(xq, vq_no_extrap, 'g*:', 'LineWidth', 1.5, 'DisplayName', 'With Extrapolation (value 0)');
hold off;
title('Handling Out-of-Range Query Points');
xlabel('x');
ylabel('Value');
legend('show');
grid on;

The plot will show the original data as solid blue circles. A dashed red line will extend beyond the original data range at both ends, continuing the linear trend. A dotted green line with stars will follow the interpolated values within the data range but drop to zero for all points outside the range.

Practical Tip: Working with Multiple Datasets

Section titled “Practical Tip: Working with Multiple Datasets”

interp1 is highly optimized for vector and matrix operations. If you have multiple datasets sampled at the same x points, you can place them in the columns of v and interpolate them all in a single call. This is much faster than using a loop.

x = (0:4)'; % Must be a column vector for this example
% Three datasets in three columns
v = [x.^2, x.^3, exp(x)];
xq = 0:0.1:4;
% Interpolate all three columns at once
vq = interp1(x, v, xq, 'pchip');
% Plotting
figure;
plot(x, v, 'o', 'MarkerSize', 8);
hold on;
plot(xq, vq, '--', 'LineWidth', 1.5);
hold off;
title('Interpolating Multiple Datasets');
legend('Original Set 1', 'Original Set 2', 'Original Set 3', 'Location', 'northwest');