Code covered by the BSD License  

Highlights from
Global Optimization with MATLAB

image thumbnail

Global Optimization with MATLAB

by

 

Demo files from the 2010 webinar "Global Optimization with MATLAB Products"

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

PlotIterates(x,optimValues,state)
function stop = PlotIterates(x,optimValues,state)
% Output function that plots the iterates of the optimization algorithm.

%  Copyright (c) 2010, The MathWorks, Inc.
%  All rights reserved.

persistent x0 r;
if nargin == 1
    showPlot(x(1).X,x(1).X0{:},x(1).Fval)
else
    switch state
        case 'init' % store initial point for later use
            x0 = x;
        case 'done'
            if ~(optimValues.iteration == 0)
                % After optimization, display solution in plot title
                r = optimValues.resnorm;
                showPlot(x,x0,r)
            end
    end
end
if nargout > 0
    stop = false;
end
end

function showPlot(b,b0,r)
f = @(b,x) b(1)*exp(-b(4).*x) + b(2).*exp(-b(5).*x) +...
    b(3).*exp(-b(6).*x);

h = findobj('Tag','PlotIterates');
if isempty(h)
    x = [  3.92,  7.93, 11.89, 23.90, 47.87, 71.91, 93.85, 117.84 ];
    y = [ 0.163, 0.679, 0.679, 0.388, 0.183, 0.125, 0.086, 0.0624 ];
    h = plot(x,y,'o');
    xlabel('t'); ylabel('c');
    title('c=b_1e^{-b_4t}+b_2e^{-b_5t}+b_3e^{-b_6t}');
    axis([0 120 0 0.8]);
    hold on
    line(3:120,f(b,3:120),'Color','r','Tag','PlotIterates');
    hold off
else
    set(h,'YData',f(b,get(h,'XData')));
end
s = sprintf('Starting Value   Fitted Value\n\n');
for i = 1:length(b)
    s = [s, sprintf('b(%d): % 2.4f      b(%d): % 2.4f\n',i,b0(i),i,b(i))];
end
s = [s,sprintf('\nMSE = %2.4e',r)];

ha = findall(gcf,'Tag','CoeffDisplay');
if ~isempty(ha)
    delete(ha);
end
% Create textbox
ha = annotation(gcf,'textbox',...
    [0.5 0.5 0.31 0.32],...
    'String',s,...
    'FitBoxToText','on',...
    'Tag','CoeffDisplay');
drawnow
end

Contact us