# OptimizationExpression

Arithmetic or functional expression in terms of optimization variables

## Description

An `OptimizationExpression` is an arithmetic or functional expression in terms of optimization variables. Use an `OptimizationExpression` as an objective function, or as a part of an inequality or equality in a constraint or equation.

## Creation

Create an optimization expression by performing operations on `OptimizationVariable` objects. Use standard MATLAB® arithmetic including taking powers, indexing, and concatenation of optimization variables to create expressions. See Supported Operations on Optimization Variables and Expressions and Examples.

You can also create an optimization expression from a MATLAB function applied to optimization variables by using `fcn2optimexpr`. For examples, see Create Expression from Nonlinear Function and Problem-Based Nonlinear Optimization.

Create an empty optimization expression by using `optimexpr`. Typically, you then fill the expression in a loop. For examples, see Create Optimization Expression by Looping and the `optimexpr` function reference page.

After you create an expression, use it as either an objective function, or as part of a constraint or equation. For examples, see the `solve` function reference page.

## Properties

expand all

Index names, specified as a cell array of strings or character vectors. For information on using index names, see Named Index for Optimization Variables.

Data Types: `cell`

Optimization variables in the object, specified as a structure of `OptimizationVariable` objects.

Data Types: `struct`

## Object Functions

 `evaluate` Evaluate optimization expression `show` Display information about optimization object `write` Save optimization object description

## Examples

collapse all

Create optimization expressions by arithmetic operations on optimization variables.

```x = optimvar('x',3,2); expr = sum(sum(x))```
```expr = Linear OptimizationExpression x(1, 1) + x(2, 1) + x(3, 1) + x(1, 2) + x(2, 2) + x(3, 2) ```
```f = [2,10,4]; w = f*x; show(w)```
```(1, 1) 2*x(1, 1) + 10*x(2, 1) + 4*x(3, 1) (1, 2) 2*x(1, 2) + 10*x(2, 2) + 4*x(3, 2) ```

Create an optimization expression by transposing an optimization variable.

```x = optimvar('x',3,2); y = x'```
```y = 2x3 Linear OptimizationExpression array with properties: IndexNames: {{} {}} Variables: [1x1 struct] containing 1 OptimizationVariable See expression formulation with show. ```

Simply indexing into an optimization array does not create an expression, but instead creates an optimization variable that references the original variable. To see this, create a variable `w` that is the first and third row of `x`. Note that `w` is an optimization variable, not an optimization expression.

`w = x([1,3],:)`
```w = 2x2 OptimizationVariable array with properties: Read-only array-wide properties: Name: 'x' Type: 'continuous' IndexNames: {{} {}} Elementwise properties: LowerBound: [2x2 double] UpperBound: [2x2 double] Reference to a subset of OptimizationVariable with Name 'x'. See variables with show. See bounds with showbounds. ```

Create an optimization expression by concatenating optimization variables.

```y = optimvar('y',4,3); z = optimvar('z',4,7); f = [y,z]```
```f = 4x10 Linear OptimizationExpression array with properties: IndexNames: {{} {}} Variables: [1x1 struct] containing 2 OptimizationVariables See expression formulation with show. ```

Use `optimexpr` to create an empty expression, then fill the expression in a loop.

```y = optimvar('y',6,4); expr = optimexpr(3,2); for i = 1:3 for j = 1:2 expr(i,j) = y(2*i,j) - y(i,2*j); end end show(expr)```
```(1, 1) y(2, 1) - y(1, 2) (2, 1) y(4, 1) - y(2, 2) (3, 1) y(6, 1) - y(3, 2) (1, 2) y(2, 2) - y(1, 4) (2, 2) y(4, 2) - y(2, 4) (3, 2) y(6, 2) - y(3, 4) ```

Create an optimization expression corresponding to the objective function

`$f\left(x\right)={x}^{2}/10+\mathrm{exp}\left(-\mathrm{exp}\left(-x\right)\right).$`

```x = optimvar('x'); f = x^2/10 + exp(-exp(-x))```
```f = Nonlinear OptimizationExpression ((x.^2 ./ 10) + exp(-exp(-x))) ```

Find the point that minimizes `fun` starting from the point `x0 = 0`.

```x0 = struct('x',0); prob = optimproblem('Objective',f); [sol,fval] = solve(prob,x0)```
```Solving problem using fminunc. Local minimum found. Optimization completed because the size of the gradient is less than the value of the optimality tolerance. ```
```sol = struct with fields: x: -0.9595 ```
```fval = 0.1656 ```

If `f` were not a supported function, you would convert it using `fcn2optimexpr`. See Supported Operations on Optimization Variables and Expressions and Convert Nonlinear Function to Optimization Expression.

```f = @(x)x^2/10 + exp(-exp(-x)); fun = fcn2optimexpr(f,x)```
```fun = Nonlinear OptimizationExpression anonymousFunction1(x) where: anonymousFunction1 = @(x)x^2/10+exp(-exp(-x)); ```
```prob = optimproblem('Objective',fun); [sol,fval] = solve(prob,x0)```
```Solving problem using fminunc. Local minimum found. Optimization completed because the size of the gradient is less than the value of the optimality tolerance. ```
```sol = struct with fields: x: -0.9595 ```
```fval = 0.1656 ```

Create an optimization expression in two variables.

```x = optimvar('x',3,2); y = optimvar('y',1,2); expr = sum(x,1) - 2*y;```

Evaluate the expression at a point.

```xmat = [3,-1; 0,1; 2,6]; sol.x = xmat; sol.y = [4,-3]; val = evaluate(expr,sol)```
```val = 1×2 -3 12 ```

expand all