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

VEPlot(x,optimValues,state)
function stop = VEPlot(x,optimValues,state)
% Plotting helper function for VEoptim

%  Copyright (c) 2010, The MathWorks, Inc.
%  All rights reserved.
if nargout > 0
    stop = false;
end
if nargin == 1
x(1) = x(1)*10000;
    state = 'init';
elseif nargin == 0
    x = [-1 -1];
    state = 'iter';
end

% Create contour plot
h = findobj('Tag','VEMapPlot');
if isempty(h)
    load VEData
    Xeval = linspace(1000,6000,300)';
    Yeval = linspace(0,1,300)';

    % Make them into a grid for use in surf
    [xx,yy] = meshgrid(Xeval,Yeval);

    zz = griddata(RPM,Pratio,VE,xx,yy,'cubic');

    contourf(xx,yy,zz,[0 0.2 0.4 0.6 0.7 0.75 0.8 0.85 0.9 0.92 0.94 0.96 1.0])
    xlabel('Revolutions Per Minute, RPM')
    ylabel('Manifold Pressure Ratio')
    colorbar
    set(gca,'Tag','VEMapPlot')
    hold on
    hi = findobj('Tag','PlotOfIterates');
    ms = get(hi,'UserData');
    if isempty(ms)
        % colors to use for multiple staring points
        ms.index = 1;
        ms.Colors = ['kgbcmyr'];
    end

    hi = plot(x(1),x(2),[ms.Colors(ms.index),'o-'],...
        'MarkerFaceColor',ms.Colors(ms.index));
    set(hi,'Tag','PlotOfIterates')
    set(h,'UserData',ms);
    hold off
    % Label plot
    set(gcf,'NumberTitle','off')
    set(gcf,'Name','Optimization: Finding Peak Volumetric Efficiency')
    
else % update plot with next point
    hi = findobj('Tag','PlotOfIterates');
    ms = get(h,'UserData');
    if isempty(ms)
        % colors to use for multiple staring points
        ms.index = 1;
        ms.Colors = ['kgbcmyr'];
        set(h,'UserData',ms);
    end
    if isempty(hi)
        hold on
        hi = plot(x(1),x(2),[ms.Colors(ms.index),'o-'],...
        'MarkerFaceColor',ms.Colors(ms.index));
        set(hi,'Tag','PlotOfIterates')
    else
        set(hi, 'XData', [get(hi,'XData'), x(1)])
        set(hi, 'YData', [get(hi,'YData'), x(2)])
    end
end
drawnow
if exist('optimValues','var')
    iter = optimValues.iteration;
    if iter < 10
        iterStr = ['VEPlot0',num2str(iter)];
    else
        iterStr = ['VEPlot',num2str(iter)];
    end
end
switch state
    case 'init'
        text(x(1)*0.9, x(2)*0.9,'Start')        
    case 'done'
        
        if optimValues.iteration ~= 0
            text(x(1)*0.9, x(2),'End')
            % update color to next
            set(hi,'Tag',['PlotOfIterates',num2str(ms.index)])
            ms = get(h,'UserData');
            ms.index = ms.index+1;
            set(h,'UserData',ms);
        end
end

Contact us