## Optimization Expressions

### What Are Optimization Expressions?

Optimization expressions are polynomial or rational combinations of optimization variables.

```x = optimvar('x',3,3); % a 3-by-3 variable named 'x' expr1 = sum(x,1) % add the columns of x, get a row vector expr2 = sum(x,2) % add the rows of x, get a column vector expr3 = sum(sum(x.*randn(3))) % add the elements of x multiplied by random numbers expr4 = randn(3)*x % multiply a random matrix times x expr5 = sum(sum(x*diag(1:3))) % multiply the columns of x by their column number and sum the result expr6 = sum(sum(x.*x)) % sum of the squares of all the variables```

Optimization expressions also result from many MATLAB® operations on optimization variables, such as transpose or concatenation of variables. For the list of supported operations on optimization expressions, see Supported Operations for Optimization Variables and Expressions.

Finally, optimization expressions can be the result of applying `fcn2optimexpr` to a MATLAB function acting on optimization variables. For details, see Convert Nonlinear Function to Optimization Expression.

Optimization modeling functions do not allow you to specify complex, `Inf`, or `NaN` values. If you obtain such an expression through operations, the expression cannot be displayed. See Expression Contains Inf or NaN.

### Expressions for Objective Functions

An objective function is an expression of size 1-by-1.

```y = optimvar('y',5,3); expr = sum(y,2); % a 5-by-1 vector expr2 = [1:5]*expr;```

The expression `expr` is not suitable for an objective function because it is a vector. The expression `expr2` is suitable for an objective function.

Note

If you have a nonlinear function that is not composed of polynomials, rational expressions, and elementary functions such as `exp`, then convert the function to an optimization expression by using `fcn2optimexpr`. See Convert Nonlinear Function to Optimization Expression and Supported Operations for Optimization Variables and Expressions.

To include an expression as an objective function in a problem, use dot notation, or include the objective when you create the problem.

```prob = optimproblem; prob.Objective = expr2; % or equivalently prob = optimproblem('Objective',expr2);```

To create an expression in a loop, start with an empty expression as returned by `optimexpr`.

```x = optimvar('x',3,3,'Type','integer','LowerBound',0,'UpperBound',1); y = optimvar('y',3,3); expr = optimexpr; for i = 1:3 for j = 1:3 expr = expr + y(j,i) - x(i,j); end end show(expr)```
``` y(1, 1) + y(2, 1) + y(3, 1) + y(1, 2) + y(2, 2) + y(3, 2) + y(1, 3) + y(2, 3) + y(3, 3) - x(1, 1) - x(2, 1) - x(3, 1) - x(1, 2) - x(2, 2) - x(3, 2) - x(1, 3) - x(2, 3) - x(3, 3)```

You can create `expr` without any loops:

```x = optimvar('x',3,3,'Type','integer','LowerBound',0,'UpperBound',1); y = optimvar('y',3,3); expr = sum(sum(y' - x)); show(expr)```
``` y(1, 1) + y(2, 1) + y(3, 1) + y(1, 2) + y(2, 2) + y(3, 2) + y(1, 3) + y(2, 3) + y(3, 3) - x(1, 1) - x(2, 1) - x(3, 1) - x(1, 2) - x(2, 2) - x(3, 2) - x(1, 3) - x(2, 3) - x(3, 3)```

Note

If your objective function is a sum of squares, and you want `solve` to recognize it as such, write it as `sum(expr.^2)`, and not as `expr'*expr`. The internal parser recognizes only explicit sums of squares. For an example, see Nonnegative Linear Least Squares, Problem-Based.

### Expressions for Constraints and Equations

Constraints are any two comparable expressions that include one of these comparison operators: `==`, `<=`, or `>=`. Equations are two comparable expressions that use the comparison operator `==`. Comparable expressions have the same size, or one of the expressions must be scalar, meaning of size 1-by-1.

