fmincon optimization in matlab using OutputFcn

조회 수: 30 (최근 30일)
mohammed hussein
mohammed hussein 2020년 9월 24일
댓글: mohammed hussein 2020년 9월 25일
Hi all
i have this example and i want to save the x viarable in each iteration not only see the final value How can i do it . all i found is using OutputFcn but i dont know how can i do it in this example below . thank you very much for your helping
fun = @(x)100*(x(2)-x(1)^2)^2 + (1-x(1))^2;
A = [];
b = [];
Aeq = [];
beq = [];
lb = [];
ub = [];
x0 = [0,0];
options = optimoptions('fmincon','Display','iter');
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,[],options)
  댓글 수: 2
Mario Malic
Mario Malic 2020년 9월 25일
편집: Mario Malic 2020년 9월 25일
Check here https://ch.mathworks.com/help/optim/ug/output-function.html there are some examples if you use the search option.
mohammed hussein
mohammed hussein 2020년 9월 25일
Thank you for your answer

댓글을 달려면 로그인하십시오.

채택된 답변

Michael Croucher
Michael Croucher 2020년 9월 25일
편집: Michael Croucher 2020년 9월 25일
Following the advice at https://uk.mathworks.com/help/matlab/math/output-functions.html, we can use the 'OutputFcn' option. By putting the optimistion problem into a function, as shown below, we can take advantage of the fact that nested functions can access variables defined in the outer scope. That is, although I cannot explicitly pass the xvals array to outfun, it can access it anyway since it is a nested function.
function [x, xvals] = myproblem(x0)
xvals=[]; % This will contain the values of x where fun has been evaluated
fun = @(x)100*(x(2)-x(1)^2)^2 + (1-x(1))^2;
A = [];
b = [];
Aeq = [];
beq = [];
lb = [];
ub = [];
options = optimoptions('fmincon','Display','iter','OutputFcn',@outfun);
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,[],options);
function stop = outfun(x,optimValues,state)
stop=false;
if isequal(state,'iter')
xvals = [xvals;x];
end
end
end
Use this as follows:
[x,xvals] = myproblem([0,0]);
x will have the final result and xvals will contain the history.
  댓글 수: 1
mohammed hussein
mohammed hussein 2020년 9월 25일
Thank you for your answer . this help me alot .

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2020년 9월 25일
shows an example complete with code. The runfmincon() function returns a structure in which you can ask to see all the x values.
  댓글 수: 2
Michael Croucher
Michael Croucher 2020년 9월 25일
That will teach me to hit 'refresh' before writing an answer :)
mohammed hussein
mohammed hussein 2020년 9월 25일
Thank you for your answer

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by