custom plot function -凯发k8网页登录

main content

custom plot function

about custom plot functions

to use a patternsearch plot function other than those included with the software, you can write your own custom plot function that is called at each iteration of the pattern search to create the plot. this example shows how to create a plot function that displays the logarithmic change in the best objective function value from the previous iteration to the current iteration. more plot function details are available in plot options.

creating the custom plot function

to create the plot function for this example, copy and paste the following code into a new function file in the matlab® editor:

function stop = psplotchange(optimvalues, flag)
% psplotchange plots the change in the best objective function 
% value from the previous iteration.
  
% best objective function value in the previous iteration
persistent last_best
 
stop = false;
if(strcmp(flag,'init')) 
        set(gca,'yscale','log'); %set up the plot
        hold on;
        xlabel('iteration'); 
        ylabel('log change in values');
        title(['change in best function value']);
end
 
% best objective function value in the current iteration
best = min(optimvalues.fval);  
 
 % set last_best to best
if optimvalues.iteration == 0
last_best = best;
        
else
        %change in objective function value
			 change = last_best - best; 
        plot(optimvalues.iteration, change, '.r');
end

save the file as psplotchange.m in a folder on the matlab path. the code is explained in how the plot function works.

set up the problem

the problem is the same as . to set up the problem:

  1. enter the following at the matlab command line:

    x0 = [2 1 0 9 1 0];
    aineq = [-8 7 3 -4 9 0];
    bineq = 7;
    aeq = [7 1 8 3 3 3; 5 0 -5 1 -5 8; -2 -6 7 1 1 9; 1 -1 2 -2 3 -3];
    beq = [84 62 65 1];
    h = [36 17 19 12  8 15; 
         17 33 18 11  7 14; 
         19 18 43 13  8 16;
         12 11 13 18  6 11; 
          8  7  8  6  9  8; 
         15 14 16 11  8 29];
    f = [ 20 15 21 18 29 24 ]';
     
    f = @(x)0.5*x'*h*x   f'*x;
  2. because this is a linearly constrained problem, set the pollmethod option to 'gsspositivebasis2n'. include both the @psplotbestf built-in plot function and the custom plot function @psplotchange in the options.

    options = optimoptions('patternsearch',...
        'plotfcn',{@psplotbestf,@psplotchange},...
        'pollmethod','gsspositivebasis2n');

run the optimization with custom plot function

run the example by calling patternsearch starting from x0.

[x,fval] = patternsearch(f,x0,...
    aineq,bineq,aeq,beq,[],[],[],options);

because the scale of the y-axis in the lower custom plot is logarithmic, the plot shows only changes that are greater than 0.

how the plot function works

the plot function uses information contained in the following structures.

  • optimvalues — current state of the solver, a structure

  • flag — current status of the algorithm, a character vector

the most important statements of the custom plot function, psplotchange.m, are summarized in the following table.

custom plot function statements

statementdescription
persistent last_best

creates the persistent variable last_best, the best objective function value in the previous generation. persistent variables are preserved over multiple calls to the plot function.

set(gca,'yscale','log')

sets up the plot before the algorithm starts.

best = min(optimvalues.fval)

sets best equal to the minimum objective function value. the field optimvalues.fval contains the objective function value in the current iteration. the variable best is the minimum objective function value. for a complete description of the fields of the structure optimvalues, see structure of the plot functions.

change = last_best - best

sets the variable change to the best objective function value at the previous iteration minus the best objective function value in the current iteration.

plot(optimvalues.iteration, change, '.r')

plots the variable change at the current objective function value, for the current iteration contained inoptimvalues.iteration.

related topics

网站地图