function [c, ifail] = getnearest(g)
% Computes nearest correlation matrix with g02aa

% Set parameters
errtol = 1e-07;
maxits = nag_int(200);
maxit = nag_int(10);
[gOut, c, iter, feval, nrmgrd, ifail] = ...
            g02aa(g, 'errtol', errtol, 'maxits', maxits, 'maxit', maxit);

% Print results
if (ifail == 0)
  fprintf('\n Nearest Correlation Matrix\n');
  disp(c);
  fprintf('\n Number of Newton steps taken:   %d\n', iter);
  fprintf(' Number of function evaluations: %d\n', feval);
  if (nrmgrd > errtol)
    fprintf(' Norm of gradient of last Newton step: %6.4f\n', ...
                                                         nrmgrd);
  end
end

clear errtol maxits maxit iter feval nrmgrd

end
