%
% This code demonstrates that the cubic spline interpolation 
% converges to the original function.
%
clear
figure(2);
clf reset
set(gcf,'position',[100,50,500,600])
set(gcf,'paperposition',[0.5,0.5,7.5,10.0])
%
axes('position',[0.18,0.56,0.74,0.36])
%
% We first plot the function f = 1/(1+x^2).
%
x0=[-5:0.01:5];
f0=1./(1+x0.^2);
n0=size(x0,2);
plot(x0,f0,'k-','linewidth',2.0)
hold on
%
% cubic spline interpolation through (4+1) points
%
p=4;
dx=10/p;
x=[-5:dx:5];
f=1./(1+x.^2);
y4=spline(x,f,x0);
%
% cubic spline interpolation through (8+1) points
%
p=8;
dx=10/p;
x=[-5:dx:5];
f=1./(1+x.^2);
y8=spline(x,f,x0);
%
% cubic spline interpolation through (12+1) points
%
p=12;
dx=10/p;
x=[-5:dx:5];
f=1./(1+x.^2);
y12=spline(x,f,x0);
%
plot(x0, y4,'k--','linewidth',1.0)
plot(x0, y8,'b--','linewidth',1.0)
plot(x0, y12,'r-','linewidth',1.0)
%
axis([-5,5,-0.5,2.0])
set(gca,'fontsize',14)
set(gca,'xtick',[-4:2:4])
set(gca,'ytick',[-0.5:0.5:2.0])
xlabel('x')
ylabel('f')
title('Cubic spline interpolation')
h1=legend('f(x)=1/(1+x^2)','5 points','9 points','13 points',1);
set(h1,'fontsize',10)
%
axes('position',[0.18,0.09,0.74,0.36])
semilogy(x0, abs(y4-f0),'k--','linewidth',1.0)
hold on
semilogy(x0, abs(y8-f0),'b--','linewidth',1.0)
semilogy(x0, abs(y12-f0),'r-','linewidth',1.0)
set(gca,'fontsize',14)
axis([-5,5,1.0e-3,10])
set(gca,'xtick',[-4:2:4])
set(gca,'ytick',10.^[-4:0])
xlabel('x')
ylabel('Error')
h2=legend('5 points','9 points','13 points',1);
set(h2,'fontsize',10)
%
%
