% Script File: RungeEgPWL
% For n = 8,40 the equal-spacing piecewise linear interpolants of 
% f(x) = 1/(1+25x^2) on [-1,1]'
% are of plotted.

close all
x = linspace(-1,1,100)'; 
y = ones(100,1)./(1 + 25*x.^2);
for n=8:8:40
   figure
   xEqual = linspace(-1,1,n)'; 
   yEqual = ones(n,1)./(1+25*xEqual.^2);
   j=2;
   for i=1:100
     if x(i) > xEqual(j)
       j=j+1;
     end
     PLval(i)=yEqual(j-1)*(x(i)-xEqual(j))/(xEqual(j-1)-xEqual(j));
     PLval(i)=PLval(i)-yEqual(j)*(x(i)-xEqual(j-1))/(xEqual(j-1)-xEqual(j));
   end
   plot(x,y,x,PLval,xEqual,yEqual,'*')
   title(sprintf('Piecewise Linear (n = %2.0f)',n))
end
