Plot function value vs 2 variables (expecting 3D plot) for each iteration in fmincon
3 views (last 30 days)
Show older comments
Hi
I need to plot for each iteration fval as Z, vs optimal values x from fmincon at this iteration
I tried to do it with Output Function Syntax and Plot Function Syntax in matlab doc and wrote(adapt) this code, but I get just a empty figure...
options = optimoptions('fmincon','PlotFcns',@optim_plot_fval_vs_q);
[x_opt, fval, exitflag, output, lambda_fmincon, grad, hessian]...
= fmincon(@objectiveFunction, x0, [],[],[],[],[],[], @nonlincon, options);
function stop = optim_plot_fval_vs_q(x,optimValues,state,varargin)
% OPTIMPLOTRESNORM Plot value of the norm of residuals at each iteration.
%
% STOP = OPTIMPLOTRESNORM(X,OPTIMVALUES,STATE) plots OPTIMVALUES.resnorm.
%
% Example:
% Create an options structure that will use OPTIMPLOTRESNORM as the plot
% function
% options = optimoptions('lsqnonlin','PlotFcn',@optimplotresnorm);
%
% Pass the options into an optimization problem to view the plot
% lsqnonlin(@(x) sin(3*x),[1 4],[],[],options);
% Copyright 2006-2015 The MathWorks, Inc.
persistent plotavailable
stop = false;
switch state
case 'init'
if isfield(optimValues,'resnorm')
plotavailable = true;
else
plotavailable = false;
title(getString(message('optim:optimplot:TitleNormResid', ...
getString(message('optim:optimplot:NotAvailable')))),'interp','none');
end
case 'iter'
if plotavailable
if optimValues.iteration == 0
% The 'iter' case is called during the zeroth iteration,
% but it now has values that were empty during the 'init' case
% plotresnorm = plot(optimValues.iteration,optimValues.resnorm,'kd', ...
% 'MarkerFaceColor',[1 0 1]);
plotresnorm = plot3(x(1), x(length(x)/2+1), optimValues.fval);
% xlabel(getString(message('optim:optimplot:XlabelIter')),'interp','none');
% set(plotresnorm,'Tag','optimplotresnorm');
% ylabel(getString(message('optim:optimplot:YlabelNormResid')),'interp','none');
% title(getString(message('optim:optimplot:TitleNormResid', ...
% sprintf('%g',norm(optimValues.resnorm)))),'interp','none');
else
% plotresnorm = findobj(get(gca,'Children'),'Tag','optimplotresnorm');
% newX = [get(plotresnorm,'Xdata') optimValues.iteration];
% newY = [get(plotresnorm,'Ydata') optimValues.resnorm];
% set(plotresnorm,'Xdata',newX, 'Ydata',newY);
newX = [get(plotresnorm,'Xdata') x(1)];
newY = [get(plotresnorm,'Ydata') x(length(x)/2+1)];
newZ = [get(plotresnorm,'Zdata') optimValues.fval];
set(plotresnorm,'Xdata',newX, 'Ydata',newY, 'Zdata',newZ);
% set(get(gca,'Title'),'String', ...
% getString(message('optim:optimplot:TitleNormResid', ...
% sprintf('%g',optimValues.resnorm))));
end
end
end
I am expecting to get updating figure (with iteration) like this (example at the end of fmincon (after all iterations)):
![untitled.jpg](https://www.mathworks.com/matlabcentral/answers/uploaded_files/225493/untitled.jpeg)
Please, help me to get this figure
0 Comments
Answers (0)
See Also
Categories
Find more on Get Started with Optimization Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!