| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Optimization Toolbox |
| Contents | Index |
| Learn more about Optimization Toolbox |
| On this page… |
|---|
Problem Formulation: Rosenbrock's Function |
Consider the problem of minimizing Rosenbrock's function
![]()
over the unit disk, i.e.,
the disk of radius 1 centered at the origin. In other words, find x that
minimizes the function f(x) over
the set
. This problem is
a minimization of a nonlinear function with a nonlinear constraint.
Note 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 since it has a shallow minimum inside a deeply curved valley. |
Here are 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. This is the function
you wish to minimize. The inequality
is
called a constraint. Constraints limit the set
of x over which you may search for a minimum. You
may have any number of constraints, which may be inequalities or equations.
All Optimization Toolbox optimization functions minimize an objective function. To maximize a function f, apply an optimization routine to minimize –f.
To use Optimization Toolbox software, you need to
Define your objective function in the MATLAB language, as an M-file or anonymous function. This example will use an M-file.
Define your constraint(s) as a separate M-file or anonymous function.
An M-file is a text file containing MATLAB commands with the extension .m. Create a new M-file in any text editor, or use the built-in MATLAB Editor as follows:
At the command line type
edit rosenbrock
The MATLAB Editor opens.
In the editor type:
function f = rosenbrock(x) f = 100*(x(2) - x(1)^2)^2 + (1 - x(1))^2;
Save the file by selecting File > Save.
Constraint functions must be formulated so that they are in
the form c(x) ≤ 0 or ceq(x) = 0. The constraint
needs to be reformulated
as
in order to have
the correct syntax.
Furthermore, toolbox functions that accept nonlinear constraints need to have both equality and inequality constraints defined. In this example there is 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 M-file for the nonlinear constraint:
Create a file named unitdisk.m containing the following code:
function [c, ceq] = unitdisk(x) c = x(1)^2 + x(2)^2 - 1; ceq = [ ];
Save the file unitdisk.m.
There are two ways to run the optimization:
Using the Optimization Tool Graphical User Interface (GUI)
Using command line functions; see Minimizing at the Command Line.
Start the Optimization Tool by typing optimtool at the command line. The following GUI opens.

For more information about this tool, see Optimization Tool.
The default Solver fmincon - Constrained nonlinear minimization is selected. This solver is appropriate for this problem, since Rosenbrock's function is nonlinear, and the problem has a constraint. For more information about how to choose a solver, see Choosing a Solver.
In the Algorithm pop-up menu choose Active set—the default Trust region reflective solver doesn't handle nonlinear constraints.
For Objective function type @rosenbrock. The @ character indicates that this is a function handle of the M-file rosenbrock.m.
For Start point type [0 0]. This is the initial point where fmincon begins its search for a minimum.
For Nonlinear constraint function type @unitdisk, the function handle of unitdisk.m.
Your Problem Setup and Results pane should match this figure.

In the Options pane (center bottom), select iterative in
the Level of display pop-up menu. (If you don't
see the option, click
Display to
command window.) This shows the progress of fmincon in
the command window.

Click Start under Run solver and view results.

The following message appears in the box below the Start button:
Optimization running. Optimization terminated. Objective function value: 0.04567480869296667 Local minimum possible. Constraints satisfied. fmincon stopped because the predicted change in the objective function is less than the default value of the function tolerance and constraints were satisfied to within the default value of the constraint tolerance.
Your objective function value may differ slightly, depending on your computer system and version of Optimization Toolbox software.
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 very nearly satisfied.
Exit Flags and Exit Messages discusses exit messages such as these.
The minimizer x appears under Final point.

You can run the same optimization from the command line, as follows.
Create an options structure to choose iterative display and the active-set algorithm:
options = optimset('Display','iter','Algorithm','active-set');Run the fmincon solver with the structure options, reporting both the location x of the minimizer, and value fval attained by the objective function:
[x,fval] = fmincon(@rosenbrock,[0 0],...
[],[],[],[],[],[],@unitdisk,options)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 possible. Constraints satisfied.
fmincon stopped because the predicted change in the objective function
is less than the default value of the function tolerance and constraints
were satisfied to within the default value of the constraint tolerance.
<stopping criteria details>
Active inequalities (to within options.TolCon = 1e-006):
lower upper ineqlin ineqnonlin
1
x =
0.7864 0.6177
fval =
0.0457The 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 very nearly satisfied. Several phrases in the message contain links that give you more information about the terms used in the message. For more details about these links, see Enhanced Exit Messages.
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 Optimization Tool or the command line. MATLAB reports the minimization as follows:
Max Line search Directional First-order Iter F-count f(x) constraint steplength derivative optimality Procedure 0 3 1 -1 1 9 0.953127 -0.9375 0.125 -2 12.5 2 16 0.808446 -0.8601 0.0625 -2.41 12.4 3 21 0.462347 -0.836 0.25 -12.5 5.15 4 24 0.340677 -0.7969 1 -4.07 0.811 5 27 0.300877 -0.7193 1 -0.912 3.72 6 30 0.261949 -0.6783 1 -1.07 3.02 7 33 0.164971 -0.4972 1 -0.908 2.29 8 36 0.110766 -0.3427 1 -0.833 2 9 40 0.0750939 -0.1592 0.5 -0.5 2.41 10 43 0.0580974 -0.007618 1 -0.284 3.19 11 47 0.048247 -0.003788 0.5 -2.96 1.41 12 51 0.0464333 -0.00189 0.5 -1.23 0.725 13 55 0.0459218 -0.0009443 0.5 -0.679 0.362 14 59 0.0457652 -0.0004719 0.5 -0.4 0.181 15 63 0.0457117 -0.0002359 0.5 -0.261 0.0905 Hessian modified 16 67 0.0456912 -0.0001179 0.5 -0.191 0.0453 Hessian modified 17 71 0.0456825 -5.897e-005 0.5 -0.156 0.0226 Hessian modified 18 75 0.0456785 -2.948e-005 0.5 -0.139 0.0113 Hessian modified 19 79 0.0456766 -1.474e-005 0.5 -0.13 0.00566 Hessian modified
This table might differ from yours 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 19. fmincon took 19 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 79, indicating that fmincon evaluated Rosenbrock's function 79 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.0456766, is the minimum that is reported in the Optimization Tool Run solver and view results box, and at the end of the exit message in the command window.
The fourth column, Max constraint,
goes from a value of –1 at the initial value, to very nearly
0, –1.474e–005, at the final iteration.
This column shows the value of the constraint function unitdisk at
each iteration. Since the value of unitdisk was
nearly 0 at the final iteration,
there.
The other columns of the iteration table are described in Displaying Iterative Output.
![]() | Product Overview | Examples | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |