Effacer les filtres
Effacer les filtres

How to create a hold for the generated plots using optimset, PlotFcns

4 vues (au cours des 30 derniers jours)
Julio Martinez
Julio Martinez le 18 Août 2015
Commenté : Julio Martinez le 31 Août 2015
Hello, thanks you in advance,
I am optimizing two objective functions using the 'optimset' function. I have also set the Plot Functions for @optimplotx @optimplotfunccount @optimplotfval @optimplotconstrviolation. How can I 'hold' the two "figures" generated by these two routines? Here's what I typed on my editor:
objFcn = @(x) -CabTRvar(x,TE1, T1a, T1b, T2a, T2b);
%%Parameters to be used by the optimization tool.
x0 = TR;
lb = lowb;
ub = uppb;
%%This section contains the optimization algorithm for absolute contrast
disp('This table shows the optimization table for Absolute Contrast')
options = optimset('Algorithm', 'interior-point', 'Disp','iter');
% The next line generates plots that can be useful to visualize how the
% algorithm is working during an optimization.
options = optimset(options,'PlotFcns', { @optimplotx @optimplotfunccount @optimplotfval @optimplotconstrviolation });
[xOpt, fVal] = fmincon(objFcn, x0,[],[],[],[], lb, ub, [],options);
%%This section contains the optimization algorithm for Normalized contrast
% The function for Normalized contrast (variable TR) is called by this program.
objFcn = @(x) -CabTRvarNorm(x,TE1, T1a, T1b, T2a, T2b);
disp('This table shows the optimization table for Normalized Contrast')
options2 = optimset('Algorithm', 'interior-point', 'Disp','iter');
% The next line generates plots that can be useful to visualize how the
% algorithm is working during an optimization.
options2 = optimset(options2,'PlotFcns', { @optimplotx @optimplotfunccount @optimplotfval @optimplotconstrviolation });
[xOpt2, fVal2] = fmincon(objFcn, x0,[],[],[],[], lb, ub, [],options2);
%%Output: Optimized TR value and its corresponding maximized absolute contrast
fprintf('Optimized TR: \t%4.0f ms\n',xOpt(1));
fprintf('Maximized Absolute Contrast:\t%4.8f\n\n',-fVal);
fprintf('Optimized TR: \t%4.0f ms\n',xOpt2(1));
fprintf('Maximized Normalized Contrast:\t%4.8f\n\n',-fVal2);
When I run this routine, a PlotFcns window shows the first optimization. Then, it is replaced by the second optimization. This is leaving me with only the second "figure". I tried a hold on but it did not work. Do I need to create a hold for these plots? h = plot(something)? I would like to keep both PlotFcns windows -if it's possible- to include in my report. Thanks a lot!

Réponse acceptée

James Wiken
James Wiken le 20 Août 2015
You can make a copy of the current figure with the following code snippet:
h1 = gcf;
h2 = figure(2); %change this variable for each figure
copyobj(get(h1,'children'),h2);
You can add this code snippet after each call to 'fmincon' to create a copy of the current figure. This will create separate plots for each optimization. If you want to set the plots on the same axes you can follow the directions provided in the answer to the question linked below:
  1 commentaire
Julio Martinez
Julio Martinez le 31 Août 2015
Thank you very much James. This code snippet works like a charm!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Optimization dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by