This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Solve a Constrained Nonlinear Problem, Solver-Based

Typical Optimization Problem

This example shows how to solve a constrained nonlinear problem using an Optimization Toolbox™ solver. The example demonstrates the typical work flow: create an objective function, create constraints, solve the problem, and examine the results.

For the problem-based approach to this problem, see Solve a Constrained Nonlinear Problem, Problem-Based.

Problem Formulation: Rosenbrock's Function

Consider the problem of minimizing Rosenbrock's function


over the unit disk, that is, the disk of radius 1 centered at the origin. In other words, find x that minimizes the function f(x) over the set x12+x221. This problem is a minimization of a nonlinear function with a nonlinear constraint.


Rosenbrock's function is a standard test function in optimization. It has a unique minimum value of 0 attained at the point [1,1]. Finding the minimum is a challenge for some algorithms because the function has a shallow minimum inside a deeply curved valley. The solution for this problem is not at the point [1,1] because that point does not satisfy the constraint.

This figure shows two views of Rosenbrock's function in the unit disk. The vertical axis is log-scaled; in other words, the plot shows log(1+f(x)). Contour lines lie beneath the surface plot.

Rosenbrock's Function, Log-Scaled: Two Views.

 Code for Generating the Figure

The function f(x) is called the objective function. The objective function is the function you want to minimize. The inequality x12+x221 is called a constraint. Constraints limit the set of x over which a solver searches for a minimum. You can have any number of constraints, which are inequalities or equations.

All Optimization Toolbox optimization functions minimize an objective function. To maximize a function f, apply an optimization routine to minimize –f. For more details about maximizing, see Maximizing an Objective.

Define the Problem in Toolbox Syntax

To use Optimization Toolbox software, express your problem as follows:

  1. Define the objective function in the MATLAB® language, as a function file or anonymous function. This example uses a function file.

  2. Define the constraints as a separate file or anonymous function.

Function File for Objective Function

A function file is a text file that contains MATLAB commands and has the extension .m. Create a function file in any text editor, or use the built-in MATLAB Editor as in this example.

  1. At the command line, enter:

    edit rosenbrock
  2. In the MATLAB Editor, enter:

    %% ROSENBROCK(x) expects a two-column matrix and returns a column vector
    % The output is the Rosenbrock function, which has a minimum at
    % (1,1) of value 0, and is strictly positive everywhere else.
    function f = rosenbrock(x)
    f = 100*(x(:,2) - x(:,1).^2).^2 + (1 - x(:,1)).^2;


    rosenbrock is a vectorized function that can compute values for several points at once. See Vectorization (MATLAB). A vectorized function is best for plotting. For a nonvectorized version, enter:

    %% ROSENBROCK1(x) expects a two-element vector and returns a scalar
    % The output is the Rosenbrock function, which has a minimum at
    % (1,1) of value 0, and is strictly positive everywhere else.
    function f = rosenbrock1(x)
    f = 100*(x(2) - x(1)^2)^2 + (1 - x(1))^2;
  3. Save the file with name rosenbrock.m.

Function File for Constraint

Constraint functions have the form c(x) ≤ 0 or ceq(x) = 0. The constraint x12+x221 is not in the form that the solver handles. To have the correct syntax, reformulate the constraint as x12+x2210.

Furthermore, the syntax for nonlinear constraints returns both equality and inequality constraints. This example includes only an inequality constraint, so you must pass an empty array [] as the equality constraint function ceq.

With these considerations in mind, write a function file for the nonlinear constraint.

  1. Create a file named unitdisk.m containing the following code:

    function [c,ceq] = unitdisk(x)
    c = x(1)^2 + x(2)^2 - 1;
    ceq = [ ];
  2. Save the file unitdisk.m.

Run the Optimization

There are two ways to run the optimization:

Minimize Rosenbrock's Function Using the Optimization App


The Optimization app warns that it will be removed in a future release. For alternatives, see Optimization App Alternatives.

  1. Start the Optimization app by entering optimtool at the command line. For more information about this tool, see Optimization App.

    The default Solver, fmincon - Constrained nonlinear minimization, is selected. This solver is appropriate for this problem because Rosenbrock's function is nonlinear, and the problem has a constraint. For more information about choosing a solver, see Optimization Decision Table.

    The default Algorithm, Interior point, is also selected.

  2. In the Objective function box, enter @rosenbrock. The @ character indicates the function handle (MATLAB) of the file rosenbrock.m.

  3. In the Start point box, enter [0 0] to specify the initial point where fmincon begins its search for a minimum.

  4. In the Nonlinear constraint functionbox, enter @unitdisk, the function handle of unitdisk.m.

    Ensure that your Problem Setup and Results pane matches this figure.

  5. In the Options pane, under Display to command window (at the bottom of the pane), select iterative from the Level of display list. (If you do not see the option, click Display to command window.) This setting shows the progress of fmincon in the command window.

  6. In the Problem Setup and Results pane, under Run solver and view results, click Start.

The following message appears in the Run solver and view results box:

