MATLAB: How to conveniently plot iteration progress during a 2D optimization

Global Optimization Toolboxoptimization progressOptimization Toolboxoutput functionplot of iterates

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.

Best Answer

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);