Cubic spline interpolation vs polynomial interpolation

前端 未结 2 1576
情书的邮戳
情书的邮戳 2020-12-07 00:06

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]
         


        
2条回答
  •  鱼传尺愫
    2020-12-07 00:36

    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')
    


    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

提交回复
热议问题