Find minimum of constrained nonlinear multivariable function
Nonlinear programming solver.
Finds the minimum of a problem specified by
$$\underset{x}{\mathrm{min}}f(x)\text{suchthat}\{\begin{array}{c}c(x)\le 0\\ ceq(x)=0\\ A\cdot x\le b\\ Aeq\cdot x=beq\\ lb\le x\le ub,\end{array}$$
b and beq are vectors, A and Aeq are matrices, c(x) and ceq(x) are functions that return vectors, and f(x) is a function that returns a scalar. f(x), c(x), and ceq(x) can be nonlinear functions.
x, lb, and ub can be passed as vectors or matrices; see Matrix Arguments.
x = fmincon(fun,x0,A,b)
x = fmincon(fun,x0,A,b,Aeq,beq)
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub)
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon)
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options)
x = fmincon(problem)
[x,fval]
= fmincon(___)
[x,fval,exitflag,output]
= fmincon(___)
[x,fval,exitflag,output,lambda,grad,hessian]
= fmincon(___)
starts
at x
= fmincon(fun
,x0
,A
,b
)x0
and attempts to find a minimizer x
of
the function described in fun
subject to the linear
inequalities A*x ≤ b
. x0
can
be a scalar, vector, or matrix.
Passing Extra Parameters explains how to pass extra parameters to the objective function and nonlinear constraint functions, if necessary.
defines
a set of lower and upper bounds on the design variables in x
= fmincon(fun
,x0
,A
,b
,Aeq
,beq
,lb
,ub
)x
,
so that the solution is always in the range lb
≤ x
≤ ub
.
If no equalities exist, set Aeq = []
and beq
= []
. If x(i)
is unbounded below, set lb(i)
= Inf
, and if x(i)
is unbounded above,
set ub(i) = Inf
.
If the specified input bounds for a problem are inconsistent, fmincon
throws
an error. In this case, output x
is x0
and fval
is []
.
For the default 'interiorpoint'
algorithm, fmincon
sets
components of x0
that violate the bounds lb ≤ x ≤ ub
, or are equal to a bound, to the interior
of the bound region. For the 'trustregionreflective'
algorithm, fmincon
sets
violating components to the interior of the bound region. For other
algorithms, fmincon
sets violating components
to the closest bound. Components that respect the bounds are not changed.
See Iterations Can Violate Constraints.
finds
the minimum for x
= fmincon(problem
)problem
, where problem
is
a structure described in Input Arguments.
Create the problem
structure by exporting a problem
from Optimization app, as described in Exporting Your Work.
Find the minimum value of Rosenbrock's function when there is a linear inequality constraint.
Set the objective function fun
to be Rosenbrock's function. Rosenbrock's function is wellknown to be difficult to minimize. It has its minimum objective value of 0 at the point (1,1). For more information, see Solve a Constrained Nonlinear Problem, SolverBased.
fun = @(x)100*(x(2)x(1)^2)^2 + (1x(1))^2;
Find the minimum value starting from the point [1,2]
, constrained to have $$x(1)+2x(2)\le 1$$. Express this constraint in the form Ax <= b
by taking A = [1,2]
and b = 1
. Notice that this constraint means that the solution will not be at the unconstrained solution (1,1), because at that point $$x(1)+2x(2)=3>1$$.
x0 = [1,2]; A = [1,2]; b = 1; x = fmincon(fun,x0,A,b)
Local minimum found that satisfies the constraints. Optimization completed because the objective function is nondecreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
x = 1×2
0.5022 0.2489
Find the minimum value of Rosenbrock's function when there are both a linear inequality constraint and a linear equality constraint.
Set the objective function fun
to be Rosenbrock's function.
fun = @(x)100*(x(2)x(1)^2)^2 + (1x(1))^2;
Find the minimum value starting from the point [0.5,0]
, constrained to have $$x(1)+2x(2)\le 1$$ and $$2x(1)+x(2)=1$$.
Express the linear inequality constraint in the form A*x <= b
by taking A = [1,2]
and b = 1
.
Express the linear equality constraint in the form Aeq*x = beq
by taking Aeq = [2,1]
and beq = 1
.
x0 = [0.5,0]; A = [1,2]; b = 1; Aeq = [2,1]; beq = 1; x = fmincon(fun,x0,A,b,Aeq,beq)
Local minimum found that satisfies the constraints. Optimization completed because the objective function is nondecreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
x = 1×2
0.4149 0.1701
Find the minimum of an objective function in the presence of bound constraints.
The objective function is a simple algebraic function of two variables.
fun = @(x)1+x(1)/(1+x(2))  3*x(1)*x(2) + x(2)*(1+x(1));
Look in the region where x
has positive
values, x(1) ≤ 1, and x(2) ≤ 2.
lb = [0,0]; ub = [1,2];
There are no linear constraints, so set those arguments
to []
.
A = []; b = []; Aeq = []; beq = [];
Try an initial point in the middle of the region. Find
the minimum of fun
, subject to the bound constraints.
x0 = [0.5,1]; x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub)
Local minimum found that satisfies the constraints. Optimization completed because the objective function is nondecreasing 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. <stopping criteria details>
x = 1.0000 2.0000
A different initial point can lead to a different solution.
x0 = x0/5; x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub)
Local minimum found that satisfies the constraints. Optimization completed because the objective function is nondecreasing 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. <stopping criteria details>
x = 1.0e06 * 0.4000 0.4000
To see which solution is better, see Obtain the Objective Function Value.
Find the minimum of a function subject to nonlinear constraints
Find the point where Rosenbrock's function is minimized within a circle, also subject to bound constraints.
fun = @(x)100*(x(2)x(1)^2)^2 + (1x(1))^2;
Look within the region , .
lb = [0,0.2]; ub = [0.5,0.8];
Also look within the circle centered at [1/3,1/3] with radius 1/3. Copy the following code to a file on your MATLAB® path named circlecon.m
.
% Copyright 2015 The MathWorks, Inc. function [c,ceq] = circlecon(x) c = (x(1)1/3)^2 + (x(2)1/3)^2  (1/3)^2; ceq = [];
There are no linear constraints, so set those arguments to []
.
A = []; b = []; Aeq = []; beq = [];
Choose an initial point satisfying all the constraints.
x0 = [1/4,1/4];
Solve the problem.
nonlcon = @circlecon; x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon)
Local minimum found that satisfies the constraints. Optimization completed because the objective function is nondecreasing 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.5000 0.2500
Set options to view iterations as they occur and to use a different algorithm.
To observe the fmincon
solution process, set the Display
option to 'iter'
. Also, try the 'sqp'
algorithm, which is sometimes faster or more accurate than the default 'interiorpoint'
algorithm.
options = optimoptions('fmincon','Display','iter','Algorithm','sqp');
Find the minimum of Rosenbrock's function on the unit disk, . First create a function that represents the nonlinear constraint. Save this as a file named unitdisk.m
on your MATLAB® path.
function [c,ceq] = unitdisk(x)
c = x(1)^2 + x(2)^2  1;
ceq = [];
Create the remaining problem specifications. Then run fmincon
.
fun = @(x)100*(x(2)x(1)^2)^2 + (1x(1))^2; A = []; b = []; Aeq = []; beq = []; lb = []; ub = []; nonlcon = @unitdisk; x0 = [0,0]; x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options)
Iter Funccount Fval Feasibility Step Length Norm of Firstorder step optimality 0 3 1.000000e+00 0.000e+00 1.000e+00 0.000e+00 2.000e+00 1 12 8.913011e01 0.000e+00 1.176e01 2.353e01 1.107e+01 2 22 8.047847e01 0.000e+00 8.235e02 1.900e01 1.330e+01 3 28 4.197517e01 0.000e+00 3.430e01 1.217e01 6.172e+00 4 31 2.733703e01 0.000e+00 1.000e+00 5.254e02 5.705e01 5 34 2.397111e01 0.000e+00 1.000e+00 7.498e02 3.164e+00 6 37 2.036002e01 0.000e+00 1.000e+00 5.960e02 3.106e+00 7 40 1.164353e01 0.000e+00 1.000e+00 1.459e01 1.059e+00 8 43 1.161753e01 0.000e+00 1.000e+00 1.754e01 7.383e+00 9 46 5.901600e02 0.000e+00 1.000e+00 1.547e02 7.278e01 10 49 4.533081e02 2.898e03 1.000e+00 5.393e02 1.252e01 11 52 4.567454e02 2.225e06 1.000e+00 1.492e03 1.679e03 12 55 4.567481e02 4.405e12 1.000e+00 2.095e06 1.502e05 13 58 4.567481e02 2.220e16 1.000e+00 2.442e09 1.287e05 Local minimum possible. Constraints satisfied. fmincon stopped because the size of the current step is less than the value of the step size tolerance and constraints are satisfied to within the value of the constraint tolerance. x = 0.7864 0.6177
Include gradient evaluation in the objective function for faster or more reliable computations.
Include the gradient evaluation as a conditionalized output in the objective function file. For details, see Including Gradients and Hessians. The objective function is Rosenbrock's function,
which has gradient
function [f,g] = rosenbrockwithgrad(x) % Calculate objective f f = 100*(x(2)  x(1)^2)^2 + (1x(1))^2; if nargout > 1 % gradient required g = [400*(x(2)x(1)^2)*x(1)2*(1x(1)); 200*(x(2)x(1)^2)]; end
Save this code as a file named rosenbrockwithgrad.m
on your MATLAB® path.
Create options to use the objective function gradient.
options = optimoptions('fmincon','SpecifyObjectiveGradient',true);
Create the other inputs for the problem. Then call fmincon
.
fun = @rosenbrockwithgrad; x0 = [1,2]; A = []; b = []; Aeq = []; beq = []; lb = [2,2]; ub = [2,2]; nonlcon = []; x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options)
Local minimum found that satisfies the constraints. Optimization completed because the objective function is nondecreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance. x = 1.0000 1.0000
Solve the same problem as in Nondefault Options using a problem structure instead of separate arguments.
Create
the options and a problem structure. See problem
for the field names and required
fields.
options = optimoptions('fmincon','Display','iter','Algorithm','sqp'); problem.options = options; problem.solver = 'fmincon'; problem.objective = @(x)100*(x(2)x(1)^2)^2 + (1x(1))^2; problem.x0 = [0,0];
Create a function file for the nonlinear constraint function representing norm(x)^{2} ≤ 1.
function [c,ceq] = unitdisk(x) c = x(1)^2 + x(2)^2  1; ceq = [ ];
Save this as a file named unitdisk.m
on your MATLAB^{®} path.
Include the nonlinear constraint function in problem
.
problem.nonlcon = @unitdisk;
Solve the problem.
x = fmincon(problem)
Iter Funccount Fval Feasibility Step Length Norm of Firstorder step optimality 0 3 1.000000e+00 0.000e+00 1.000e+00 0.000e+00 2.000e+00 1 12 8.913011e01 0.000e+00 1.176e01 2.353e01 1.107e+01 2 22 8.047847e01 0.000e+00 8.235e02 1.900e01 1.330e+01 3 28 4.197517e01 0.000e+00 3.430e01 1.217e01 6.172e+00 4 31 2.733703e01 0.000e+00 1.000e+00 5.254e02 5.705e01 5 34 2.397111e01 0.000e+00 1.000e+00 7.498e02 3.164e+00 6 37 2.036002e01 0.000e+00 1.000e+00 5.960e02 3.106e+00 7 40 1.164353e01 0.000e+00 1.000e+00 1.459e01 1.059e+00 8 43 1.161753e01 0.000e+00 1.000e+00 1.754e01 7.383e+00 9 46 5.901602e02 0.000e+00 1.000e+00 1.547e02 7.278e01 10 49 4.533081e02 2.898e03 1.000e+00 5.393e02 1.252e01 11 52 4.567454e02 2.225e06 1.000e+00 1.492e03 1.679e03 12 55 4.567481e02 4.406e12 1.000e+00 2.095e06 1.501e05 13 58 4.567481e02 0.000e+00 1.000e+00 2.158e09 1.511e05 Local minimum possible. Constraints satisfied. fmincon stopped because the size of the current step is less than the value of the step size tolerance and constraints are satisfied to within the value of the constraint tolerance. <stopping criteria details>
x = 0.7864 0.6177
The iterative display and solution are the same as in Nondefault Options.
Call fmincon
with the fval
output
to obtain the value of the objective function at the solution.
The Bound Constraints example shows two solutions.
Which is better? Run the example requesting the fval
output
as well as the solution.
fun = @(x)1+x(1)./(1+x(2))  3*x(1).*x(2) + x(2).*(1+x(1)); lb = [0,0]; ub = [1,2]; A = []; b = []; Aeq = []; beq = []; x0 = [0.5,1]; [x,fval] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub)
Local minimum found that satisfies the constraints. Optimization completed because the objective function is nondecreasing 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. <stopping criteria details>
x = 1.0000 2.0000 fval = 0.6667
Run the problem using a different starting point x0
.
x0 = x0/5; [x2,fval2] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub)
Local minimum found that satisfies the constraints. Optimization completed because the objective function is nondecreasing 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. <stopping criteria details>
x2 = 1.0e06 * 0.4000 0.4000 fval2 = 1.0000
This solution has an objective function value fval2
= 1
, which is higher than the first value fval
= 0.6667
. The first solution x
has a
lower local minimum objective function value.
To easily examine the quality of a solution, request the exitflag
and output
outputs.
Set up the problem of minimizing Rosenbrock's function on the unit disk, . First create a function that represents the nonlinear constraint. Save this as a file named unitdisk.m
on your MATLAB® path.
function [c,ceq] = unitdisk(x)
c = x(1)^2 + x(2)^2  1;
ceq = [];
Create the remaining problem specifications.
fun = @(x)100*(x(2)x(1)^2)^2 + (1x(1))^2; nonlcon = @unitdisk; A = []; b = []; Aeq = []; beq = []; lb = []; ub = []; x0 = [0,0];
Call fmincon
using the fval
, exitflag
, and output
outputs.
[x,fval,exitflag,output] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon)
Local minimum found that satisfies the constraints. Optimization completed because the objective function is nondecreasing 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 exitflag = 1 output = struct with fields: iterations: 24 funcCount: 84 constrviolation: 0 stepsize: 6.9162e06 algorithm: 'interiorpoint' firstorderopt: 2.4373e08 cgiterations: 4 message: '...'
The exitflag
value 1
indicates that the solution is a local minimum.
The output
structure reports several statistics about the solution process. In particular, it gives the number of iterations in output.iterations
, number of function evaluations in output.funcCount
, and the feasibility in output.constrviolation
.
fmincon
optionally returns several outputs that you can use for analyzing the reported solution.
Set up the problem of minimizing Rosenbrock's function on the unit disk. First create a function that represents the nonlinear constraint. Save this as a file named unitdisk.m
on your MATLAB® path.
function [c,ceq] = unitdisk(x)
c = x(1)^2 + x(2)^2  1;
ceq = [];
Create the remaining problem specifications.
fun = @(x)100*(x(2)x(1)^2)^2 + (1x(1))^2; nonlcon = @unitdisk; A = []; b = []; Aeq = []; beq = []; lb = []; ub = []; x0 = [0,0];
Request all fmincon
outputs.
[x,fval,exitflag,output,lambda,grad,hessian] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon)
Local minimum found that satisfies the constraints. Optimization completed because the objective function is nondecreasing 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 exitflag = 1 output = struct with fields: iterations: 24 funcCount: 84 constrviolation: 0 stepsize: 6.9162e06 algorithm: 'interiorpoint' firstorderopt: 2.4373e08 cgiterations: 4 message: '...' lambda = struct with fields: eqlin: [0x1 double] eqnonlin: [0x1 double] ineqlin: [0x1 double] lower: [2x1 double] upper: [2x1 double] ineqnonlin: 0.1215 grad = 0.1911 0.1501 hessian = 497.2903 314.5589 314.5589 200.2392
The lambda.ineqnonlin
output shows that the nonlinear constraint is active at the solution, and gives the value of the associated Lagrange multiplier.
The grad
output gives the value of the gradient of the objective function at the solution x
.
The hessian
output is described in fmincon Hessian.
fun
— Function to minimizeFunction to minimize, specified as a function handle or function
name. fun
is a function that accepts a vector or
array x
and returns a real scalar f
,
the objective function evaluated at x
.
Specify fun
as a function handle for a file:
x = fmincon(@myfun,x0,A,b)
where myfun
is a MATLAB function such
as
function f = myfun(x) f = ... % Compute function value at x
You can also specify fun
as a function handle
for an anonymous function:
x = fmincon(@(x)norm(x)^2,x0,A,b);
If you can compute the gradient of fun
and the SpecifyObjectiveGradient
option
is set to true
, as set by
options = optimoptions('fmincon','SpecifyObjectiveGradient',true)
fun
must
return the gradient vector g(x)
in the second output
argument.
If you can also compute the Hessian matrix and the HessianFcn
option
is set to 'objective'
via optimoptions
and the Algorithm
option
is 'trustregionreflective'
, fun
must
return the Hessian value H(x)
, a symmetric matrix,
in a third output argument. fun
can give a sparse
Hessian. See Hessian for fminunc trustregion or fmincon trustregionreflective algorithms for
details.
If you can also compute the Hessian matrix and the Algorithm
option
is set to 'interiorpoint'
, there is a different
way to pass the Hessian to fmincon
. For more
information, see Hessian for fmincon interiorpoint algorithm. For an example
using Symbolic Math
Toolbox™ to compute the gradient and Hessian,
see Symbolic Math Toolbox Calculates Gradients and Hessians.
The interiorpoint
and trustregionreflective
algorithms
allow you to supply a Hessian multiply function. This function gives
the result of a Hessiantimesvector product without computing the
Hessian directly. This can save memory. See Hessian Multiply Function.
Example: fun = @(x)sin(x(1))*cos(x(2))
Data Types: char
 function_handle
 string
