optimconstr
Create empty optimization constraint array
Syntax
Description
Use optimconstr to initialize a set of constraint
expressions.
Tip
For the full workflow, see Problem-Based Optimization Workflow.
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.
Examples
Input Arguments
Output Arguments
Limitations
Each constraint expression in a problem must use the same comparison. For example, the following code leads to an error, because
cons1uses the<=comparison,cons2uses the>=comparison, andcons1andcons2are 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;
Tips
It is generally more efficient to create constraints by vectorized expressions rather than loops. See Create Efficient Optimization Problems.
You can use
optimineqinstead ofoptimconstrto create inequality expressions. Similarly, you can useoptimeqinstead ofoptimconstrto create equality expressions.
Version History
Introduced in R2017b