% Script File: EqSpline
% For n = 4,8,16,32, the equispaced interpolants of
% f(x) = exp(-x^2), -2 <= x <= 2;
% f(x) = 1/(1+x^2), -5 <= x <= 5;
% f(x) = Sqrt(x), 0 <= x <= 4
% are plotted and maximum errors are displayed.

close all
for fcase=1:3
if fcase==1
a=-2;
b=2;
fname='gauss';
end
if fcase==2
a=-5;
b=5;
fname='runge';
end
if fcase==3
a=0;
b=4;
fname='sqrt';
end
figure
disp(['Function:' fname])
x = linspace(a,b,100)';
y = feval(fname,x);
xN=cell(4);
yN=cell(4);
cN=cell(4);
pVN=cell(4);
for i=1:4
n=2^(i+1);
xN{i} = linspace(a,b,n)';
yN{i} = feval(fname,xN{i});
pVN{i}=spline(xN{i},yN{i},x);
err = max(abs(y-pVN{i}));
disp(sprintf(' Error n= %2.0f %10.10f',n,err))
end
plot(x,y,x,pVN{1},x,pVN{2},x,pVN{3},x,pVN{4})
title(['Interpolants of ' fname])
end