optimexpr

Create empty optimization expression array

Syntax

expr = optimexpr(n)
expr = optimexpr(cstr)
expr = optimexpr(cstr1,n2,...,cstrk)
expr = optimexpr([n1,n2,...,nk])
expr = optimexpr({cstr1,cstr2,...,cstrk})

Description

example

expr = optimexpr(n) creates an empty n-by-1 OptimizationExpression array. Use expr as the initial value in a loop that creates optimization expressions.

example

expr = optimexpr(cstr) creates an empty OptimizationExpression array that can use the vector cstr for indexing. The number of elements of expr is the same as the length of cstr. When cstr is a row vector, then expr is a row vector. When cstr is a column vector, then expr is a column vector.

example

expr = optimexpr(cstr1,n2,...,cstrk) or expr = optimexpr([n1,n2,...,nk])or expr = optimexpr({cstr1,cstr2,...,cstrk}), for any combination of positive integers nj and names cstrj, creates an empty array of optimization expressions with dimensions equal to the integers nj or the lengths of the entries of cstrj.

Examples

collapse all

Create an empty array of three optimization expressions.

expr = optimexpr(3)
expr = 
  3x1 OptimizationExpression array with properties:

    IndexNames: {{}  {}}
     Variables: [1x1 struct] containing 0 OptimizationVariables

  See expression formulation with showexpr.

Create a string array of color names, and an optimization expression that is indexed by the color names.

strexp = ["red","green","blue","yellow"];
expr = optimexpr(strexp)
expr = 
  1x4 OptimizationExpression array with properties:

    IndexNames: {{}  {1x4 cell}}
     Variables: [1x1 struct] containing 0 OptimizationVariables

  See expression formulation with showexpr.

You can use a cell array of character vectors instead of strings to get the same effect.

strexp = {'red','green','blue','yellow'};
expr = optimexpr(strexp)
expr = 
  1x4 OptimizationExpression array with properties:

    IndexNames: {{}  {1x4 cell}}
     Variables: [1x1 struct] containing 0 OptimizationVariables

  See expression formulation with showexpr.

If strexp is 4-by-1 instead of 1-by-4, then expr is also 4-by-1:

strexp = ["red";"green";"blue";"yellow"];
expr = optimexpr(strexp)
expr = 
  4x1 OptimizationExpression array with properties:

    IndexNames: {{1x4 cell}  {}}
     Variables: [1x1 struct] containing 0 OptimizationVariables

  See expression formulation with showexpr.

Create an empty 3-by-4-by-2 array of optimization expressions.

expr = optimexpr(3,4,2)
expr = 
  3x4x2 OptimizationExpression array with properties:

    IndexNames: {{}  {}  {}}
     Variables: [1x1 struct] containing 0 OptimizationVariables

  See expression formulation with showexpr.

Create a 3-by-4 array of optimization expressions, where the first dimension is indexed by the strings "brass", "stainless", and "galvanized", and the second dimension is numerically indexed.

bnames = ["brass","stainless","galvanized"];
expr = optimexpr(bnames,4)
expr = 
  3x4 OptimizationExpression array with properties:

    IndexNames: {{1x3 cell}  {}}
     Variables: [1x1 struct] containing 0 OptimizationVariables

  See expression formulation with showexpr.

Create an expression using a named index indicating that each stainless expression is 1.5 times the corresponding x(galvanized) value.

x = optimvar('x',bnames,4);
expr('stainless',:) = x('galvanized',:)*1.5;
showexpr(expr('stainless',:))
('stainless', 1)

  1.5*x('galvanized', 1)

('stainless', 2)

  1.5*x('galvanized', 2)

('stainless', 3)

  1.5*x('galvanized', 3)

('stainless', 4)

  1.5*x('galvanized', 4)

Input Arguments

collapse all

Variable dimension, specified as a positive integer.

Example: 4

Data Types: double

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

Example: expr = optimexpr(["Warehouse","Truck","City"])

Example: expr = optimexpr({'Warehouse','Truck','City'})

Data Types: string | cell

Output Arguments

collapse all

Optimization expression, returned as an OptimizationExpression object.

Tips

  • You can use optimexpr to create empty expressions that you fill programmatically, such as in a for loop.

    x = optimvar('x',8);
    expr = optimexpr(4)
    for k = 1:4
        expr(k) = 5*k*(x(2*k) - x(2*k-1));
    end
  • It is generally more efficient to create expressions by vectorized statements rather than loops. See Create Efficient Optimization Problems.

Introduced in R2017b