MATLAB - Linear Interpolation
MATLAB - 1-D Data Interpolation
Section titled “MATLAB - 1-D Data 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.
The interp1 Function
Section titled “The interp1 Function”Syntax
Section titled “Syntax”% Basic syntaxvq = interp1(x, v, xq)
% Specify interpolation methodvq = interp1(x, v, xq, method)
% Specify method and extrapolation behaviorvq = interp1(x, v, xq, method, extrapolation)
% Use default sample points [1, 2, 3, ...]vq = interp1(v, xq, ...)Parameters Explained
Section titled “Parameters Explained”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). Ifvis 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 toxq.
Choosing the Right Interpolation Method
Section titled “Choosing the Right Interpolation Method”The method option significantly changes the result. Here are the most common choices:
| Method | Description & 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 pointsv = sin(x); % Original valuesxq = 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 dataplot(x, v, 'o', 'MarkerSize', 10, 'DisplayName', 'Original Samples');
% Plot interpolated resultsplot(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;Expected Output
Section titled “Expected Output”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.
Extrapolation: Beyond the Data Range
Section titled “Extrapolation: Beyond the Data Range”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.
Example 2: Extrapolation
Section titled “Example 2: Extrapolation”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 extrapolationvq_extrap = interp1(x, v, xq, 'linear', 'extrap');
% Perform interpolation and return 0 for points outside the rangevq_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;Expected Output
Section titled “Expected Output”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 columnsv = [x.^2, x.^3, exp(x)];
xq = 0:0.1:4;
% Interpolate all three columns at oncevq = interp1(x, v, xq, 'pchip');
% Plottingfigure;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');