| Products & Services | Industries | 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… |
|---|
Optimization Toolbox solvers have special forms for constraints. Constraints are separated into the following types:
Bound Constraints — Lower and upper bounds on individual components: x ≥ l and x ≤ u.
Linear Inequality Constraints — A·x ≤ b. A is an m-by-n matrix, which represents m constraints for an n-dimensional vector x. b is m-dimensional.
Linear Equality Constraints — Aeq·x = beq. This is a system of equations.
Nonlinear Constraints — c(x) ≤ 0 and ceq(x) = 0. Both c and ceq are scalars or vectors representing several constraints.
Optimization Toolbox functions assume that inequality constraints are of the form ci(x) ≤ 0 or A x ≤ b. Greater-than constraints are expressed as less-than constraints by multiplying them by –1. For example, a constraint of the form ci(x) ≥ 0 is equivalent to the constraint –ci(x) ≤ 0. A constraint of the form A·x ≥ b is equivalent to the constraint –A·x ≤ –b. For more information, see Linear Inequality Constraints and Nonlinear Constraints.
You can sometimes write constraints in a variety of ways. To make the best use of the solvers, use the lowest numbered constraints possible:
Bounds
Linear equalities
Linear inequalities
Nonlinear equalities
Nonlinear inequalities
For example, with a constraint 5 x ≤ 20, use a bound x ≤ 4 instead of a linear inequality or nonlinear inequality.
For information on how to pass extra parameters to constraint functions, see Passing Extra Parameters.
Be careful when writing your objective and constraint functions. Intermediate iterations can lead to points that are infeasible (do not satisfy constraints). If you write objective or constraint functions that assume feasibility, these functions can error or give unexpected results.
For example, if you take a square root or logarithm of x, and x < 0, the result is not real. You can try to avoid this error by setting 0 as a lower bound on x. Nevertheless, an intermediate iteration can violate this bound.
Some algorithms honor bound constraints at every iteration. These algorithms include:
The trust-region-reflective algorithm (fmincon, lsqnonlin, lsqcurvefit)
The fmincon interior-point algorithm
No algorithm satisfies equality constraints at all iterations.
Lower and upper bounds on the components of the vector x. You need not give gradients for this type of constraint; solvers calculate them automatically. Bounds do not affect Hessians.
If you know bounds on the location of your optimum, then you may obtain faster and more reliable solutions by explicitly including these bounds in your problem formulation.
Bounds are given as vectors, with the same length as x.
If a particular component is not bounded below, use –Inf as the bound; similarly, use Inf if a component is not bounded above.
If you have only bounds of one type (upper or lower), you do not need to write the other type. For example, if you have no upper bounds, you do not need to supply a vector of Infs. Also, if only the first m out of n components are bounded, then you need only supply a vector of length m containing bounds.
For example, suppose your bounds are:
x3 ≥ 8
x2 ≤ 3
Write the constraint vectors as
l = [–Inf; –Inf; 8]
u = [Inf; 3] or u = [Inf; 3; Inf]
Linear inequality constraints are of the form A·x ≤ b. When A is m-by-n, this represents m constraints on a variable x with n components. You supply the m-by-n matrix A and the m-component vector b. You do not need to give gradients for this type of constraint; solvers calculate them automatically. Linear inequalities do not affect Hessians.
For example, suppose that you have the following linear inequalities as constraints:
x1 + x3 ≤
4,
2x2 – x3 ≥
–2,
x1 – x2 + x3 – x4 ≥
9.
Here m = 3 and n = 4.
Write these using the following matrix A and vector b:

Notice that the "greater than" inequalities were first multiplied by –1 in order to get them into "less than" inequality form.
Linear equalities are of the form Aeq·x = beq. This represents m equations with n-component vector x. You supply the m-by-n matrix Aeq and the m-component vector beq. You do not need to give a gradient for this type of constraint; solvers calculate them automatically. Linear equalities do not affect Hessians. The form of this type of constraint is exactly the same as for Linear Inequality Constraints. Equalities rather than inequalities are implied by the position in the input argument list of the various solvers.
Nonlinear inequality constraints are of the form c(x) ≤ 0, where c is a vector of constraints, one component for each constraint. Similarly, nonlinear equality constraints are of the form ceq(x) = 0.
Nonlinear constraint functions must return both inequality and equality constraints, even if they do not both exist. Return empty [] for a nonexistent constraint.
If you provide gradients for c and ceq, your solver may run faster and give more reliable results.
For example, suppose that you have the following inequalities as constraints:

Write these constraints in an M-file as follows:
function [c,ceq]=ellipseparabola(x) % Inside the ellipse bounded by (-3<x<3),(-2<y<2) % Above the line y=x^2-1 c(1) = (x(1)^2)/9 + (x(2)^2)/4 - 1; c(2) = x(1)^2 - x(2) - 1; ceq = []; end
ellipseparabola returns empty [] as the nonlinear equality function. Also, both inequalities were put into ≤ 0 form.
To include gradient information, write a conditionalized function as follows:
function [c,ceq,gradc,gradceq]=ellipseparabola(x)
% Inside the ellipse bounded by (-3<x<3),(-2<y<2)
% Above the line y=x^2-1
c(1) = x(1)^2/9 + x(2)^2/4 - 1;
c(2) = x(1)^2 - x(2) - 1;
ceq = [];
if nargout > 2
gradc = [2*x(1)/9, 2*x(1);...
x(2)/2, -1];
gradceq = [];
endSee Writing Objective Functions for information on conditionalized gradients. The gradient matrix is of the form
gradci, j = [∂c(j)/∂xi].
The first column of the gradient matrix is associated with c(1), and the second column is associated with c(2). This is the transpose of the form of Jacobians.
To have a solver use gradients of nonlinear constraints, indicate that they exist by using optimset:
options=optimset('GradConstr','on');Make sure to pass the options structure to your solver:
[x,fval] = fmincon(@myobj,x0,A,b,Aeq,beq,lb,ub,...
@ellipseparabola,options)If you have a license for Symbolic Math Toolbox software, you can calculate gradients and Hessians automatically, as described in Example: Using Symbolic Math Toolbox Functions to Calculate Gradients and Hessians.
This section contains an example of a nonlinear minimization problem with all possible types of constraints. The objective function is in the subfunction myobj(x). The nonlinear constraints are in the subfunction myconstr(x). This example does not use gradients.
function fullexample
x0 = [1; 4; 5; 2; 5];
lb = [-Inf; -Inf; 0; -Inf; 1];
ub = [ Inf; Inf; 20];
Aeq = [1 -0.3 0 0 0];
beq = 0;
A = [0 0 0 -1 0.1
0 0 0 1 -0.5
0 0 -1 0 0.9];
b = [0; 0; 0];
[x,fval,exitflag]=fmincon(@myobj,x0,A,b,Aeq,beq,lb,ub,...
@myconstr)
%---------------------------------------------------------
function f = myobj(x)
f = 6*x(2)*x(5) + 7*x(1)*x(3) + 3*x(2)^2;
%---------------------------------------------------------
function [c, ceq] = myconstr(x)
c = [x(1) - 0.2*x(2)*x(5) - 71
0.9*x(3) - x(4)^2 - 67];
ceq = 3*x(2)^2*x(5) + 3*x(1)^2*x(3) - 20.875;Calling fullexample produces the following display in the command window:
fullexample
Warning: Trust-region-reflective method does not currently solve this type of problem,
using active-set (line search) instead.
> In fmincon at 439
In fullexample at 12
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 were satisfied to within the default value of the constraint tolerance.
Active inequalities (to within options.TolCon = 1e-006):
lower upper ineqlin ineqnonlin
3
x =
0.6114
2.0380
1.3948
0.3587
1.5498
fval =
37.3806
exitflag =
1
![]() | Writing Objective Functions | Passing Extra Parameters | ![]() |

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 |