x0
— Initial pointInitial point, specified as a real vector or real array. Solvers use the
number of elements in, and size of, x0
to determine the
number and size of variables that fun
accepts.
'interiorpoint'
algorithm — If the
HonorBounds
option is true
(default), fmincon
resets x0
components that are on or outside bounds lb
or
ub
to values
strictly between the bounds.
'trustregionreflective'
algorithm —
fmincon
resets infeasible
x0
components to be feasible with respect to
bounds or linear equalities.
'sqp'
, 'sqplegacy'
, or
'activeset'
algorithm —
fmincon
resets x0
components that are outside bounds to the values of the
corresponding bounds.
Example: x0 = [1,2,3,4]
Data Types: double
A
— Linear inequality constraintsLinear inequality constraints, specified as a real matrix. A
is
an M
byN
matrix, where M
is
the number of inequalities, and N
is the number
of variables (number of elements in x0
). For
large problems, pass A
as a sparse matrix.
A
encodes the M
linear
inequalities
A*x <= b
,
where x
is the column vector of N
variables x(:)
,
and b
is a column vector with M
elements.
For example, to specify
x_{1} + 2x_{2} ≤
10
3x_{1} +
4x_{2} ≤ 20
5x_{1} +
6x_{2} ≤ 30,
enter these constraints:
A = [1,2;3,4;5,6]; b = [10;20;30];
Example: To specify that the x components sum to 1 or less, use A =
ones(1,N)
and b = 1
.
Data Types: double
b
— Linear inequality constraintsLinear inequality constraints, specified as a real vector. b
is
an M
element vector related to the A
matrix.
If you pass b
as a row vector, solvers internally
convert b
to the column vector b(:)
.
For large problems, pass b
as a sparse vector.
b
encodes the M
linear
inequalities
A*x <= b
,
where x
is the column vector of N
variables x(:)
,
and A
is a matrix of size M
byN
.
For example, to specify
x_{1} + 2x_{2} ≤
10
3x_{1} +
4x_{2} ≤ 20
5x_{1} +
6x_{2} ≤ 30,
enter these constraints:
A = [1,2;3,4;5,6]; b = [10;20;30];
Example: To specify that the x components sum to 1 or less, use A =
ones(1,N)
and b = 1
.
Data Types: double
Aeq
— Linear equality constraintsLinear equality constraints, specified as a real matrix. Aeq
is
an Me
byN
matrix, where Me
is
the number of equalities, and N
is the number of
variables (number of elements in x0
). For large
problems, pass Aeq
as a sparse matrix.
Aeq
encodes the Me
linear
equalities
Aeq*x = beq
,
where x
is the column vector of N
variables x(:)
,
and beq
is a column vector with Me
elements.
For example, to specify
x_{1} + 2x_{2} +
3x_{3} = 10
2x_{1} +
4x_{2} + x_{3} =
20,
enter these constraints:
Aeq = [1,2,3;2,4,1]; beq = [10;20];
Example: To specify that the x components sum to 1, use Aeq = ones(1,N)
and
beq = 1
.
Data Types: double
beq
— Linear equality constraintsLinear equality constraints, specified as a real vector. beq
is
an Me
element vector related to the Aeq
matrix.
If you pass beq
as a row vector, solvers internally
convert beq
to the column vector beq(:)
.
For large problems, pass beq
as a sparse vector.
beq
encodes the Me
linear
equalities
Aeq*x = beq
,
where x
is the column vector of N
variables
x(:)
, and Aeq
is a matrix of size
Me
byN
.
For example, to specify
x_{1} + 2x_{2} +
3x_{3} = 10
2x_{1} +
4x_{2} + x_{3} =
20,
enter these constraints:
Aeq = [1,2,3;2,4,1]; beq = [10;20];
Example: To specify that the x components sum to 1, use Aeq = ones(1,N)
and
beq = 1
.
Data Types: double
lb
— Lower boundsLower bounds, specified as a real vector or real array. If the number of elements in
x0
is equal to the number of elements in lb
,
then lb
specifies that
x(i) >= lb(i)
for all i
.
If numel(lb) < numel(x0)
, then lb
specifies
that
x(i) >= lb(i)
for 1 <=
i <= numel(lb)
.
If there are fewer elements in lb
than in x0
, solvers
issue a warning.
Example: To specify that all x components are positive, use lb =
zeros(size(x0))
.
Data Types: double
ub
— Upper boundsUpper bounds, specified as a real vector or real array. If the number of elements in
x0
is equal to the number of elements in ub
,
then ub
specifies that
x(i) <= ub(i)
for all i
.
If numel(ub) < numel(x0)
, then ub
specifies
that
x(i) <= ub(i)
for 1 <=
i <= numel(ub)
.
If there are fewer elements in ub
than in x0
, solvers
issue a warning.
Example: To specify that all x components are less than 1, use ub =
ones(size(x0))
.
Data Types: double
nonlcon
— Nonlinear constraintsNonlinear constraints, specified as a function handle or function
name. nonlcon
is a function that accepts a vector
or array x
and returns two arrays, c(x)
and ceq(x)
.
c(x)
is the array of nonlinear
inequality constraints at x
. fmincon
attempts
to satisfy
c(x) <= 0
for all entries of c
.
ceq(x)
is the array of nonlinear
equality constraints at x
. fmincon
attempts
to satisfy
ceq(x) = 0
for all entries of ceq
.
For example,
x = fmincon(@myfun,x0,A,b,Aeq,beq,lb,ub,@mycon)
where mycon
is a MATLAB function such
as
function [c,ceq] = mycon(x) c = ... % Compute nonlinear inequalities at x. ceq = ... % Compute nonlinear equalities at x.
SpecifyConstraintGradient
option
is true
, as set byoptions = optimoptions('fmincon','SpecifyConstraintGradient',true)
nonlcon
must
also return, in the third and fourth output arguments, GC
,
the gradient of c(x)
, and GCeq
,
the gradient of ceq(x)
. GC
and GCeq
can
be sparse or dense. If GC
or GCeq
is
large, with relatively few nonzero entries, save running time and
memory in the interiorpoint
algorithm by representing
them as sparse matrices. For more information, see Nonlinear Constraints.
Data Types: char
 function_handle
 string
