%this supports Activity 11 in class
% cubic spline interpolation of Runge function
clear all
clc
close all
%The Runge function is to be used
% f(x) = 1/(1+25x^2)
%Use is made of in-line vector mode function
%f = inline('1/(1+25*x*x)');
f = inline('1/(1+x*x)');
%vectorize f
fv = vectorize(f);
%Plot the Runge function to observe behavior
x1 = linspace(-5,5,120);
y1 = fv(x1);
%Interpolation points
npts = 11;
x = linspace(-5,5,npts);
%x = [-5 -4 -3 -2 -1.5 -1 -0.5 1 2 3 4 5]
y = fv(x);
%Develop spline interpolant
%Use the PP form of spline
hold on;
PP1 = spline(x,y);  %not-a-knot spline
f1 = ppval(PP1,x1);
plot (x1,y1,'-',x,y,'p')
pause
PP2 = spline(x,[0 y 0])  %natural spline
f2 = ppval (PP2,x1);
plot(x1,f1,'--g')
pause
plot(x1,f2,'-.r')
pause
PP4 = pchip (x,y);  %pchip interpolant
f4 = ppval(PP4,x1);
plot(x1,f4,'-m')
pause
%plot(x1,y1,'-',x,y,'p',x1,f1,'--',x1,f2,'-.',x1,f4,'-')
title (['Runge Function and ',num2str(npts),'-point Interpolants'])
ylabel('F(x)')
xlabel ('x')
legend('Runge','Data','Not-a-knot','natural','pchip',0)
axis([-5 5 -0.1 1.0])