Optimization variables and expressions are the basic elements of the Problem-Based Optimization Workflow. For the legal operations on optimization variables and expressions:
x
and y
represent optimization
arrays of arbitrary size (usually the same size).
x2D
represents a 2-D optimization array.
a
is a scalar numeric constant.
M
is a constant numeric matrix.
c
is a numeric array of the same size as
x
.
Warning
The problem-based approach does not support complex values in an objective function, nonlinear equalities, or nonlinear inequalities. If a function calculation has a complex value, even as an intermediate value, the final result can be incorrect.
These operations on optimization variables or expressions return an optimization expression.
Category | Operation | Example |
---|---|---|
Arithmetic | Add constant | x+c or c+x |
Add variable | x+y | |
Unary plus | +x | |
Subtract a constant | x-c | |
Subtract variables | x–y | |
Unary minus | -x | |
Multiply by a constant scalar | a*x or a.*x or
x*a or x.*a | |
Divide by a constant scalar | x/a or x./a or
a\x or a.\x | |
Pointwise multiply by an array | c.*x or x.*c | |
Pointwise divide by an array | x./c or c.\x | |
Pointwise multiply variables | x.*y | |
Matrix multiply variables | x*y | |
Matrix multiply variable and matrix | M*x2D or x2D*M | |
Dot product of variable and array | dot(x,c) or
dot(c,x) | |
Linear combination of variables | sum(x) , sum(x,dim) ,
sum(x,'all') , mean(x) , and
mean(x,dim) | |
Product of array elements | prod(x) , prod(x,dim) , and
prod(x,'all') | |
Trace of matrix | trace(x2D) | |
Cumulative sum or product | cumsum(x) or cumprod(x) ,
including the syntaxes cumsum(x,dim) ,
cumsum(_,direction) ,
cumprod(x,dim) , and
cumprod(_,direction) | |
Differences | diff(x) , including the syntaxes
diff(x,n) and
diff(x,n,dim) | |
Concatenate and Reshape | Transpose | x' or x.' |
Concatenate | cat , vertcat , and
horzcat | |
Reshape | reshape(x,[10 1]) | |
Create diagonal matrix or get diagonal elements of matrix | diag(x2D) , where x2D is a
matrix or vector, including the syntax
diag(x2D,k) | |
Elementary Functions | Power of square matrix | x2D^a |
Pointwise power | x.^a | |
Square root | sqrt (x ) | |
Sine | sin (x ) | |
Cosine | cos (x ) | |
Tangent | tan (x ) | |
Arcsine | asin (x ) | |
Arccosine | acos (x ) | |
Arctangent | atan (x ) | |
Exponential | exp (x ) | |
Logarithm | log (x ) | |
Hyperbolic sine | sinh (x ) | |
Hyperbolic cosine | cosh (x ) | |
Hyperbolic tangent | tanh (x ) | |
Inverse hyperbolic sine | asinh (x ) | |
Inverse hyperbolic cosine | acosh (x ) | |
Inverse hyperbolic tangent | atanh (x ) |
These operations on optimization variables return an optimization variable.
Operation | Example |
---|---|
N-D numeric indexing (includes colon and
end ) | x(3,5:end) |
N-D logical indexing | x(ind) , where ind is a
logical array |
N-D string indexing | x(str1,str2) , where str1
and str2 are strings |
N-D mixed indexing (combination of numeric, logical, colon, end, and string) | x(ind,str1,:) |
Linear numeric indexing (includes colon and
end ) | x(17:end) |
Linear logical indexing | x(ind) |
Linear string indexing | x(str1) |
Optimization expressions support all the operations that optimization variables
support, and return optimization expressions. Also, you can index into or assign
into an optimization expression using numeric, logical, string, or linear indexing,
including the colon and end
operators for numeric or linear
indexing.
Constraints are any two comparable expressions that include
one of these comparison operators: ==
, <=
,
or >=
. Comparable expressions have the same size, or one of the
expressions must be scalar, meaning of size 1-by-1. For examples, see Expressions for Constraints and Equations.
Internally, some functions and operations call only the documented supported
operations. In these cases you can obtain sensible results from the functions or
operations. For example, currently squeeze
internally calls
reshape
, which is a documented supported operation. So if you
squeeze
an optimization variable then you can obtain a
sensible expression.
fcn2optimexpr
If your objective function or nonlinear constraint functions are not supported,
convert a MATLAB® function to an optimization expression by using fcn2optimexpr
. For examples, see Convert Nonlinear Function to Optimization Expression or the
fcn2optimexpr
function reference page.
fcn2optimexpr
| OptimizationExpression
| OptimizationVariable