Linear Constraints
What Are Linear Constraints?
Several optimization solvers accept linear constraints, which are restrictions on
the solution x to satisfy linear equalities or inequalities.
Solvers that accept linear constraints include fmincon, intlinprog, linprog, lsqlin, quadprog, multiobjective solvers, and some Global Optimization Toolbox solvers.
Linear Inequality Constraints
Linear inequality constraints have the form A·x ≤ b. When A is m-by-n, there are m constraints on a variable x with n components. You supply the m-by-n matrix A and the m-component vector b.
Pass linear inequality constraints in the A and
b arguments.
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 constraints using the following matrix A and vector b:
Notice that the “greater than” inequalities are first multiplied by –1 to put them in “less than” inequality form. In MATLAB® syntax:
A = [1 0 1 0;
0 -2 1 0;
-1 1 -1 1];
b = [4;2;-9];You do not need to give gradients for linear constraints; solvers calculate them automatically. Linear constraints do not affect Hessians.
Even if you pass an initial point x0 as a matrix, solvers pass
the current point x as a column vector to linear constraints. See
Matrix Arguments.
For a more complex example of linear constraints, see Set Up a Linear Program, Solver-Based.
Intermediate iterations can violate linear constraints. See Iterations Can Violate Constraints.
Linear Range Constraints
Starting in R2026a, you can specify lower and upper linear inequalities by using a
two-element cell array for the linprog and intlinprog solvers. Passing A and
{bl,b} as the linear
constraints means the solver enforces
For example, consider the following inequalities.
| –2 ≤ x1 +
2x3 ≤
4, x2 – 3x3 ≥ –2, x1 – x2 + x3 – x4 ≤ 8. | (1) |
Specify the inequalities by entering these arrays.
A = [1 0 2 0;
0 1 -3 0;
1 -1 1 -1];
bl = [-2 -2 -inf];
b = [4 inf 8]; % Pass the range constraints as {bl,b}For each unspecified lower range, such as the third line of inequalities, you
specify -inf as the value of bl. Similarly,
for each unspecified upper range, such as the second line of inequalities, you
specify inf as the value of b.
Instead of using the cell array syntax, you can use the ≤ form
for each inequality. For example, the two inequalities below are equivalent.
| –2 ≤ x1 + 2x3 | (2) |
| –x1 – 2x3 ≤ 2. | (3) |
The syntax of range constraints is particularly convenient when you have both lower and upper limits on each linear expression, such as in the first line of inequalities.
| –2 ≤ x1 + 2x3 ≤ 4. | (4) |
To indicate that bl or b does not apply to a
problem, pass an empty array, such as {bl []}.
Linear Equality Constraints
Linear equalities have the form Aeq·x = beq, which represents m equations with n-component vector x. You supply the m-by-n matrix Aeq and the m-component vector beq.
Pass linear equality constraints in the Aeq and
beq arguments in the same way as described for the
A and b arguments in Linear Inequality Constraints.