I tried to use that but it seems that you can only use that for things output by the actual optimization routine itself, such as step size, iteration number, etc.
No, that's not true. If you study this example,
http://www.mathworks.com/help/toolbox/optim/ug/brhkghv-56.html#brjhnpu
you will see that it uses nested functions to share variables between
the objective function and the OutputFcn. The shared variables can be any quantity that you want.
I think the example does not save 'all' the function calls. Notice the solver performs 8 iterations (9 if you include the 0th iteration) so, the history contains only 9 data points. The total number of function call made in this example is more than 9.
The output function is not going to work in this case. However, you can use the example as a starting point for this case as well.
You should use a nested objective function and store the values of 'x' and 'fval' every time the objective and/or constraint is called. I have an example for you:
function [objhistory,constrhistory] = saveSolverHistory
% Set up shared variables with OUTFUN
objhistory.x = [];
objhistory.fval = [];
constrhistory.x = [];
constrhistory.c = [];
constrhistory.ceq = [];
% call optimization
x0 = [-1 1];
options = optimset('display','iter',...
'Algorithm','active-set');
xsol = fmincon(@objfun,x0,[],[],[],[],[],[],@confun,options);
function f = objfun(x)
f = exp(x(1))*(4*x(1)^2 + 2*x(2)^2 + 4*x(1)*x(2) +...
2*x(2) + 1);
objhistory.fval = [objhistory.fval f];
objhistory.x = [objhistory.x x(:)];
end
function [c, ceq] = confun(x)
% Nonlinear inequality constraints
c = [1.5 + x(1)*x(2) - x(1) - x(2);
-x(1)*x(2) - 10];
% Nonlinear equality constraints
ceq = [];
% Keep the points in the history
constrhistory.x = [constrhistory.x x(:)];
constrhistory.c = [constrhistory.c c];
constrhistory.ceq = [constrhistory.ceq ceq];
end
end