```x = optimvar('x',3,2,'Type','integer','LowerBound',0,'UpperBound',1); y = optimvar('y',2,4); z = optimvar('z'); constr1 = sum(x,2) >= z;```

`x` is of size 3-by-2, so `sum(x,2)` is of size 3-by-1. This expression is comparable to `z` because `z` is a scalar variable.

`constr2 = y <= z;`

`y` is of size 2-by-4. Again, `y` is comparable to `z` because `z` is a scalar variable.

`constr3 = (sum(x,1))' <= sum(y,2);`

`sum(x,1)` is of size 1-by-2, so `(sum(x,1))'` is of size 2-by-1. `sum(y,2)` is of size 2-by-1, so the two expressions are comparable.

Note

If you have a nonlinear function that is not composed of polynomials, rational expressions, and elementary functions such as `exp`, then convert the function to an optimization expression by using `fcn2optimexpr`. See Convert Nonlinear Function to Optimization Expression and Supported Operations for Optimization Variables and Expressions.

To include constraints in a problem, use dot notation and give each constraint a different name.

```prob = optimproblem; prob.Constraints.constr1 = constr1; prob.Constraints.constr2 = constr2; prob.Constraints.constr3 = constr3;```

Similarly, to include equations in a problem, use dot notation and give each equation a different name.

```prob = eqnproblem; prob.Equations.eq1 = eq1; prob.Equations.eq2 = eq12```

You can also include constraints or equations when you create a problem. For example, suppose that you have 10 pairs of positive variables whose sums are no more than one.

```x = optimvar('x',10,2,'LowerBound',0); prob = optimproblem('Constraints',sum(x,2) <= 1);```

To create constraint or equation expressions in a loop, start with an empty constraint expression as returned by `optimconstr`, `optimeq`, or `optimineq`.

```x = optimvar('x',3,2,'Type','integer','LowerBound',0,'UpperBound',1); y = optimvar('y',2,4); z = optimvar('z'); const1 = optimconstr(2); for i = 1:2 const1(i) = x(1,i) - x(3,i) + 2*z >= 4*(y(i,2) + y(i,3) + 2*y(i,4)); end show(const1)```
```(1, 1) x(1, 1) - x(3, 1) + 2*z - 4*y(1, 2) - 4*y(1, 3) - 8*y(1, 4) >= 0 (2, 1) x(1, 2) - x(3, 2) + 2*z - 4*y(2, 2) - 4*y(2, 3) - 8*y(2, 4) >= 0```

You can create `const1` without any loops.

```x = optimvar('x',3,2,'Type','integer','LowerBound',0,'UpperBound',1); y = optimvar('y',2,4); z = optimvar('z'); const1 = x(1,:) - x(3,:) + 2*z >= 4*(y(:,1) + y(:,3) + 2*y(:,4))'; show(const1)```
```(1, 1) x(1, 1) - x(3, 1) + 2*z - 4*y(1, 1) - 4*y(1, 3) - 8*y(1, 4) >= 0 (1, 2) x(1, 2) - x(3, 2) + 2*z - 4*y(2, 1) - 4*y(2, 3) - 8*y(2, 4) >= 0```

Tip

For best performance, include variable bounds in the variable definitions, not in constraint expressions. Also, performance generally improves when you create constraints without using loops. See Create Efficient Optimization Problems.

Caution

Each constraint expression in a problem must use the same comparison. For example, the following code leads to an error, because `cons1` uses the `<=` comparison, `cons2` uses the `>=` comparison, and `cons1` and `cons2` are 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;```

### Optimization Variables Have Handle Behavior

• `OptimizationVariable` objects have handle copy behavior. See Handle Object Behavior and Comparison of Handle and Value Classes. Handle copy behavior means that a copy of an `OptimizationVariable` points to the original and does not have an independent existence. For example, create a variable `x`, copy it to `y`, then set a property of `y`. Note that `x` takes on the new property value.

```x = optimvar('x','LowerBound',1); y = x; y.LowerBound = 0; showbounds(x)```
` 0 <= x`