% Example for interpolation with pw cubic Hermite, complete spline, not-a-knot spline
% for f(x) = sin(x)

x = pi/2*(0:4);                 % use 0, pi/2, ..., 2*pi as nodes
y = sin(x);                     % function values
s = cos(x);                     % slopes

xe = linspace(0,2*pi,200);      % evaluation points

yex = sin(xe);                  % exact values

yherm = hermite(x,y,s,xe);      % pw cubic Hermite interpolation
err_herm = max(abs(yherm-yex))

sa = cos(0); sb = cos(2*pi);    % slopes at left and right endpoint
ycomp = spline(x,[sa,y,sb],xe); % complete spline
err_comp = max(abs(ycomp-yex))

ynak = spline(x,y,xe);          % not-a-knot spline
err_nak = max(abs(ynak-yex))

figure(1)
plot(x,y,'o',xe,[yex;yherm;ycomp;ynak])
legend('given points','sin(x)','Hermite','complete spline','not-a-knot spline')

figure(2)
plot(xe,[yherm-yex;ycomp-yex;ynak-yex])
title('Errors')
legend('Hermite','complete spline','not-a-knot spline')
 
  
   

