function example2p4 ( )

% program for example 2.4
% Polynomial interpolation at equally spaced points to the function
%
%  Reference:
%
%    Samuel Conte, Carl de Boor,
%    Elementary Numerical Analysis,
%    Third Edition,
%    SIAM, 2017,
%    ISBN: 978-1-611975-19-2.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'example2p4:\n' );

     f = @(y) 1./(1+y.*y);

  disp('  N      MAXIMUM ERROR')
  for n=2:2:16
   x = (0:n)*10/n - 5;
   d = f(x);
   % calculate divided differences by algorithm 2.3
   for k=1:n
      d(1:n+1-k) = diff(d(1:n+2-k))./(x(1+k:n+1)-x(1:n+1-k));
   end
   % estimate maximum interpolation error on [-5 .. 5]
   y = (0:100)/10 - 5;
   % calculate p_n(y) by algorithm 2.1
   pnofy = repmat(d(1),size(y));
   for k=2:n+1
      pnofy = d(k) + (y-x(k)).*pnofy;
   end
   fprintf(' %2d      %1.7e\n', [n, max(abs(pnofy-f(y)))]);
  end 

  return
end

