Documentation

# OptimizationExpression

Objective function or constraints

## Description

An `OptimizationExpression` is an arithmetic expression in terms of optimization variables for an objective function or for comparison in constraints.

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

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

This property is read-only.

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

Data Types: `struct`

## Object Functions

 `evaluate` Evaluate optimization expression `showexpr` Display optimization expression `writeexpr` Save optimization expression 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; showexpr(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 showexpr. ```

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

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 showexpr(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)x^2/10 + exp(-exp(-x)); fun = fcn2optimexpr(f,x)```
```fun = Nonlinear OptimizationExpression anonymousFunction1(x) where: anonymousFunction1 = @(x)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',fun); [sol,fval] = solve(prob,x0)```
```Local minimum found. Optimization completed because the size of the gradient is less than the value of the optimality tolerance. <stopping criteria details> ```
```sol = struct with fields: x: -0.9595 ```
```fval = 0.1656 ```

expand all

Watch now