%Demo Program for Chapter 4 overview
%
%Set up a data set for piecewise interpolation
%
close all
clear all
clc
n = 10;
m=200;
x = linspace(0,3,n);
%Use the humps function todefine y
y = humps(x);
%set up fine grid for evaluation:
z = linspace(0,3,m);
fz0 = interp1(x,y,z,'pchip'); %piecewise cubic hermite
% fz1 = spline(x,[ 300 y 0],z); %complete spline
PP1 = spline(x,[ 300 y 0]); %complete spline
% fz2 = spline(x,[ 0 y 0],z); %natural spline
PP2 = spline(x,[ 0 y 0]); %natural spline
% fz3 = interp1(x,y,z,'spline'); %not-a-knot cubic spline
PP3 = interp1(x,y,'spline','pp'); %not-a-knot cubic spline
fz1 = ppval(PP1,z);
fz2 = ppval(PP2,z);
fz3 = ppval (PP3,z);
%a= polyfit(x,y,n-1);
%fzpoly = polyval(a,z);
%a2=polyfit(x,y,n-2);
%fzpoly2 = polyval(a2,z);
yz = humps(z);
plot(z,yz,'-k',z,fz0,'-b',x,y,'g*')
title ('pchip interpolation of humps fn')
xlabel('x')
ylabel ('f(x)')
pause
figure
hold on
plot(z,yz,'-k',z,fz1,'-b',x,y,'g*')
title ('spline interpolation of humps fn')
xlabel('x')
ylabel ('f(x)')
pause
plot(z,fz2,'-r',z,fz3,'-m')
legend('humps','complete','data','natural','not-a-knot' )
pause