function max_error=test_poly_interpolate(f,xdata )
% max_error=test_poly_interpolate(f,xdata ) 
% utility function used for testing different interpolation methods.
% f is the function to be interpolated 
% xdata are abscissae at which interpolation takes place
% max_error is the maximum difference between the function
%   and its interpolant
%PC MA428
 
% Choose the number of the test points and generate them
% Use 1001 because it is odd, capturing the interval midpoint
NTEST=1001;
% construct NTEST points evenly spaced so that 
% they cover the interpolation interval in a standard way, i.e.,
% xval(1)=xdata(1) and xval(NTEST)=xdata(length(xdata))
xval= linspace(xdata(1),xdata(length(xdata)),1001);
 
% we need the results of f at the points xdata to do the
% interpolation WARNING: this is a vector statement
% In a real problem, ydata would be "given" somehow, and
% a function would not be available
ydata=feval(f,xdata);

% use Lagrange interpolation from problem 1 in Exercise 2 to do the interpolation
% WARNING: these use componentwise (vector) statements.
% Generate yval as interpolated values corresponding to xval
yval=eval_lag(xdata,ydata,xval);

% we will be comparing yval with the exact results of f at xval
% In a real problem, the exact results would not be available.
yexact=feval(f,xval);
 
% plot the exact and interpolated results on the same plot
% this gives assurance that everything is reasonable
plot(xval,yexact,'b');
hold on
plot(xval,yval,'r');
hold off
 
% compute the error.  The easiest way is to use the
% Matlab norm function.
% The inf indicates a maximum (infinity) norm.
max_error=norm(yexact-yval,inf);