Optimization running.
Objective function value: 0.04567482475812774
Local minimum found that satisfies the constraints.

Optimization completed because the objective function is non-decreasing in 
feasible directions, to within the value of the optimality tolerance,
and constraints are satisfied to within the value of the constraint tolerance.
Your objective function value can differ slightly, depending on your computer system and version of Optimization Toolbox.

The message tells you that:

  • The search for a constrained optimum ended because the derivative of the objective function is nearly 0 in directions allowed by the constraint.

  • The constraint is satisfied to the requisite accuracy.

At the bottom of the Problem Setup and Results pane, the minimizer x appears under Final point. For more information about exit messages, see Exit Flags and Exit Messages.

Minimize Rosenbrock's Function at the Command Line

You can run the same optimization from the command line.

  1. Create options that choose iterative display and the interior-point algorithm.

    options = optimoptions(@fmincon,...
  2. Run the fmincon solver with the options structure, reporting both the location x of the minimizer and the value fval attained by the objective function.

    [x,fval] = fmincon(@rosenbrock,[0 0],...

    The six sets of empty brackets represent optional constraints that are not being used in this example. See the fmincon function reference pages for the syntax.

MATLAB outputs a table of iterations and the results of the optimization.

Local minimum found that satisfies the constraints.

Optimization completed because the objective function is non-decreasing in 
feasible directions, to within the value of the optimality tolerance,
and constraints are satisfied to within the value of the constraint tolerance.

x =
    0.7864    0.6177

fval =

The message tells you that the search for a constrained optimum ended because the derivative of the objective function is nearly 0 in directions allowed by the constraint, and that the constraint is satisfied to the requisite accuracy. Several phrases in the message contain links to more information about the terms used in the message. For more details about these links, see Enhanced Exit Messages.

Interpret the Result

The iteration table in the command window shows how MATLAB searched for the minimum value of Rosenbrock's function in the unit disk. This table is the same whether you use the Optimization app or the command line. MATLAB reports the minimization as follows:

                                            First-order      Norm of
 Iter F-count            f(x)  Feasibility   optimality         step
    0       3    1.000000e+00    0.000e+00    2.000e+00
    1      13    7.753537e-01    0.000e+00    6.250e+00    1.768e-01
    2      18    6.519648e-01    0.000e+00    9.048e+00    1.679e-01
    3      21    5.543209e-01    0.000e+00    8.033e+00    1.203e-01
    4      24    2.985207e-01    0.000e+00    1.790e+00    9.328e-02
    5      27    2.653799e-01    0.000e+00    2.788e+00    5.723e-02
    6      30    1.897216e-01    0.000e+00    2.311e+00    1.147e-01
    7      33    1.513701e-01    0.000e+00    9.706e-01    5.764e-02
    8      36    1.153330e-01    0.000e+00    1.127e+00    8.169e-02
    9      39    1.198058e-01    0.000e+00    1.000e-01    1.522e-02
   10      42    8.910052e-02    0.000e+00    8.378e-01    8.301e-02
   11      45    6.771960e-02    0.000e+00    1.365e+00    7.149e-02
   12      48    6.437664e-02    0.000e+00    1.146e-01    5.701e-03
   13      51    6.329037e-02    0.000e+00    1.883e-02    3.774e-03
   14      54    5.161934e-02    0.000e+00    3.016e-01    4.464e-02
   15      57    4.964194e-02    0.000e+00    7.913e-02    7.894e-03
   16      60    4.955404e-02    0.000e+00    5.462e-03    4.185e-04
   17      63    4.954839e-02    0.000e+00    3.993e-03    2.208e-05
   18      66    4.658289e-02    0.000e+00    1.318e-02    1.255e-02
   19      69    4.647011e-02    0.000e+00    8.006e-04    4.940e-04
   20      72    4.569141e-02    0.000e+00    3.136e-03    3.379e-03
   21      75    4.568281e-02    0.000e+00    6.437e-05    3.974e-05
   22      78    4.568281e-02    0.000e+00    8.000e-06    1.083e-07
   23      81    4.567641e-02    0.000e+00    1.601e-06    2.793e-05
   24      84    4.567482e-02    0.000e+00    1.996e-08    6.916e-06

Your table can differ, depending on toolbox version and computing platform. The following description applies to the table as displayed.

  • The first column, labeled Iter, is the iteration number from 0 to 24. fmincon took 24 iterations to converge.

  • The second column, labeled F-count, reports the cumulative number of times Rosenbrock's function was evaluated. The final row shows an F-count of 84, indicating that fmincon evaluated Rosenbrock's function 84 times in the process of finding a minimum.

  • The third column, labeled f(x), displays the value of the objective function. The final value, 0.04567482, is the minimum reported in the Optimization app Run solver and view results box, and at the end of the exit message in the command window.

  • The fourth column, Feasibility, is 0 for all iterations. This column shows the value of the constraint function unitdisk at each iteration where the constraint is positive. Because the value of unitdisk was negative in all iterations, every iteration satisfied the constraint.

The other columns of the iteration table are described in Iterative Display.

See Also

Related Topics