options
— Optimization optionsoptimoptions
 structure such as optimset
returnsOptimization options, specified as the output of optimoptions
or
a structure such as optimset
returns.
Some options apply to all algorithms, and others are relevant for particular algorithms. See Optimization Options Reference for detailed information.
Some options are absent from the
optimoptions
display. These options appear in italics in the following
table. For details, see View Options.
All Algorithms  
Algorithm  Choose the optimization algorithm:
For information on choosing the algorithm, see Choosing the Algorithm. The
If you select the The 
CheckGradients  Compare usersupplied derivatives
(gradients of objective or constraints) to finitedifferencing derivatives.
Choices are For 
ConstraintTolerance  Tolerance on the constraint violation, a positive scalar. The
default is For 
Diagnostics  Display diagnostic information
about the function to be minimized or solved. Choices are 
DiffMaxChange  Maximum change in variables for
finitedifference gradients (a positive scalar). The default is 
DiffMinChange  Minimum change in variables for
finitedifference gradients (a positive scalar). The default is 
Display  Level of display (see Iterative Display):

FiniteDifferenceStepSize  Scalar or vector step size factor for finite differences. When
you set
sign′(x) = sign(x) except sign′(0) = 1 .
Central finite differences are
FiniteDifferenceStepSize expands to a vector. The default
is sqrt(eps) for forward finite differences, and eps^(1/3)
for central finite differences.
For 
FiniteDifferenceType  Finite differences, used to estimate
gradients, are either
For 
FunValCheck  Check whether objective function
values are valid. The default setting, 
MaxFunctionEvaluations  Maximum number of function evaluations
allowed, a positive integer. The default value for all algorithms
except For 
MaxIterations  Maximum number of iterations allowed,
a positive integer. The default value for all algorithms except For 
OptimalityTolerance  Termination tolerance on the firstorder optimality (a positive
scalar). The default is For 
OutputFcn  Specify one or more userdefined functions that an optimization
function calls at each iteration. Pass a function handle
or a cell array of function handles. The default is none
( 
PlotFcn  Plots various measures of progress while the algorithm executes;
select from predefined plots or write your own. Pass a
builtin plot function name, a function handle, or a
cell array of builtin plot function names or function
handles. For custom plot functions, pass function
handles. The default is none
(
For information on writing a custom plot function, see Plot Function Syntax. For 
SpecifyConstraintGradient  Gradient for nonlinear constraint
functions defined by the user. When set to the default, For 
SpecifyObjectiveGradient  Gradient for the objective function
defined by the user. See the description of For 
StepTolerance  Termination tolerance on For 
TypicalX  Typical The 
UseParallel  When 
TrustRegionReflective Algorithm  
FunctionTolerance  Termination tolerance on the function
value, a positive scalar. The default is For 
HessianFcn  If For 
HessianMultiplyFcn  Hessian multiply function,
specified as a function handle. For largescale
structured problems, this function computes the Hessian
matrix product W = hmfun(Hinfo,Y) where
The first
argument is the same as the third argument returned by
the objective function [f,g,Hinfo] = fun(x)
NoteTo use the See Hessian Multiply Function. See Minimization with Dense Structured Hessian, Linear Equalities for an example. For

HessPattern  Sparsity pattern of the Hessian
for finite differencing. Set Use When the structure is unknown,
do not set 
MaxPCGIter  Maximum number of preconditioned conjugate gradient (PCG) iterations, a positive scalar. The
default is

PrecondBandWidth  Upper bandwidth of preconditioner
for PCG, a nonnegative integer. By default, diagonal preconditioning
is used (upper bandwidth of 0). For some problems, increasing the
bandwidth reduces the number of PCG iterations. Setting 
SubproblemAlgorithm  Determines how the iteration step
is calculated. The default, 
TolPCG  Termination tolerance on the PCG
iteration, a positive scalar. The default is 
ActiveSet Algorithm  
FunctionTolerance  Termination tolerance on the function
value, a positive scalar. The default is For 
MaxSQPIter  Maximum number of SQP iterations
allowed, a positive integer. The default is 
RelLineSrchBnd  Relative bound (a real nonnegative
scalar value) on the line search step length. The total displacement
in x satisfies Δx(i) ≤ relLineSrchBnd· max(x(i),typicalx(i)).
This option provides control over the magnitude of the displacements
in x for cases in which the solver takes steps
that are considered too large. The default is no bounds ( 
RelLineSrchBndDuration  Number of iterations for which
the bound specified in 
TolConSQP  Termination tolerance on inner
iteration SQP constraint violation, a positive scalar. The default
is 
InteriorPoint Algorithm  
HessianApproximation  Chooses how
NoteTo use For 
HessianFcn  If For 
HessianMultiplyFcn  Usersupplied function that gives a Hessiantimesvector product (see Hessian Multiply Function). Pass a function handle. NoteTo use the For 
HonorBounds  The default For 
InitBarrierParam  Initial barrier value, a positive
scalar. Sometimes it might help to try a value above the default 
InitTrustRegionRadius  Initial radius of the trust region, a positive scalar. On badly scaled problems it might help to choose a value smaller than the default $$\sqrt{n}$$, where n is the number of variables. 
MaxProjCGIter  A tolerance (stopping criterion)
for the number of projected conjugate gradient iterations; this is
an inner iteration, not the number of iterations of the algorithm.
This positive integer has a default value of 
ObjectiveLimit  A tolerance (stopping criterion)
that is a scalar. If the objective function value goes below 
ScaleProblem 
For 
SubproblemAlgorithm  Determines how the iteration step
is calculated. The default, 
TolProjCG  A relative tolerance (stopping
criterion) for projected conjugate gradient algorithm; this is for
an inner iteration, not the algorithm iteration. This positive scalar
has a default of 
TolProjCGAbs  Absolute tolerance (stopping criterion)
for projected conjugate gradient algorithm; this is for an inner iteration,
not the algorithm iteration. This positive scalar has a default of 
SQP and SQP Legacy Algorithms  
ObjectiveLimit  A tolerance (stopping criterion)
that is a scalar. If the objective function value goes below 
ScaleProblem 
For 
Example: options = optimoptions('fmincon','SpecifyObjectiveGradient',true,'SpecifyConstraintGradient',true)
problem
— Problem structureProblem structure, specified as a structure with the following fields:
Field Name  Entry 

 Objective function 
 Initial point for x 
 Matrix for linear inequality constraints 
 Vector for linear inequality constraints 
 Matrix for linear equality constraints 
 Vector for linear equality constraints 
lb  Vector of lower bounds 
ub  Vector of upper bounds 
 Nonlinear constraint function 
 'fmincon' 
 Options created with optimoptions 
You must supply at least the objective
, x0
, solver
,
and options
fields in the problem
structure.
The simplest way to obtain a problem
structure
is to export the problem from the Optimization app.
Data Types: struct
x
— SolutionSolution, returned as a real vector or real array. The size
of x
is the same as the size of x0
.
Typically, x
is a local solution to the problem
when exitflag
is positive. For information on
the quality of the solution, see When the Solver Succeeds.
fval
— Objective function value at solutionObjective function value at the solution, returned as a real
number. Generally, fval
= fun(x)
.
exitflag
— Reason fmincon
stoppedReason fmincon
stopped, returned as an
integer.
All Algorithms:  
 Firstorder optimality measure was less than 
 Number of iterations exceeded 
 Stopped by an output function or plot function. 
 No feasible point was found. 
All algorithms except  
 Change in 
 
 Change in the objective function value was less than 
 
 Magnitude of the search direction was less than 2* 
 Magnitude of directional derivative in search direction
was less than 2* 
 
 Objective function at current iteration went below 
output
— Information about the optimization processInformation about the optimization process, returned as a structure with fields:
iterations  Number of iterations taken 
funcCount  Number of function evaluations 
lssteplength  Size of line search step relative to search direction
( 
constrviolation  Maximum of constraint functions 
stepsize  Length of last displacement in 
algorithm  Optimization algorithm used 
cgiterations  Total number of PCG iterations ( 
firstorderopt  Measure of firstorder optimality 
message  Exit message 
lambda
— Lagrange multipliers at the solutiongrad
— Gradient at the solutionGradient at the solution, returned as a real vector. grad
gives
the gradient of fun
at the point x(:)
.
hessian
— Approximate HessianApproximate Hessian, returned as a real matrix. For the meaning
of hessian
, see Hessian.
fmincon
is a gradientbased method
that is designed to work on problems where the objective and constraint
functions are both continuous and have continuous first derivatives.
For the 'trustregionreflective'
algorithm,
you must provide the gradient in fun
and set
the 'SpecifyObjectiveGradient'
option to true
.
The 'trustregionreflective'
algorithm
does not allow equal upper and lower bounds. For example, if lb(2)==ub(2)
, fmincon
gives
this error:
Equal upper and lower bounds not permitted in trustregionreflective algorithm. Use either interiorpoint or SQP algorithms instead.
There are two different syntaxes for passing a Hessian,
and there are two different syntaxes for passing a HessianMultiplyFcn
function;
one for trustregionreflective
, and another for interiorpoint
.
See Including Hessians.
For trustregionreflective
, the
Hessian of the Lagrangian is the same as the Hessian of the objective
function. You pass that Hessian as the third output of the objective
function.
For interiorpoint
, the Hessian
of the Lagrangian involves the Lagrange multipliers and the Hessians
of the nonlinear constraint functions. You pass the Hessian as a separate
function that takes into account both the current point x
and
the Lagrange multiplier structure lambda
.
When the problem is infeasible, fmincon
attempts
to minimize the maximum constraint value.
fmincon
uses a Hessian
as an optional input. This Hessian is the matrix of second derivatives
of the Lagrangian (see Equation 1), namely,
$${\nabla}_{xx}^{2}L(x,\lambda )={\nabla}^{2}f(x)+{\displaystyle \sum {\lambda}_{i}{\nabla}^{2}{c}_{i}(x)}+{\displaystyle \sum {\lambda}_{i}{\nabla}^{2}ce{q}_{i}(x)}.$$  (1) 
For details of how to supply a Hessian to the trustregionreflective
or interiorpoint
algorithms,
see Including Hessians.
The activeset
and sqp
algorithms
do not accept an input Hessian. They compute a quasiNewton approximation
to the Hessian of the Lagrangian.
The interiorpoint
algorithm has several choices for the
'HessianApproximation'
option; see Choose Input Hessian Approximation for interiorpoint fmincon:
'bfgs'
— fmincon
calculates the Hessian by a dense quasiNewton approximation. This is
the default Hessian approximation.
'lbfgs'
— fmincon
calculates the Hessian by a limitedmemory, largescale quasiNewton
approximation. The default memory, 10 iterations, is used.
{'lbfgs',positive integer}
—
fmincon
calculates the Hessian by a
limitedmemory, largescale quasiNewton approximation. The positive
integer specifies how many past iterations should be remembered.
'finitedifference'
—
fmincon
calculates a Hessiantimesvector
product by finite differences of the gradient(s). You must supply the
gradient of the objective function, and also gradients of nonlinear
constraints (if they exist). Set the
'SpecifyObjectiveGradient'
option to
true
and, if applicable, the
'SpecifyConstraintGradient'
option to
true
. You must set the
'SubproblemAlgorithm'
to
'cg'
.
The interiorpoint
and trustregionreflective
algorithms
allow you to supply a Hessian multiply function. This function gives
the result of a Hessiantimesvector product, without computing the
Hessian directly. This can save memory. For details, see Hessian Multiply Function.
For help choosing the algorithm, see fmincon Algorithms. To set the algorithm, use optimoptions
to create options
, and use the
'Algorithm'
namevalue pair.
The rest of this section gives brief summaries or pointers to information about each algorithm.
This algorithm is described in fmincon Interior Point Algorithm. There is more extensive description in [1], [41], and [9].
The fmincon
'sqp'
and 'sqplegacy'
algorithms
are similar to the 'activeset'
algorithm described
in ActiveSet Optimization. fmincon SQP Algorithm describes the main
differences. In summary, these differences are:
fmincon
uses a sequential quadratic programming (SQP) method. In this
method, the function solves a quadratic
programming (QP) subproblem at each iteration. fmincon
updates
an estimate of the Hessian of the Lagrangian at each iteration using
the BFGS formula (see fminunc
and
references [7] and [8]).
fmincon
performs a line search using a
merit function similar to that proposed by [6], [7], and [8]. The QP subproblem is solved using
an active set strategy similar to that described in [5]. fmincon Active Set Algorithm describes this algorithm in
detail.
See also SQP Implementation for more details on the algorithm used.
The 'trustregionreflective'
algorithm is
a subspace trustregion method and is based on the interiorreflective
Newton method described in [3] and [4]. Each iteration involves the approximate
solution of a large linear system using the method of preconditioned
conjugate gradients (PCG). See the trustregion and preconditioned
conjugate gradient method descriptions in fmincon Trust Region Reflective Algorithm.
[1] Byrd, R. H., J. C. Gilbert, and J. Nocedal. “A Trust Region Method Based on Interior Point Techniques for Nonlinear Programming.” Mathematical Programming, Vol 89, No. 1, 2000, pp. 149–185.
[2] Byrd, R. H., Mary E. Hribar, and Jorge Nocedal. “An Interior Point Algorithm for LargeScale Nonlinear Programming.” SIAM Journal on Optimization, Vol 9, No. 4, 1999, pp. 877–900.
[3] Coleman, T. F. and Y. Li. “An Interior, Trust Region Approach for Nonlinear Minimization Subject to Bounds.” SIAM Journal on Optimization, Vol. 6, 1996, pp. 418–445.
[4] Coleman, T. F. and Y. Li. “On the Convergence of Reflective Newton Methods for LargeScale Nonlinear Minimization Subject to Bounds.” Mathematical Programming, Vol. 67, Number 2, 1994, pp. 189–224.
[5] Gill, P. E., W. Murray, and M. H. Wright. Practical Optimization, London, Academic Press, 1981.
[6] Han, S. P. “A Globally Convergent Method for Nonlinear Programming.” Journal of Optimization Theory and Applications, Vol. 22, 1977, pp. 297.
[7] Powell, M. J. D. “A Fast Algorithm for Nonlinearly Constrained Optimization Calculations.” Numerical Analysis, ed. G. A. Watson, Lecture Notes in Mathematics, SpringerVerlag, Vol. 630, 1978.
[8] Powell, M. J. D. “The Convergence of Variable Metric Methods For Nonlinearly Constrained Optimization Calculations.” Nonlinear Programming 3 (O. L. Mangasarian, R. R. Meyer, and S. M. Robinson, eds.), Academic Press, 1978.
[9] Waltz, R. A., J. L. Morales, J. Nocedal, and D. Orban. “An interior algorithm for nonlinear optimization that combines line search and trust region steps.” Mathematical Programming, Vol 107, No. 3, 2006, pp. 391–408.
To run in parallel, set the 'UseParallel'
option to true
.
options = optimoptions('
solvername
','UseParallel',true)
For more information, see Using Parallel Computing in Optimization Toolbox.
fminbnd
 fminsearch
 fminunc
 optimoptions
 optimtool
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
Select web siteYou can also select a web site from the following list:
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.