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.

OptimizationExpression

Objective function or constraints

Description

An OptimizationExpression is an arithmetic expression in terms of optimization variables for an objective function or for comparison in constraints.

Creation

Create an optimization expression by performing linear operations on OptimizationVariable objects. Use standard MATLAB® arithmetic, indexing, and concatenation of optimization variables to create expressions. See Examples.

You can also create an empty optimization expression by using optimexpr. Typically, you then fill the expression in a loop. For examples, see the optimexpr function reference page.

Properties

expand all

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

Object Functions

evaluateEvaluate optimization expression
showexprDisplay optimization expression
writeexprSave optimization expression description

Examples

collapse all

Create optimization expressions by arithmetic operations on optimization variables.

x = optimvar('x',3,2);
expr = sum(sum(x))
expr = 
  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;
showexpr(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)

Create an optimization expression by transposing an optimization variable.

x = optimvar('x',3,2);
y = x'
y = 
  2x3 OptimizationExpression array with properties:

    IndexNames: {{}  {}}

  See expression formulation with showexpr.

Simply indexing into an optimization array does not create an expression, but instead creates an optimization variable that references the original variable. To see this, create a variable w that is the first and third row of x. Note that w is an optimization variable, not an optimization expression.

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

Create an optimization expression by concatenating optimization variables.

y = optimvar('y',4,3);
z = optimvar('z',4,7);
f = [y,z]
f = 
  4x10 OptimizationExpression array with properties:

    IndexNames: {{}  {}}

  See expression formulation with showexpr.

Use optimexpr to create an empty expression, then fill the expression in a loop.

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
showexpr(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)

More About

expand all

Introduced in R2017b