%% Data Interpolation Using spline and pchip
% Compare the interpolation results produced by spline and pchip for two different functions.
% Create vectors of x values, function values at those points y, and query points xq. Compute interpolations at the query points using both spline and pchip. Plot the interpolated function values at the query points for comparison.

x = -3:3; 
y = [-1 -1 -1 0 1 1 1]; 
xq1 = -3:.01:3;
p = pchip(x,y,xq1);
s = spline(x,y,xq1);
plot(x,y,'o',xq1,p,'-',xq1,s,'-.')
legend('Sample Points','pchip','spline','Location','SouthEast')

% In this case, pchip is favorable since it does not oscillate as freely between the sample points.
%
% Perform a second comparison using an oscillatory sample function.

x = 0:25;
y = besselj(1,x);
xq2 = 0:0.01:25;
p = pchip(x,y,xq2);
s = spline(x,y,xq2);
plot(x,y,'o',xq2,p,'-',xq2,s,'-.')
legend('Sample Points','pchip','spline')

% When the underlying function is oscillatory, spline captures the movement between points better than pchip.
% Copyright 2012 The MathWorks, Inc.