%% Data Interpolation with spline, pchip, and makima
% Compare the interpolation results produced by spline, pchip, and makima for two different data sets. These functions all perform different forms of piecewise cubic Hermite interpolation. Each function differs in how it computes the slopes of the interpolant, leading to different behaviors when the underlying data has flat areas or undulations.
%
% Compare the interpolation results on sample data that connects flat regions. Create vectors of x values, function values at those points y, and query points xq. Compute interpolations at the query points using  spline, pchip, and makima. 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);
m = makima(x,y,xq1);
plot(x,y,'o',xq1,p,'-',xq1,s,'-.',xq1,m,'--')
legend('Sample Points','pchip','spline','makima','Location','SouthEast')

% In this case, pchip and makima have similar behavior in that they avoid overshoots and can accurately connect the flat regions.
%
% Perform a second comparison using an oscillatory sample function.

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

% When the underlying function is oscillatory, spline and makima capture the movement between points better than pchip, which is aggressively flattened near local extrema.
% Copyright 2012 The MathWorks, Inc.
%
%