Main Content

# optimvar

Create optimization variables

## Syntax

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

## Description

Use `optimvar` to create optimization variables.

example

````x = optimvar(name)` creates a scalar optimization variable. An optimization variable is a symbolic object that enables you to create expressions for the objective function and the problem constraints in terms of the variable. TipTo avoid confusion, set `name` to be the MATLAB® variable name. For example,metal = optimvar('metal') ```

example

````x = optimvar(name,n)` creates an `n`-by-1 vector of optimization variables.```

example

````x = optimvar(name,cstr)` creates a vector of optimization variables that can use `cstr` for indexing. The number of elements of `x` is the same as the length of the `cstr` vector. The orientation of `x` is the same as the orientation of `cstr`: `x` is a row vector when `cstr` is a row vector, and `x` is a column vector when `cstr` is a column vector.```

example

````x = optimvar(name,cstr1,n2,...,cstrk)` or `x = optimvar(name,{cstr1,cstr2,...,cstrk})` or `x = optimvar(name,[n1,n2,...,nk])`, for any combination of positive integers `n`j and names `cstr`k, creates an array of optimization variables with dimensions equal to the integers `n`j and the lengths of the entries `cstr1`k.```

example

````x = optimvar(___,Name,Value)`, for any previous syntax, uses additional options specified by one or more `Name,Value` pair arguments. For example, to specify an integer variable, use ```x = optimvar('x','Type','integer')```.```

## Examples

collapse all

Create a scalar optimization variable named `dollars`.

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

Create a 3-by-1 optimization variable vector named `x`.

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

Create an integer optimization variable vector named `bolts` that is indexed by the strings `"brass"`, `"stainless"`, and `"galvanized"`. Use the indices of `bolts` to create an optimization expression, and experiment with creating `bolts` using character arrays or in a different orientation.

Create `bolts` using strings in a row orientation.

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

Create an optimization expression using the string indices.

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

Use a cell array of character vectors instead of strings to get a variable with the same indices as before.

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

Use a column-oriented version of `bnames`, 3-by-1 instead of 1-by-3, and observe that `bolts` has that orientation as well.

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

Create a 3-by-4-by-2 array of optimization variables named `xarray`.

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

You can also create multidimensional variables indexed by a mixture of names and numeric indices. For example, create a 3-by-4 array of optimization variables where the first dimension is indexed by the strings `'brass'`, `'stainless'`, and `'galvanized'`, and the second dimension is numerically indexed.

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

Create an optimization variable named `x` of size 3-by-3-by-3 that represents binary variables.

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

## Input Arguments

collapse all

Variable name, specified as a character vector or string.

Tip

To avoid confusion about which name relates to which aspect of a variable, set the workspace variable name to the variable name. For example,

`truck = optimvar('truck');`

Example: `"Warehouse"`

Example: `'truck'`

Data Types: `char` | `string`

Variable dimension, specified as a positive integer.

Example: `4`

Data Types: `double`

Index names, specified as a string array or a cell array of character arrays.

Example: ```x = optimvar('x',["Warehouse","Truck","City"])```

Example: ```x = optimvar('x',{'Warehouse','Truck','City'})```

Data Types: `string` | `cell`

### Name-Value Pair Arguments

Specify optional comma-separated pairs of `Name,Value` arguments. `Name` is the argument name and `Value` is the corresponding value. `Name` must appear inside quotes. You can specify several name and value pair arguments in any order as `Name1,Value1,...,NameN,ValueN`.

Example: Create `x` as a 3-element nonnegative vector with `x(2) <= 2` and `x(3) <= 4` by the command ```x = optimvar('x',3,'LowerBound',0,'UpperBound',[Inf,2,4])```

Variable type, specified as `'continuous'` or `'integer'`.

• `'continuous'` – Real values

• `'integer'` – Integer values

The variable type applies to all variables in the array. To have multiple variable types, create multiple variables.

Tip

To specify binary variables, use the `'integer'` type with `LowerBound` equal to `0` and `UpperBound` equal to `1`.

Example: `'integer'`

Lower bounds, specified as an array of the same size as `x` or as a real scalar. If `LowerBound` is a scalar, the value applies to all elements of `x`.

Example: To set a lower bound of `0` to all elements of `x`, specify the scalar value `0`.

Data Types: `double`

Upper bounds, specified as an array of the same size as `x` or as a real scalar. If `UpperBound` is a scalar, the value applies to all elements of `x`.

Example: To set an upper bound of `2` to all elements of `x`, specify the scalar value `2`.

Data Types: `double`

## Output Arguments

collapse all

Optimization variable, returned as an `OptimizationVariable` array. The dimensions of the array are the same as those of the corresponding input variables, such as `cstr``1`-by-`cstr2`.

## Tips

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

## See Also

Introduced in R2017b

Download now