Create empty optimization constraint array
constr = optimconstr(N)constr = optimconstr(cstr)constr = optimconstr(cstr1,N2,...,cstrk)constr = optimconstr({cstr1,cstr2,...,cstrk})constr = optimconstr([N1,N2,...,Nk])
or
constr = optimconstr(cstr1,N2,...,cstrk)
or
constr = optimconstr({cstr1,cstr2,...,cstrk}),
for any combination of constr = optimconstr([N1,N2,...,Nk])cstr and N arguments,
creates an
ncstr1-by-N2-by-...-by-ncstrk
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.
OptimizationConstraint | OptimizationExpression | OptimizationProblem | OptimizationVariable | optimexpr