| Contents | Index |
| On this page… |
|---|
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.
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.
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
endSave 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 = [];
endSave this file as unitdisk2.m.
Start the Optimization Tool by entering optimtool at the command line.
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.

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

Click the Start button under Run solver and view results.
The output appears as follows in the Optimization Tool.

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

The "Current Point" plot graphically shows the minimizer [0.786,0.618], which is reported as the Final point in the Run solver and view results pane. This plot updates at each iteration, showing the intermediate iterates.
The "Current Function Value" plot shows the objective function value at all iterations. This graph is nearly monotone, showing fmincon reduces the objective function at almost every iteration.
The "First-order Optimality" plot shows the first-order optimality measure at all iterations.
Write the nonlinear objective and constraint functions, including the derivatives, as shown in Running the Optimization Using the Optimization Tool.
Create an options structure that includes calling the three plot functions:
options = optimset('Algorithm','interior-point',...
'GradObj','on','GradConstr','on','PlotFcns',{@optimplotx,...
@optimplotfval,@optimplotfirstorderopt});Call fmincon:
x = fmincon(@rosenboth,[0 0],[],[],[],[],[],[],...
@unitdisk2,options)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.6177fmincon also displays the three plot functions, shown at the end of Running the Optimization Using the Optimization Tool.
![]() | Hessian | Output Functions | ![]() |

Learn how to use optimization to solve systems of equations, fit models to data, or optimize system performance.
Get free kit| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |