Skip to Main Content Skip to Search
Product Documentation

Plot Functions

The PlotFcns field of the options structure specifies one or more functions that an optimization function calls at each iteration to plot various measures of progress while the algorithm executes. Pass a function handle or cell array of function handles. The structure of a plot function is the same as the structure of an output function. For more information on this structure, see Output Function.

There are a variety of predefined plot functions available. Find them in the PlotFcns option description, in the solver function reference page, or in the Optimization Tool in the Plot functions pane of the Options pane.

Example: Using a Plot Function

This example shows how to use a plot function to view the progress of the fmincon interior-point algorithm. The problem is taken from the Getting Started Example: Nonlinear Constrained Minimization. The first part of the example shows how to run the optimization using the Optimization Tool. The second part shows how to run the optimization from the command line.

Running the Optimization Using the Optimization Tool

  1. Write the nonlinear objective and constraint functions, including the derivatives:

    %% ROSENBOTH returns both the value y of Rosenbrock's function
    % and also the value g of its gradient and H the Hessian.
    
    function [f g H] = rosenboth(x)
    
    f = 100*(x(2) - x(1)^2)^2 + (1-x(1))^2;
    
    if nargout > 1
        g = [-400*(x(2)-x(1)^2)*x(1)-2*(1-x(1));
            200*(x(2)-x(1)^2)];
        
        if nargout > 2
            H = [1200*x(1)^2-400*x(2)+2, -400*x(1);
                -400*x(1), 200];  
        end
    end

    Save this file as rosenboth.m.

    function [c,ceq,gc,gceq] = unitdisk2(x)
    % UNITDISK2 returns the value of the constraint
    % function for the disk of radius 1 centered at
    % [0 0]. It also returns the gradient.
    
    c = x(1)^2 + x(2)^2 - 1;
    ceq = [ ];
    
    if nargout > 2
        gc = [2*x(1);2*x(2)];
        gceq = [];
    end

    Save this file as unitdisk2.m.

  2. Start the Optimization Tool by entering optimtool at the command line.

  3. Set up the optimization:

    • Choose the fmincon solver.

    • Choose the Interior point algorithm.

    • Set the objective function to @rosenboth.

    • Choose Gradient supplied for the objective function derivative.

    • Set the start point to [0 0].

    • Set the nonlinear constraint function to @unitdisk2.

    • Choose Gradient supplied for the nonlinear constraint derivatives.

    Your Problem Setup and Results panel should match the following figure.

  4. Choose three plot functions in the Options pane: Current point, Function value, and First order optimality.

  5. Click the Start button under Run solver and view results.

  6. The output appears as follows in the Optimization Tool.

    In addition, the following three plots appear in a separate window.

Running the Optimization from the Command Line

  1. Write the nonlinear objective and constraint functions, including the derivatives, as shown in Running the Optimization Using the Optimization Tool.

  2. Create an options structure that includes calling the three plot functions:

    options = optimset('Algorithm','interior-point',...
     'GradObj','on','GradConstr','on','PlotFcns',{@optimplotx,...
        @optimplotfval,@optimplotfirstorderopt});
  3. Call fmincon:

    x = fmincon(@rosenboth,[0 0],[],[],[],[],[],[],...
        @unitdisk2,options)
  4. fmincon gives the following output:

    Local minimum found that satisfies the constraints.
    
    Optimization completed because the objective function is
    non-decreasing in feasible directions, to within the default
    value of the function tolerance, and constraints are satisfied
    to within the default value of the constraint tolerance.
    
    x =
        0.7864    0.6177

    fmincon also displays the three plot functions, shown at the end of Running the Optimization Using the Optimization Tool.

  


Free Optimization Interactive Kit

Learn how to use optimization to solve systems of equations, fit models to data, or optimize system performance.

Get free kit

Trials Available

Try the latest version of optimization products.

Get trial software
 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS