How to visualize the iterations of an optimization problem ?

7 ビュー (過去 30 日間)
Mohammad Assar
Mohammad Assar 2021 年 7 月 3 日
コメント済み: Mohammad Assar 2021 年 7 月 3 日
Hi everybody,
I recently started to learn how to optimize a continuous function and I want to get a figure like the one I attached as one of the outputs but I do not know how to code that.
prob = optimproblem
%defining variables
x= optimvar('x',"LowerBound",-2.5,'UpperBound',2.5);
y= optimvar('y',"LowerBound",-2.5,'UpperBound',2.5);
%ndefining objective function
prob.Objective=log(1+3.*(y-(x.^3-3)).^2+(x-4/3).^2);
%setting initial guess
initialpt.x=-1
initialpt.y=2
%options
options=optimoptions(prob,'Display','iter')
[sol,fval,exitflag,output]=solve(prob,initialpt,'options',options);
disp("# of function evaluations:"+output.funcCount)
fval
sol
exitflag
I want to have the following figure as an output:
Thank you

採用された回答

Matt J
Matt J 2021 年 7 月 3 日
編集済み: Matt J 2021 年 7 月 3 日
function main
xhist=[];
prob = optimproblem
%defining variables
x= optimvar('x',"LowerBound",-2.5,'UpperBound',2.5);
y= optimvar('y',"LowerBound",-2.5,'UpperBound',2.5);
%ndefining objective function
prob.Objective=log(1+3.*(y-(x.^3-3)).^2+(x-4/3).^2);
%setting initial guess
initialpt.x=-1;
initialpt.y=2;
%options
options=optimoptions(prob,'Display','iter','OutputFcn',@histfunc);
[sol,fval,exitflag,output]=solve(prob,initialpt,'options',options);
fcontour( @(x,y) log(1+3.*(y-(x.^3-3)).^2+(x-4/3).^2));
hold on
plot(xhist(1,:), xhist(2,:),'-om','MarkerFaceColor','b');
hold off
function stop = histfunc(x,optimValues,state)
stop = 0;
if state~="iter"; return; end
xhist=[xhist,x(:)];
end
end
  1 件のコメント
Mohammad Assar
Mohammad Assar 2021 年 7 月 3 日
Thank you, it was a lot of help.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with Problem-Based Optimization and Equations についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by