% Cubic spline interpolant for Runge function

f = inline('1 ./ (1 + 25*x.^2)');
npts = 11;
x = [-1:0.2:1]; % 11 equally spaced interpolation pts
y = f(x);% points at which to evaluate interpolant

xx = [-1:.01:1]; % for plotting
fxx = f(xx);

figure(); % creates a new figure window
% piecewise cubic spline interpolant evaluation
yy = interp1(x,y,xx,'spline'); % piecewise cubic spline interpolant
subplot(2,1,1);
plot(x,y,'o',xx,fxx,'-', xx,yy,'--r');
xlabel('x');
ylabel('1/(1 + 25*x^2)');
title('Runge function and its piecewise cubic spline interpolant');
legend('Values at nodes','Runge function','Cubic spline interpolant');

% plotting the errors
subplot(2,1,2);
plot(xx,fxx-yy);
xlabel('x');
ylabel('error');
title('Error of piecewise cubic spline interpolation');
