I create the following objective function that counts the number of times it has been called:
function [f,g,cout]=sumsquared(p)persistent myCountf=sum(p.^2);if isempty(myCount) myCount = 1;else myCount = myCount + 1;endif nargout>1, g=2*p; end % gradient computation
if nargout>2, cout=myCount; end % gradient computation
When I then execute the following commands:
opts=optimset('largescale','off','gradobj','on','display','none');clear('sumsquared')[x,fval,exitflag,output]=fmincon(@sumsquared,rand(40,1),eye(40),ones(40,1),... [],[],[],[],[], opts);[ignore ignore cout] = sumsquared([]);ActualFunCount = cout-1ReportedFunCount = output.funcCount
I find the actual number of function evaluations is fewer than that reported by the output of FMINCON:
ActualFunCount = 5 ReportedFunCount = 125
Best Answer