Interp 1
Interp 1
Syntax
vq = interp1(x,v,xq)
vq = interp1(x,v,xq,method)
vq = interp1(x,v,xq,method,extrapolation)
vq = interp1(v,xq)
vq = interp1(v,xq,method)
vq = interp1(v,xq,method,extrapolation)
pp = interp1(x,v,method,'pp')
Description
example
Interpolate the function at the query points and plot the result.
figure
vq1 = interp1(x,v,xq);
plot(x,v,'o',xq,vq1,':.');
xlim([0 2*pi]);
title('(Default) Linear Interpolation');
Define a set of query points that fall between the default points, 1:9. In this case, the
default points are 1:9 because v contains 9 values.
xq = 1.5:8.5;
Evaluate v at xq.
vq = interp1(v,xq);
Plot the real part of the result in red and the imaginary part in blue.
figure
plot(x,real(v),'*r',xq,real(vq),'-r');
hold on
plot(x,imag(v),'*b',xq,imag(vq),'-b');
Interpolation of Dates and Times
Try This Example
Interpolate time-stamped data points.
Consider a data set containing temperature readings that are measured every four hours.
Create a table with one day's worth of data and plot the data.
x = (datetime(2016,1,1):hours(4):datetime(2016,1,2))';
x.Format = 'MMM dd, HH:mm';
T = [31 25 24 41 43 33 31]';
WeatherData = table(x,T,'VariableNames',{'Time','Temperature'})
WeatherData=7x2 table
Time Temperature
_____________ ___________
Specify the query points, xq, that extend beyond the domain of x.
xq = [0 0.5 1.5 5.5 6];
Specify the query points, xq, that extend beyond the domain of x.
xq = [-4 -2.5 -0.5 0.5 2.5 4];
Now evaluate v at xq using the 'pchip' method and assign any values outside the
domain of x to the value, 27.
vq = interp1(x,v,xq,'pchip',27)
vq =
Create matrix v, whose columns are the vectors, v1, v2, and v3.
v = [v1 v2 v3];
Define a set of query points, xq, to be a finer sampling over the range of x.
xq = -5:0.1:5;
h = gca;
h.XTick = -5:5;
The circles in the plot represent v, and the solid lines represent vq.
Input Arguments
collapse all
x — Sample points
vector
Sample points, specified as a row or column vector of real numbers. The values
in x must be distinct. The length of xmust conform to one of the following requirements:
• If v is a vector, then length(x) must equal length(v).
• If v is an array, then length(x) must equal size(v,1).
Example: [1 2 3 4 5 6 7 8 9 10]
Example: 1:10
Example: [3 7 11 15 19 23 27 31]'
Data Types: single | double | duration | datetime
v — Sample values
vector | matrix | array
Sample values, specified as a vector, matrix, or array of real or complex numbers. If v is
a matrix or an array, then each column contains a separate set of 1-D values.
If v contains complex numbers, then interp1 interpolates the real and imaginary parts
separately.
Example: rand(1,10)
Example: rand(10,1)
Example: rand(10,3)
Data Types: single | double | duration | datetime
Complex Number Support: Yes
xq — Query points
scalar | vector | matrix | array
Query points, specified as a scalar, vector, matrix, or array of real numbers.
Example: 5
Example: 1:0.05:10
Example: (1:0.05:10)'
Example: [0 1 2 7.5 10]
Data Types: single | double | duration | datetime
method — Interpolation method
'linear' (default)
| 'nearest' | 'next' | 'previous' | 'spline' | 'pchip' | 'cubic' | 'makima'
Interpolation method, specified as one of the options in this table.
The behavior
ofinterp1(...,'cubic')will change in a
future release. In a future release, this method
will perform cubic convolution.
Output Arguments
collapse all
vq — Interpolated values
scalar | vector | matrix | array
Interpolated values, returned as a scalar, vector, matrix, or array. The size of vq depends
on the shape of v and xq.
Shape of
Shape of v Size of Vq Example
xq
pp — Piecewise polynomial
structure
Piecewise polynomial, returned as a structure that you can pass to the ppval function for
evaluation.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
See Also
griddedInterpolant | interp2 | interp3 | interpn