This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

OptimizationVariable

Variable for optimization

Description

An OptimizationVariable object contains variables for optimization expressions. These expressions represent the objective function or constraints. Variables are symbolic in nature, and can be arrays of any size.

Creation

Create an OptimizationVariable object using optimvar.

Properties

expand all

Array-Wide Properties

This property is read-only.

Variable name, specified as a string or character vector.

Name gives the variable label to be displayed, such as in showbounds, showconstr, showexpr, showproblem, or showvar. Name also gives the field names in the solution structure that solve returns.

Tip

To avoid confusion, set name to be the MATLAB® variable name. For example,

metal = optimvar('metal')

Data Types: char | string

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 a binary variable, use the 'integer' type and specify LowerBound = 0 and UpperBound = 1.

Data Types: char | string

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

Element-wise Properties

Lower bound, specified as a real scalar or as a real array having the same dimensions as the OptimizationVariable object. Scalar values apply to all elements of the variable.

The LowerBound property is always displayed as an array. However, you can set the property as a scalar that applies to all elements. For example,

var.LowerBound = 0

Data Types: double

Upper bound, specified as a real scalar or as a real array having the same dimensions as the OptimizationVariable object. Scalar values apply to all elements of the variable.

The UpperBound property is always displayed as an array. However, you can set the property as a scalar that applies to all elements. For example

var.UpperBound = 1

Data Types: double

Object Functions

showboundsDisplay variable bounds
showvarDisplay optimization variable
writeboundsSave description of variable bounds
writevarSave optimization variable description

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 showvar.
  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 showvar.
  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 showvar.
  See bounds with showbounds.

Create an optimization expression using the string indices.

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

    bolts(1, 'brass') + 2*bolts(1, 'stainless') + 4*bolts(1, '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 showvar.
  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 showvar.
  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 showvar.
  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 showvar.
  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 showvar.
  See bounds with showbounds.

More About

expand all

Tips

  • OptimizationVariable objects have handle copy behavior. See Handle Object Behavior (MATLAB) and Comparison of Handle and Value Classes (MATLAB). 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

Introduced in R2017b