Passing parameter from the objective function to the output function

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)^3
end
function 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

 采纳的回答

You can use nested functions along the lines of passing information for this other purpose.
Alan Weiss
MATLAB mathematical toolbox documentation

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Manual Performance Optimization 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by