## 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`

.

**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.

### 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` 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 | `x2D*y2D` , or `x*y` when
`x` or `y` is scalar | |

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` ) | |

Norm (Euclidean) | `norm` (`x` ), which calculates
`sqrt(sum(x.^2,'all'))` | |

Sine | `sin` (`x` ) | |

Cosine | `cos` (`x` ) | |

Secant | `sec` (`x` ) | |

Cosecant | `csc` (`x` ) | |

Tangent | `tan` (`x` ) | |

Arcsine | `asin` (`x` ) | |

Arccosine | `acos` (`x` ) | |

Arcsecant | `asec` (`x` ) | |

Arccosecant | `acsc` (`x` ) | |

Arctangent | `atan` (`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` ) | |

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` ) |

**Note**

`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.

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)` |

### 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

`OptimizationExpression`

| `OptimizationVariable`

| `fcn2optimexpr`