% This presents the uniform-mesh interpolants of the function 1/(1+x^2)
% for x in [-5,5].  To execute the file, just type "interpolating" at 
% the matlab prompt.
%

a = -5;
b = 5;

for n=2:17
  h = (b-a)/n;
  for i=1:n+1;
    xx(i) = a + (i-1)*h;
  end

  x = a:.001:b;

  y = zeros(size(x));
  for i=1:n+1
    z = ones(size(x));
    for j=1:n+1
      if j ~= i
        z = z.*(x-xx(j))/(xx(i)-xx(j));
      end
    end
% we're fitting the function f(x)=1/(1+x^2).
    y = y + z/(1+xx(i)^2);
  end

  for i=1:length(x)
    yy(i) = 1/(1+x(i)^2);
  end
  figure(n)
  clf
  plot(x,yy)
  hold on
  plot(x,y);
  str = sprintf('degree n = %2.0f',n);
  title(str);
  aa(n) = n;
  bb(n) = max(abs(y-yy));
end

figure(n+1)
clf
plot(aa,bb);
hold on
plot(aa,bb,'o');
title('the problem with using a uniform mesh')
xlabel('degree n');
ylabel('L^\infty norm of the error: f - p_n');