function numI = AdaptQNC(fname,a,b,m,tol)
%
% Pre:
%   fname    string that names an available function of the
%            form f(x) that is defined on [a,b]. f should
%            return a column vector if x is a column vector.
%     a,b    real scalars 
%       m    integer that satisfies 2 <= m <=11
%     tol    positive real
%
% Post:
%    numI    the composite m-point Newton-Cotes approximation of the 
%            integral of f(x) from a to b, with the abscissae chosen
%            adaptively. The 

% Compute the integral two ways and estimate the error.

   A1 = CompQNC(fname,a,b,m,1);
   A2 = CompQNC(fname,a,b,m,2);
   
   d = 2*floor((m-1)/2)+1;
   error = abs(A1-A2)/2^(d+1);
   
   if error <= tol
      % A2 is acceptable
      numI = A2;
   else
      % Sum suitably accurate left and right integrals
      mid = (a+b)/2;
      numI = AdaptQNC(fname,a,mid,m,tol/2) + AdaptQNC(fname,mid,b,m,tol/2);
   end

