Optimization plot in App Designer

18 ビュー (過去 30 日間)
Jonatan Solanas
Jonatan Solanas 2022 年 8 月 2 日
コメント済み: Jonatan Solanas 2022 年 8 月 3 日
My App Designer program runs an optimization process. Specifically with the fmincon algorithm.
I would like to plot the real-time optimization process so that the user can watch it evolve. Specifically I want to plot 'optimplotx' and 'optimplotfval'.
However, I would like to plot these two graphs within two UIAxes elements in my App, and not in a pop-up window as it does by default.
My question is if it is possible to easily transfer those two plots that the optimization algorithm create (and updates) by default to their respective UIAxes elements.
By easily I mean not having to code all the plotting process (input data, plotting, updating...) but rather "associating" the actual graphs that pop-up to the UIAxes in the App.
Thanks in advance.

回答 (1 件)

Kevin Holly
Kevin Holly 2022 年 8 月 2 日
You could get the handle of the axes and transfer the children to the uiaxes in your app. See example code below:
fun = @(x)100*(x(2)-x(1)^2)^2 + (1-x(1))^2;
nonlcon = @unitdisk;
A = [];
b = [];
Aeq = [];
beq = [];
lb = [];
ub = [];
x0 = [0,0];
options = optimoptions('fmincon','PlotFcn',@optimplotx);
[x,fval,exitflag,output,lambda,grad,hessian] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options);
h = gca;
h.Children.Parent = app.UIAxes;
app.UIAxes.XLabel.String = h.XLabel.String;
app.UIAxes.YLabel.String = h.YLabel.String;
app.UIAxes.Title.String = h.Title.String;
app.UIAxes.XLim = h.XLim;
app.UIAxes.YLim = h.YLim;
close(gcf)
  2 件のコメント
Kevin Holly
Kevin Holly 2022 年 8 月 2 日
Here is an example if you are transferring two plots:
fun = @(x)100*(x(2)-x(1)^2)^2 + (1-x(1))^2;
nonlcon = @unitdisk;
A = [];
b = [];
Aeq = [];
beq = [];
lb = [];
ub = [];
x0 = [0,0];
options = optimoptions('fmincon','PlotFcn',{@optimplotx,@optimplotfval});
[x,fval,exitflag,output,lambda,grad,hessian] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options);
h=gcf;
app.UIAxes.XLim = h.Children(end).XLim;
app.UIAxes.YLim = h.Children(end).YLim;
h.Children(end).Children.Parent = app.UIAxes;
app.UIAxes.XLabel.String = h.Children(end).XLabel.String;
app.UIAxes.YLabel.String = h.Children(end).YLabel.String;
app.UIAxes.Title.String = h.Children(end).Title.String;
app.UIAxes2.XLim = h.Children(end-1).XLim;
app.UIAxes2.YLim = h.Children(end-1).YLim;
h.Children(end-1).Children.Parent = app.UIAxes2;
app.UIAxes2.XLabel.String = h.Children(end-1).XLabel.String;
app.UIAxes2.YLabel.String = h.Children(end-1).YLabel.String;
app.UIAxes2.Title.String = h.Children(end-1).Title.String;
Jonatan Solanas
Jonatan Solanas 2022 年 8 月 3 日
Thanks for your feedback.
I have tried your code, but since it is added right after the fmincon function it only executes when the optimization process ends, plotting only the end result.
As I said, I would like to plot the real-time process.
I have tried adding your code to an OutputFcn, so that it executes with every iteration.
%fmincon input...
options = optimoptions(@fmincon, 'PlotFcn',{@optimplotx,@optimplotfval},'OutputFcn',@outfun);
%fmincon...
function stop = outfun(x,optimValues,state)
stop = false;
switch state
case 'init'
hold (app.UIAxes, "on")
hold (app.UIAxes2, "on")
case 'iter'
h=gcf;
app.UIAxes.XLim = h.Children(end).XLim;
app.UIAxes.YLim = h.Children(end).YLim;
h.Children(end).Children.Parent = app.UIAxes;
app.UIAxes.XLabel.String = h.Children(end).XLabel.String;
app.UIAxes.YLabel.String = h.Children(end).YLabel.String;
app.UIAxes.Title.String = h.Children(end).Title.String;
app.UIAxes2.XLim = h.Children(end-1).XLim;
app.UIAxes2.YLim = h.Children(end-1).YLim;
h.Children(end-1).Children.Parent = app.UIAxes2;
app.UIAxes2.XLabel.String = h.Children(end-1).XLabel.String;
app.UIAxes2.YLabel.String = h.Children(end-1).YLabel.String;
app.UIAxes2.Title.String = h.Children(end-1).Title.String;
case 'done'
hold (app.UIAxes, "off")
hold (app.UIAxes2, "off")
otherwise
end
end
However I can't get it to work since I am getting the following error:
Error using outfun
Unrecognized field name "searchdirection".
stop(i) = feval(OutputFcn{i},xOutputfcn,optimValues,state,varargin{:});
Error in barrier
Error in barrier
Error in barrier
Error in fmincon (line 873)
[X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD,HESSIAN] = barrier(funfcn,X,A,B,Aeq,Beq,l,u,confcn,options.HessFcn, ...

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

カテゴリ

Help Center および File ExchangeSolver Outputs and Iterative Display についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by