1

I am asked to investigate the different types of interpolation using Matlab for the following points:

x = [32 34 35 36 37 38]
y = [26 28 31 30 29 25]

and find the values for f(33), f(33.5) and f(35).

When plotting x and y, I can see that f(33) should be around 27, which is also what I get using interp1(x,y,33).

I am not sure if this is the correct way of using the Cubic spline interpolation function but I used spline(x,y,33) and got ans = 24.3906.

Shouldn't I still get the same value for f(33) no matter what type of interpolation I use?

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
P.ython
  • 63
  • 1
  • 8
  • 7
    If you use a different interpolation method, it's normal that you potentially get a different result no ? – obchardon Nov 28 '18 at 11:47

2 Answers2

6

Wanted to add this to @hazeiio's answer which I upvoted. You can see this illustrates the point well.

The interpolation method greatly affects the values obtained between data points (see image below). You'll see it can be dangerous to blindly call an interpolation method without checking to see what could go wrong.

% MATLAB R2017a
x = [32 34 35 36 37 38];
y = [26 28 31 30 29 25];  

xTgts = [33 33.5 35 37.25 37.5 37.75];

% Interpolation between data points depends on method
Linear = interp1(x,y,xTgts)
Spline = interp1(x,y,xTgts,'spline')    % Equivalent to spline(x,y,xTgts) yet faster somehow
Cubic = interp1(x,y,xTgts,'pchip')

As pointed out, they will all match the data exactly (see image below).

% Interpolation of data points will match
Linear = interp1(x,y,x)
Spline = interp1(x,y,x,'spline')    
Cubic = interp1(x,y,x,'pchip')

Interpolation Visualization


Code for illustration

step = 0.01;
xTest = (32:step:38)';

figure, hold on, box on
p(1) = plot(x,y,'ks','DisplayName','Data')
p(2) = plot(xTest,interp1(x,y,xTest),'b-','DisplayName','Linear')
p(3) = plot(xTest,interp1(x,y,xTest,'spline'),'r-','DisplayName','Spline')
p(4) = plot(xTest,interp1(x,y,xTest,'pchip'),'g-','DisplayName','Cubic')
legend('show')

% Options
xlabel('X')
ylabel('Y')
title('Interpolation Example')
for k = 1:4, p(k).LineWidth = 2; end
axis equal
xlim([31 39])
ylim([24 32])

Reference:
Interpolation (wiki)
Interpolation Methods

Dangers of Interpolation
Higher Order Interpolation is a Bad Idea

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
  • Excellent! This is exactly what the other answer lacks: an explanation for why the interpolated value differs so much. Do you know what the boundary conditions are that `spline` uses? From your graph it looks like it sets the gradient on the left and right points to the same value. – Cris Luengo Nov 28 '18 at 17:20
  • @CrisLuengo, great question on the `spline`. I don't know and would have to delve into the documentation or look at the output arguments to see. Will try to do if I can find time. – SecretAgentMan Nov 28 '18 at 19:45
  • Nah, don’t delve into docs for my sake, I asked because I’m too lazy to find out by myself. :p – Cris Luengo Nov 28 '18 at 19:59
  • @CrisLuengo Cubic spline interpolation uses third degree polynomial with boundary conditions for first and second derivative for each point given. It makes up to `4n+2` equations, so additional boundary conditions are set for first and last points, so that second derivate at them is equal to 0. – hazeiio Nov 29 '18 at 07:37
  • 1
    @hazeiio: OK, you make me look this up, because your statement doesn’t match what I see in the plot here. It turns out that MATLAB uses the “not-a-knot” end condition, which, according to Wikipedia, means that the last two equations define the 3rd derivative at the last knots to be equal. That is, it doesn’t specify any property for the derivatives at the end points, it instead adds a derivative to the two points further in. – Cris Luengo Nov 29 '18 at 14:07
  • I also leaned that you can specify end point conditions in the form of the derivative that the two end points by adding two values to the `y` parameter. – Cris Luengo Nov 29 '18 at 14:08
  • @CrisLuengo You are indeed correct. For some reason I always thought MATLAB uses natural spline, instead of not-a-knot. – hazeiio Nov 29 '18 at 14:36
1

Interpolation makes sure the values of the interpolated function are the same as the values of original function at the points you provided. Looking at your code, it means that f(35) will be same and will be equal to 31 for every interpolation method.

However, depending on the method of interpolation, the curve between each of the consecutive methods will vary, hence giving you different values, which is expected.

hazeiio
  • 195
  • 6