# optimvar

## 语法

``x = optimvar(name)``
``x = optimvar(name,n)``
``x = optimvar(name,cstr)``
``x = optimvar(name,cstr1,n2,...,cstrk)``
``x = optimvar(name,{cstr1,cstr2,...,cstrk})``
``x = optimvar(name,[n1,n2,...,nk])``
``x = optimvar(___,Name,Value)``

## 说明

````x = optimvar(name)` 创建标量优化变量。优化变量是符号对象，您可以使用它来为目标函数和问题约束创建表达式。 提示为避免混淆，请将 `name` 设置为 MATLAB® 变量名称。例如，metal = optimvar('metal') ```

``x = optimvar(name,n)` 创建由优化变量组成的 `n`×1 向量。`

``x = optimvar(name,cstr)` 创建可使用 `cstr` 进行索引的优化变量向量。`x` 的元素数与 `cstr` 向量的长度相同。`x` 的方向与 `cstr` 的方向相同：当 `cstr` 是行向量时，`x` 也是行向量，当 `cstr` 是列向量时，`x` 也是列向量。`

`对于正整数 `n`j 和名称 `cstr`k 的任意组合，`x = optimvar(name,cstr1,n2,...,cstrk)` 或 `x = optimvar(name,{cstr1,cstr2,...,cstrk})` 或 `x = optimvar(name,[n1,n2,...,nk])` 创建一个优化变量数组，其维数等于整数 `n`j 和条目 `cstr1`k 的长度。`

`对于前面的任意语法，`x = optimvar(___,Name,Value)` 使用由一个或多个 `Name,Value` 对组参量指定的其他选项。例如，要指定整数变量，请使用 `x = optimvar('x','Type','integer')`。`

## 示例

`dollars = optimvar('dollars')`
```dollars = OptimizationVariable with properties: Name: 'dollars' Type: 'continuous' IndexNames: {{} {}} LowerBound: -Inf UpperBound: Inf See variables with show. See bounds with showbounds. ```

`x = optimvar('x',3)`
```x = 3x1 OptimizationVariable array with properties: Array-wide properties: Name: 'x' Type: 'continuous' IndexNames: {{} {}} Elementwise properties: LowerBound: [3x1 double] UpperBound: [3x1 double] See variables with show. See bounds with showbounds. ```

```bnames = ["brass","stainless","galvanized"]; bolts = optimvar('bolts',bnames,'Type','integer')```
```bolts = 1x3 OptimizationVariable array with properties: Array-wide properties: Name: 'bolts' Type: 'integer' IndexNames: {{} {1x2 cell}} Elementwise properties: LowerBound: [-Inf -Inf -Inf] UpperBound: [Inf Inf Inf] See variables with show. See bounds with showbounds. ```

`y = bolts("brass") + 2*bolts("stainless") + 4*bolts("galvanized")`
```y = Linear OptimizationExpression bolts('brass') + 2*bolts('stainless') + 4*bolts('galvanized') ```

```bnames = {'brass','stainless','galvanized'}; bolts = optimvar('bolts',bnames,'Type','integer')```
```bolts = 1x3 OptimizationVariable array with properties: Array-wide properties: Name: 'bolts' Type: 'integer' IndexNames: {{} {1x2 cell}} Elementwise properties: LowerBound: [-Inf -Inf -Inf] UpperBound: [Inf Inf Inf] See variables with show. See bounds with showbounds. ```

```bnames = ["brass";"stainless";"galvanized"]; bolts = optimvar('bolts',bnames,'Type','integer')```
```bolts = 3x1 OptimizationVariable array with properties: Array-wide properties: Name: 'bolts' Type: 'integer' IndexNames: {{1x2 cell} {}} Elementwise properties: LowerBound: [3x1 double] UpperBound: [3x1 double] See variables with show. See bounds with showbounds. ```

`xarray = optimvar('xarray',3,4,2)`
```xarray = 3x4x2 OptimizationVariable array with properties: Array-wide properties: Name: 'xarray' Type: 'continuous' IndexNames: {{} {} {}} Elementwise properties: LowerBound: [3x4x2 double] UpperBound: [3x4x2 double] See variables with show. See bounds with showbounds. ```

```bnames = ["brass","stainless","galvanized"]; bolts = optimvar('bolts',bnames,4)```
```bolts = 3x4 OptimizationVariable array with properties: Array-wide properties: Name: 'bolts' Type: 'continuous' IndexNames: {{1x2 cell} {}} Elementwise properties: LowerBound: [3x4 double] UpperBound: [3x4 double] See variables with show. See bounds with showbounds. ```

`x = optimvar('x',3,3,3,'Type','integer','LowerBound',0,'UpperBound',1)`
```x = 3x3x3 OptimizationVariable array with properties: Array-wide properties: Name: 'x' Type: 'integer' IndexNames: {{} {} {}} Elementwise properties: LowerBound: [3x3x3 double] UpperBound: [3x3x3 double] See variables with show. See bounds with showbounds. ```

## 输入参数

`truck = optimvar('truck');`

`cstr` 不能为诸如 `"Tp"` 之类的字符串标量，而必须为诸如 `["Tp" "ul"]` 之类的向量。要指定单个名称，请使用 `{'Tp'}` 或等效的 `cellstr("Tp")`

### 名称-值参数

• `'continuous'` - 实数值

• `'integer'` - 整数值

## 提示

• `OptimizationVariable` 对象具有句柄复制行为。请参阅句柄对象行为句柄类和值类的比较。句柄复制行为意味着 `OptimizationVariable` 的副本指向原始变量，并且不独立存在。例如，创建变量 `x`，将其复制到 `y`，然后设置 `y` 的属性。请注意，`x` 采用新属性值。

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