# OptimizationExpression

## 说明

`OptimizationExpression` 是关于优化变量的算术或函数表达式。将 `OptimizationExpression` 用作目标函数，或用作约束或方程中不等式或等式的一部分。

## 对象函数

 `evaluate` 计算优化表达式 `show` 显示有关优化对象的信息 `write` 保存优化对象描述

## 示例

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

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

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

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

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

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

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

```f = @(x)x^2/10 + exp(-exp(-x)); fun = fcn2optimexpr(f,x)```
```fun = Nonlinear OptimizationExpression ((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 ```

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

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