Optimization plot in App Designer

5 views (last 30 days)
Jonatan Solanas
Jonatan Solanas on 2 Aug 2022
Commented: Jonatan Solanas on 3 Aug 2022
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.

Answers (1)

Kevin Holly
Kevin Holly on 2 Aug 2022
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 Comments
Jonatan Solanas
Jonatan Solanas on 3 Aug 2022
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, ...

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by