0
0
MATLABdata~5 mins

Interpolation (interp1) in MATLAB

Choose your learning style9 modes available
Introduction

Interpolation helps you find values between known points. It fills in gaps smoothly.

You have temperature readings every hour and want to estimate values every 30 minutes.
You know the speed of a car at certain times and want to find speed at times in between.
You have a few points on a curve and want to draw a smooth line through them.
You want to estimate missing data points in a measurement series.
Syntax
MATLAB
vq = interp1(x, v, xq)
vq = interp1(x, v, xq, method)
vq = interp1(x, v, xq, method, extrapolation)

x and v are vectors of known data points.

xq are the query points where you want to find values.

Examples
Finds value at 2.5 by linear interpolation between points.
MATLAB
x = [1 2 3 4 5];
v = [10 20 30 40 50];
xq = 2.5;
vq = interp1(x, v, xq);
Finds value at 3.7 using nearest neighbor method.
MATLAB
vq = interp1(x, v, 3.7, 'nearest');
Estimates value at 6 by linear extrapolation beyond known points.
MATLAB
vq = interp1(x, v, 6, 'linear', 'extrap');
Sample Program

This program finds the value at 2.5 using two methods: linear and nearest neighbor interpolation.

MATLAB
x = [0 1 2 3 4];
v = [0 2 4 6 8];
xq = 2.5;
vq_linear = interp1(x, v, xq, 'linear');
vq_nearest = interp1(x, v, xq, 'nearest');
fprintf('Linear interpolation at %.1f: %.2f\n', xq, vq_linear);
fprintf('Nearest interpolation at %.1f: %.2f\n', xq, vq_nearest);
OutputSuccess
Important Notes

Make sure x is strictly increasing.

Common methods: 'linear', 'nearest', 'spline', 'pchip'.

Use 'extrap' to allow values outside the known range.

Summary

Interpolation estimates values between known points.

interp1 is used for 1D interpolation in MATLAB.

You can choose different methods for smoothness or speed.