Optimization plot in App Designer
4 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(1 个)
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
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;
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Nonlinear Optimization 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!