Hi everyone, I would like to know how I can pass a parameter from the objective function to the output function in an optimization problem. Here is an example of what I would like to do:
options = optimset(@fminunc,'GradObj','on','Hessian','on','OutputFcn', @outfun);[x fval] = fminunc(@rosenboth,[-1;2],options)function [f g H extraparameter] = rosenthree(x)% Calculate objective f, gradient g, Hessian H
f = 100*(x(2) - x(1)^2)^2 + (1-x(1))^2;g = [-400*(x(2)-x(1)^2)*x(1)-2*(1-x(1)); 200*(x(2)-x(1)^2)];H = [1200*x(1)^2-400*x(2)+2, -400*x(1); -400*x(1), 200];extraparameter = x(1)^2 - x^(2)^3endfunction stop = outfun(x,optimValues,state, extraparameter) stop = false; switch state case 'init' hold on case 'iter' % Concatenate current point and objective function
% value with history. x must be a row vector.
history.fval = [history.fval; optimValues.fval]; history.x = [history.x; x]; history.extraparameter = [history.extraparameter; extraparameter] plot(history.fval, history.extraparameter) case 'done' hold off otherwise end end
Does anyone have an idea?? Thank you very much in advance.
Àlex
Best Answer