How to conveniently plot iteration progress during a 2D optimization?

7 views (last 30 days)
Hello,
I am curious whether there is a convenient and ready-to-use option to get a 3D plot (probably, a surface plot) that would show every point that a solver took on during optimization over two variables?
I know one can write an output function to store a set of the trial points along with their corresponding function values used by the optimizer throughout the solution process. However, for a special case with only two optimization parameters (which means easy visualization in a 3D plot), maybe there is an option/function that can be called without additional programming? I would like to have something similar to what is shown in the webinar "Tips and Tricks: Getting Started Using Optimization with MATLAB" (like a plot of iterates shown around 27:00 on the timeline).
Thank you all for any help!
Igor.

Accepted Answer

Denis Perotto
Denis Perotto on 29 Jan 2019
Edited: Denis Perotto on 29 Jan 2019
First, you should create your own function for plotting a 3D contour during the optimization process:
function stop = custom_plot(x, optimValues, state, <list of your custom parameters>)
% Standard part
stop = false;
if state ~= 'iter'
return
end
% This will clear the previously plotted contour if needed
if optimValues.iteration > 0
cla;
end
% Here insert your code as if you plotted a graph normally, like 'plot' or 'surf', etc.
% Don't forget that 'x' is your vector of two variables
% Also consider looking into 'optimValues' structure, optimValues.fval contains your current function value
% Feel free to use any custom parameters from <list of your custom parameters>
<...>
% Consider trying surf(x(1), x(2), optimValues.fval);
end
Second, in your main part of code you should call the 'custom_plot' function like this:
outfanon = @(x, optimValues, state) custom_plot(x, optimValues, state, <list of your custom parameters>);
opts = optimset('Display','iter','TolFun',1e-5,'TolX',1e-5,'MaxIter',100,'PlotFcns',outfanon);
[x1, fval] = fminsearch(f, x0, opts);

More Answers (1)

Igor
Igor on 29 Jan 2019
Edited: Igor on 29 Jan 2019
Hi Denis,
Thank you for the answer!
Based on your reply, I can come to the conclusion that there is no ready-to-use function for this. There are only 2D plots displaying the solution process, like "optimplotx" plotting the current point.
Anyway, I have not yet tried your code but am wondering how your surf plot would update. There is no history build-up like here: https://www.mathworks.com/help/optim/ug/output-functions.html
Thanks,
Igor.

Community Treasure Hunt

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

Start Hunting!