%spline_interpolation: example
%f=@(x) exp(x); a=-3;b=5; % exponent
f=@(x) 1./(1+x.^2); a=-5;b=5; % Runge function
NN=10;step=10; 
err=zeros(1,NN);
close all
for i=1:NN
 % equidistant interpolation knots & f. values
 N=step*i; x=linspace(a,b,N); y=f(x);
 xx=linspace(a,b,10*N);yy=f(xx); % a fine grid & vals for error estimation
 
 % build the interp. spline for {x,y} data using not-a-knot end conditions
 % and find its values at the xx points:
 yys=spline(x,y,xx); 
 
 % plot the result
 figure(1);plot(x,y,'ko',xx,yy,'b',xx,yys,'r');
 legend('knots','function','spline');
 xlabel('x');axis tight;grid on;
 % error estimate
 err(i)=max(abs(yy-yys));
 title(['N of knots = ',int2str(N),'  err=',num2str(err(i))]);
 pause(1); % pauses for 1 sec (so that we can see the graph)
end
% the slope of the loglog graph of err(1/N) (should be close to 4)
figure(2);loglog(1./(step*(1:NN)),err);
grid on;xlabel('h');ylabel('error');
conv_order=log(err(NN)/err(5))/(log(5/NN));
title(['spline interpolation conv. order: ',num2str(conv_order)])