Interpolation with MATLAB
Syntax#
-
zy = interp1(x,y);
-
zy = interp1(x,y,‘method’);
-
zy = interp1(x,y,‘method’,‘extrapolation’);
-
zy = interp1(x,y,zx);
-
zy = interp1(x,y,zx,‘method’);
-
zy = interp1(x,y,zx,‘method’,‘extrapolation’);
Piecewise interpolation 2 dimensional
We initialize the data:
[X,Y] = meshgrid(1:2:10);
Z = X.*cos(Y) - Y.*sin(X);
The surface looks like the following.
Now we set the points where we want to interpolate:
[Vx,Vy] = meshgrid(1:0.25:10);
We now can perform nearest interpolation,
Vz = interp2(X,Y,Z,Vx,Vy,'nearest');
linear interpolation,
Vz = interp2(X,Y,Z,Vx,Vy,'linear');
cubic interpolation
Vz = interp2(X,Y,Z,Vx,Vy,'cubic');
or spline interpolation:
Vz = interp2(X,Y,Z,Vx,Vy,'spline');
Piecewise interpolation 1 dimensional
We will use the following data:
x = 1:5:50;
y = randi([-10 10],1,10);
Hereby x
and y
are the coordinates of the data points and z
are the points we need information about.
z = 0:0.25:50;
One way to find the y-values of z is piecewise linear interpolation.
z_y = interp1(x,y,z,'linear');
Hereby one calculates the line between two adjacent points and gets z_y
by assuming that the point would be an element of those lines.
interp1
provides other options too like nearest interpolation,
z_y = interp1(x,y,z, 'nearest');
next interpolation,
z_y = interp1(x,y,z, 'next');
previous interpolation,
z_y = interp1(x,y,z, 'previous');
Shape-preserving piecewise cubic interpolation,
z_y = interp1(x,y,z, 'pchip');
cubic convolution, z_y = interp1(x,y,z, ‘v5cubic’);
and spline interpolation
z_y = interp1(x,y,z, 'spline');
Hereby are nearst, next and previous interpolation piecewise constant interpolations.
Polynomial interpolation
We initialize the data we want to interpolate:
x = 0:0.5:10;
y = sin(x/2);
This means the underlying function for the data in the interval [0,10] is sinusoidal. Now the coefficients of the approximating polynómials are being calculated:
p1 = polyfit(x,y,1);
p2 = polyfit(x,y,2);
p3 = polyfit(x,y,3);
p5 = polyfit(x,y,5);
p10 = polyfit(x,y,10);
Hereby is x
the x-value and y
the y-value of our data points and the third number is the order/degree of the polynomial. We now set the grid we want to compute our interpolating function on:
zx = 0:0.1:10;
and calculate the y-values:
zy1 = polyval(p1,zx);
zy2 = polyval(p2,zx);
zy3 = polyval(p3,zx);
zy5 = polyval(p5,zx);
zy10 = polyval(p10,zx);
One can see that the approximation error for the sample gets smaller when the degree of the polynomial increases.
While the approximation of the straight line in this example has larger errors the order 3 polynomial approximates the sinus function in this intervall relatively good.
The interpolation with order 5 and order 10 polynomials has almost no apprroximation error.
However if we consider the out of sample performance one sees that too high orders tend to overfit and therefore perform badly out of sample. We set
zx = -10:0.1:40;
p10 = polyfit(X,Y,10);
p20 = polyfit(X,Y,20);
and
zy10 = polyval(p10,zx);
zy20 = polyval(p20,zx);
If we take a look at the plot we see that the out of sample performance is best for the order 1
and keeps getting worse with increasing degree.