interp1

One-dimensional interpolation of tabulated data.

Category: Mathematics > Interpolation

Syntax

vq = interp1(x, v, xq)

Linear interpolation of the samples v(x) at the query points xq.

vq = interp1(v, xq)

Takes the sample points as 1:numel(v).

vq = interp1(x, v, xq, method)

Uses the given interpolation method.

vq = interp1(x, v, xq, method, extrapolation)

Also controls values outside the range of x: 'extrap' or a scalar fill value.

Description

interp1 fits a function through the points (x, v) and evaluates it at xq. The linear method joins neighbours with straight segments; 'nearest', 'previous' and 'next' give step functions; 'pchip' and 'makima' produce smooth curves that avoid overshoot; 'spline' gives a cubic spline with not-a-knot ends and the smoothest result.

At a tie, 'nearest' rounds toward the larger sample point. Query points outside the range of x return NaN unless an extrapolation option is given.

Input arguments

x
Sample points, a vector of distinct values.
v
Sample values. A matrix is interpolated column by column.
xq
Query points.
method
'linear' (default), 'nearest', 'next', 'previous', 'pchip', 'cubic', 'v5cubic', 'makima' or 'spline'.
extrapolation
'extrap' to extrapolate with the same method, or a number used for every point outside the data range. The default is NaN.

Output arguments

vq
Interpolated values, with the shape of xq (for vector v).

Example

Methods and out-of-range queries

x = [1 2 3 4];
v = [10 20 15 30];
interp1(x, v, 2.5)
interp1(x, v, [0 5])
interp1(x, v, [0 5], 'linear', 'extrap')
interp1(x, v, 2.5, 'pchip')
interp1(x, v, 2.5, 'spline')
Output
ans = 17.5000
ans = 1×2

   NaN   NaN
ans = 1×2

     0    45
ans = 17.5000
ans = 17.1875

See also

interp2 spline pchip makima griddedInterpolant polyfit