% TESTINTERP1  Compares interpolation methods built-in to Matlab/Octave.

xf=0:0.01:7; yf = sin(2*pi*xf/5);  % the function at high resolution
xp=0:7;      yp = sin(2*pi*xp/5);  % a few data points

 near=interp1(xp,yp,xf,'nearest'); % piecewise-constant
 
 lin=interp1(xp,yp,xf,'linear');   % piecewise-linear
 
 spl=interp1(xp,yp,xf,'spline');   % cubic spline (with not-a-knot ends, instead of 'natural')

 cub=interp1(xp,yp,xf,'pchip');    % another cubic scheme

set(0,'defaultlinelinewidth',2.0) 
plot(   xf,yf,'r', xf,near,'m', xf,lin,'b', xf,cub,'c', xf,spl,'g', ...
     xp,yp,'r*','markersize',12);
legend ('y=f(x)',  'nearest',   'linear',   'pchip',    'spline', ...
     'data points', 'location','southeast')
grid on, xlabel x, ylabel y, axis tight
