 function Showpchip()
% Compares cubic spline interpolation with piecewise linear interpolation
% and pchip interpolation. It is about shape preservation. We want the
% interpolant to be increasing (decreasing)  on [x(k),x(k+1)] if
% y(k+1)>=y(k) (y(k+1)<=y(k)). If y(k) is bigger than y(k-1) and y(k+1),
% then we want the interpolant to have a local max at x(k). If y(k)
% is smaller than y(k-1) and y(k+1), then we want the interpolant to
% have a local min at x(k).
close all
% Display the function...
xVals = linspace(0,10,200)';
yVals = MyF(xVals);
plot(xVals,yVals,[0,10],[0,0],'r--')
title('f(x) = exp(-x/5)cos(2.3*x)','fontsize',18)

% Show the points of interpolation...
hold
n = 9;
x = linspace(0,10,n)';
y = MyF(x);
plot(x,y,'.k','markersize',20)
h0 = xlabel('The points of interpolation','fontsize',18);
pause

% Interpolate with a spline...
S = spline(x,y);
sVals = ppval(S,xVals);
hold on
h1=plot(xVals,sVals,'m','linewidth',2);
delete(h0)
h0 = xlabel('The spline interpolant','fontsize',18);
pause

% The piecewise linear interpolant
delete(h1)
h1 = plot(x,y,'k','linewidth',2);
delete(h0)
h0 = xlabel('The piecewise linear interpolant','fontsize',18);
pause

% Interpolate with pchip...
C = pchip(x,y);
cVals = ppval(C,xVals);
delete(h1)
h1=plot(xVals,cVals,'g','linewidth',2);
delete(h0)
h0 = xlabel('The pchip interpolant','fontsize',18);
pause
plot(xVals,sVals,'m','linewidth',2)
delete(h0)
h0 = xlabel('The pchip and spline interpolant','fontsize',18);
hold off



function [y,yp] = MyF(x)
y = exp(-x/5).*cos(2.3*x);