Create empty optimization constraint array
Use optimconstr
to initialize a set of constraint
expressions.
Tip
For the full workflow, see Problem-Based Optimization Workflow.
or
constr
= optimconstr(cstr
1,N
2,...,cstr
k)
or
constr
= optimconstr({cstr
1,cstr
2,...,cstr
k})
,
for any combination of constr
= optimconstr([N
1,N
2,...,N
k])cstr
and N
arguments,
creates an
ncstr
1-by-N
2-by-...-by-ncstr
k
array of empty optimization constraints, where
ncstr
is the number of elements in
cstr
.
Each constraint expression in a problem must use the same comparison. For example, the
following code leads to an error, because cons1
uses the
<=
comparison, cons2
uses the
>=
comparison, and cons1
and
cons2
are in the same expression.
prob = optimproblem; x = optimvar('x',2,'LowerBound',0); cons1 = x(1) + x(2) <= 10; cons2 = 3*x(1) + 4*x(2) >= 2; prob.Constraints = [cons1;cons2]; % This line throws an error
You can avoid this error by using separate expressions for the constraints.
prob.Constraints.cons1 = cons1; prob.Constraints.cons2 = cons2;
It is generally more efficient to create constraints by vectorized expressions rather than loops. See Create Efficient Optimization Problems.
You can use optimineq
instead of optimconstr
to create
inequality expressions. Similarly, you can use optimeq
instead of optimconstr
to create
equality expressions.
optimeq
| optimexpr
| optimineq
| OptimizationConstraint
| OptimizationExpression
| OptimizationProblem
| OptimizationVariable