Supported Operations for Optimization Variables and Expressions - MATLAB & Simulink (original) (raw)

Notation for Supported Operations

Optimization variables and expressions are the basic elements of the Problem-Based Optimization Workflow. For the legal operations on optimization variables and expressions:

Warning

The problem-based approach does not support complex values in the following: an objective function, nonlinear equalities, and nonlinear inequalities. If a function calculation has a complex value, even as an intermediate value, the final result might be incorrect.

Operations Returning Optimization Expressions

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 orx*a or x.*a
Divide by a constant scalar x/a or x./a ora\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 x2D*y2D, or x*y whenx or y is scalar
Matrix multiply variable and matrix M*x2D or x2D*M
Dot product of variable and array dot(x,c) ordot(c,x)
Linear combination of variables sum(x), sum(x,dim) wheredim can be a scalar or vector,sum(x,'all'), mean(x),mean(x,dim) where dim can be a scalar or vector, and mean(x,'all')
Product of array elements prod(x), prod(x,dim), andprod(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), andcumprod(_,direction)
Differences diff(x), including the syntaxesdiff(x,n) anddiff(x,n,dim)
Concatenate and Reshape Transpose x' or x.'
Concatenate cat, vertcat, andhorzcat
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 syntaxdiag(x2D,k)
Elementary Functions Power of square matrix x2D^a
Pointwise power x.^a
Square root sqrt(x)
Norm (Euclidean) norm(x) for a scalar or vector x, which calculatessqrt(sum(x.^2)). For non-vectorx or for other norm types,norm(x) returns the black-box expressionfcn2optimexpr(@norm,x,Analysis="off")
Sine sin(x)
Cosine cos(x)
Secant sec(x)
Cosecant csc(x)
Tangent tan(x)
Cotangent cot(x)
Arcsine asin(x)
Arccosine acos(x)
Arcsecant asec(x)
Arccosecant acsc(x)
Arctangent atan(x)
Arccotangent acot(x)
Exponential exp(x)
Logarithm log(x)
Hyperbolic sine sinh(x)
Hyperbolic cosine cosh(x)
Hyperbolic secant sech(x)
Hyperbolic cosecant csch(x)
Hyperbolic tangent tanh(x)
Hyperbolic cotangent coth(x)
Inverse hyperbolic sine asinh(x)
Inverse hyperbolic cosine acosh(x)
Inverse hyperbolic secant asech(x)
Inverse hyperbolic cosecant acsch(x)
Inverse hyperbolic tangent atanh(x)
Inverse hyperbolic cotangent acoth(x)

Beginning in R2024a, optimization variables and expressions support the"like" syntax. For a list of functions that support this capability, see Class Support for Array-Creation Functions. For an example showing how to use this syntax to initialize an optimization expression, see Initialize Optimization Expressions.

Note

a^x is not supported for an optimization variablex.

However, if you bound a to be strictly positive, you can use the equivalent exp(x*log(a)).

Operations Returning Optimization Variables

These operations on optimization variables return an optimization variable.

Operation Example
N-D numeric indexing (includes colon andend) 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 andend) x(17:end)
Linear logical indexing x(ind)
Linear string indexing x(str1)

Operations on Optimization Expressions

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.

Operations Returning Constraint Expressions

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.

Some Undocumented Operations Work on Optimization Variables and Expressions

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 callsreshape, which is a documented supported operation. So if yousqueeze an optimization variable then you can obtain a sensible expression.

Unsupported Functions and Operations Require 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 thefcn2optimexpr function reference page.

See Also

OptimizationExpression | OptimizationVariable | fcn2optimexpr

Topics