function graderr(finite_diff_deriv, analytic_deriv, evalstr2)
%GRADERR Used to check gradient discrepancy in optimization routines. 

%   Copyright (c) 1990-98 by The MathWorks, Inc.
%   $Revision: 1.11 $  $Date: 1998/07/22 23:08:32 $

finite_diff_deriv = full(finite_diff_deriv); 
analytic_deriv = full(analytic_deriv);
err=max(max(abs(analytic_deriv-finite_diff_deriv)));
disp(sprintf('Maximum discrepancy between derivatives  = %g',err));
if (err > 1e-6*norm(analytic_deriv) + 1e-5) 
    disp('Warning: Derivatives do not match within tolerance')
    disp('Derivative from finite difference calculation:')
    finite_diff_deriv
    disp(['User-supplied derivative, ', evalstr2, ' : '])
    analytic_deriv
    disp('Difference:')
    analytic_deriv - finite_diff_deriv
    disp('Strike any key to continue or Ctrl-C to abort')
    pause 
end
