%this supports Activity 11 in class
% polynomial 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.5 -4 -3.5 -3 -2 -1 0 1 2 3 3.5 4 4.5 5];
y = fv(x);
%Develop polynomial interpolant
%coefficients are stored in a
%develop polynomial fit of degree npts-1
a = polyfit(x,y,npts-1);
%Evaluate interpolant over the variable x1
f1 = polyval(a,x1);
plot(x1,y1,'-',x,y,'p',x1,f1,'--')
title (['Runge Function and ',num2str(npts),'-point Polynomial Interpolant'])
ylabel('F(x)')
xlabel ('x')
legend('Runge','Data','Interpolant',0)
axis([-5 5 -0.5 2])