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 workflow: create an objective function, create constraints, solve the problem, and examine the results.
This example provides two approaches to solving the problem. One uses theOptimizeLive Editor task, a visual approach. The other uses the MATLAB®command line, a text-based approach. You can also solve this type of problem using the problem-based approach; seeSolve a Constrained Nonlinear Problem, Problem-Based.
Problem Formulation: Rosenbrock's Function
The problem is to minimize Rosenbrock's function
over theunit disk, that is, the disk of radius 1 centered at the origin. In other words, findxthat minimizes the functionf(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 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 showslog(1+f(x)). Contour lines lie beneath the surface plot.
Rosenbrock's Function, Log-Scaled: Two Views
Code for Generating the Figure
The functionf(x)is called theobjective function.The objective function is the function you want to minimize. The inequality is called aconstraint.Constraints limit the set ofxover which a solver searches for a minimum. You can have any number of constraints, which are inequalities or equalities.
All Optimization Toolbox optimization functions minimize an objective function. To maximize a functionf, apply an optimization routine to minimize –f. For more details about maximizing, seeMaximizing an Objective.
Define and Solve Problem UsingOptimizeLive Editor Task
TheOptimizeLive Editor task lets you set up and solve the problem using a visual approach.
Create a new live script by clicking theNew Live Scriptbutton on theFilesection of theHometab.
插入anOptimizeLive Editor task. Click the插入tab and then, in theCodesection, selectTask > Optimize.
In theSpecify problem typesection of the task, selectObjective > NonlinearandConstraints > Nonlinear. The task selects the solver
fmincon - Constrained nonlinear minimization
.Include Rosenbrock's function as the objective function. In theSelect problem datasection of the task, selectObjective function > Local functionand then click theNew...button. A new local function appears in a section below the task.
functionf = objectiveFcn(optimInput)% Example:% Minimize Rosenbrock's function% f = 100*(y - x^2)^2 + (1 - x)^2% Edit the lines below with your calculationx = optimInput(1); y = optimInput(2); f = 100*(y - x^2)^2 + (1 - x)^2;end
This function implements Rosenbrock's function.
In theSelect problem datasection of the task, selectObjective function > objectiveFcn.
Place the initial point
x0 = [0;0]
into the MATLAB workspace. Insert a new section above theOptimizetask by clicking the task, then clicking theSection Breakbutton on the插入tab. In the new section above the task, enter the following code for the initial point.x0 = [0;0];
Run the section by pressingCtrl+Enter. This action places
x0
into the workspace.In theSelect problem datasection of the task, selectInitial point (x0) > x0.
In theSelect problem datasection, selectConstraints > Nonlinear > Local functionand then click theNew...button. A new local function appears below the previous local function.
Edit the new local function as follows.
function[c,ceq] = unitdisk(x) c = x(1)^2 + x(2)^2 - 1; ceq = [ ];end
In theSelect problem datasection, select
unitdisk
as the constraint function.To monitor the solver progress, in theDisplay progresssection of the task, selectText display > Each iteration. Also, selectObjective value and feasibilityfor the plot.
To run the solver, click the options button⁝at the top right of the task window, and selectRun Section. The plot appears in a separate figure window and in the output area.
The output area shows a table of iterations, discussed inInterpret Result.
To find the solution, look at the top of the task.
The solver places the variables
solution
andobjectiveValue
in the workspace. View their values by inserting a new section break below the task and entering these lines.Run the section by pressingCtrl+Enter.
To understand the
fmincon
process for obtaining the result, seeInterpret Result.To display the code thatOptimizegenerates to solve the problem, click the options button⁝at the top right of the task window, and selectControls and Code.
At the bottom of the task, the following code appears.
% Set nondefault solver optionsoptions = optimoptions('fmincon','Display','iter','PlotFcn',...'optimplotfvalconstr');% Solve[solution,objectiveValue] = fmincon(@objectiveFcn,x0,[],[],[],[],[],[],...@unitdisk,options);
This code is the code you use to solve the problem at the command line, as described next.
Define and Solve Problem at Command Line
The first step in solving an optimization problem at the command line is to choose a solver. Consult theOptimization Decision Table. For a problem with a nonlinear objective function and a nonlinear constraint, generally you use thefmincon
solver.
Consult thefmincon
function reference page. The solver syntax is as follows.
[x,fval] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options)
Thefun
andnonlcon
inputs represent the objective function and nonlinear constraint functions, respectively.
Express your problem as follows:
Define the objective function in the MATLAB language, as a function file or anonymous function. This example uses a function file.
Define the constraints as a separate file or anonymous 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.
At the command line, enter:
editrosenbrock
In the MATLAB Editor, enter:
%% ROSENBROCK(x) expects a two-column matrix and returns a column vector%的输出是。function, which has a minimum at% (1,1) of value 0, and is strictly positive everywhere else.functionf = rosenbrock(x) f = 100*(x(:,2) - x(:,1).^2).^2 + (1 - x(:,1)).^2;
Note
rosenbrock
is a vectorized function that can compute values for several points at once. SeeVectorization. A vectorized function is best for plotting. For a nonvectorized version, enter:%% ROSENBROCK1(x) expects a two-element vector and returns a scalar%的输出是。function, which has a minimum at% (1,1) of value 0, and is strictly positive everywhere else.functionf = rosenbrock1(x) f = 100*(x(2) - x(1)^2)^2 + (1 - x(1))^2;
Save the file with the name
rosenbrock.m
.
Constraint functions have the formc(x) ≤ 0orceq(x) = 0.The constraint is not in the form that the solver handles. To have the correct syntax, reformulate the constraint as .
非线性的语法约束返回equality and inequality constraints. This example includes only an inequality constraint, so you must pass an empty array[]
as the equality constraint functionceq.
With these considerations in mind, write a function 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
.
Now that you have defined the objective and constraint functions, create the otherfmincon
inputs.
Create options for
fmincon
to use the'optimplotfvalconstr'
plot function and to return iterative display.options = optimoptions('fmincon',...'PlotFcn','optimplotfvalconstr',...'Display','iter');
Create the initial point.
x0 = [0 0];
Create empty entries for the constraints that this example does not use.
A = []; b = []; Aeq = []; beq = []; lb = []; ub = [];
Solve the problem by callingfmincon
.
[x,fval] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options)
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.440e-05 3.974e-05 22 78 4.568281e-02 0.000e+00 8.000e-06 1.084e-07 23 81 4.567641e-02 0.000e+00 1.601e-06 2.793e-05 24 84 4.567482e-02 0.000e+00 2.023e-08 6.916e-06 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 = 0.0457
The exit 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 required accuracy. Several phrases in the message contain links to more information about the terms used in the message. For more details about these links, seeEnhanced Exit Messages.
Interpret Result
The iteration table in both the Live Editor task output area and the MATLAB Command Window shows how MATLAB searched for the minimum value of Rosenbrock's function in the unit disk. Your table can differ, depending on toolbox version and computing platform. The following description applies to the table shown in this example.
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 anF-count
of 84, indicating thatfmincon
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,4.567482e-2
, is the minimum reported in theOptimizerun, 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 functionunitdisk
at each iteration where the constraint is positive. Because the value ofunitdisk
was negative in all iterations, every iteration satisfied the constraint.
The other columns of the iteration table are described inIterative Display.