% CLASS13NOV  evaluate the actual error in approximating f(x) = cos(x)
%             by an interpolating polynomial of degree 10
%             recall that using Lagrange's theorem with remainder
%             we estimated   |f(x) - pn(x)| <= 2.5 x 10^-8

x = 0:.1:1;  y = cos(x);  % the "data" are 11 points on graph of cos(x)

pn = polyfit(x,y,10);     % coefficients of the unique interpolating poly of degree 10

% evaluate error at random locations in [0,1];  note rand() gives uniform on [0,1]
xr = rand(300,1);   norm(cos(xr) - polyval(pn,xr),'inf')

% evaluate error at regular *very* fine grid
xf = 0:0.000001:1;  norm(cos(xf) - polyval(pn,xf),'inf')

% notes:
%   norm(v,'inf')  finds max of absolute values of entries of v, while
%   norm(v) = norm(v,2) finds the square root of the sum of the squares 
%   of the entries of v:
%           norm([3 -4])        =  5
%           norm([3 -4],'inf')  =  4
