Main Content

Supported Operations for Optimization Variables and Expressions

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:

  • x and y represent optimization arrays of arbitrary size (usually the same size).

  • x2D and y2D represent 2-D optimization arrays.

  • a is a scalar numeric constant.

  • M is a constant numeric matrix.

  • c is a numeric array of the same size as x.


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.

ArithmeticAdd constantx+c or c+x
Add variablex+y
Unary plus+x
Subtract a constantx-c
Subtract variablesx–y
Unary minus-x
Multiply by a constant scalara*x or a.*x or x*a or x.*a
Divide by a constant scalarx/a or x./a or a\x or a.\x
Pointwise multiply by an arrayc.*x or x.*c
Pointwise divide by an arrayx./c or c.\x
Pointwise multiply variablesx.*y
Matrix multiply variablesx2D*y2D, or x*y when x or y is scalar
Matrix multiply variable and matrixM*x2D or x2D*M
Dot product of variable and arraydot(x,c) or dot(c,x)
Linear combination of variablessum(x), sum(x,dim) where dim 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 elementsprod(x), prod(x,dim), and prod(x,'all')
Trace of matrixtrace(x2D)
Cumulative sum or productcumsum(x) or cumprod(x), including the syntaxes cumsum(x,dim), cumsum(_,direction), cumprod(x,dim), and cumprod(_,direction)
Differencesdiff(x), including the syntaxes diff(x,n) and diff(x,n,dim)
Concatenate and ReshapeTransposex' or x.'
Concatenatecat, vertcat, and horzcat
Reshapereshape(x,[10 1])
Create diagonal matrix or get diagonal elements of matrixdiag(x2D), where x2D is a matrix or vector, including the syntax diag(x2D,k)
Elementary FunctionsPower of square matrixx2D^a
Pointwise powerx.^a
Square rootsqrt(x)
Norm (Euclidean)

norm(x) for a scalar or vector x, which calculates sqrt(sum(x.^2)). For non-vector x or for other norm types, norm(x) returns the black-box expression

Hyperbolic sinesinh(x)
Hyperbolic cosinecosh(x)
Hyperbolic secantsech(x)
Hyperbolic cosecantcsch(x)
Hyperbolic tangenttanh(x)
Inverse hyperbolic sineasinh(x)
Inverse hyperbolic cosineacosh(x)
Inverse hyperbolic secantasech(x)
Inverse hyperbolic cosecantacsch(x)
Inverse hyperbolic tangentatanh(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.


a^x is not supported for an optimization variable x.

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.

N-D numeric indexing (includes colon and end)x(3,5:end)
N-D logical indexingx(ind), where ind is a logical array
N-D string indexingx(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 indexingx(ind)
Linear string indexingx(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 calls reshape, which is a documented supported operation. So if you squeeze 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 the fcn2optimexpr function reference page.

See Also

| |

Related Topics