Main Content

OptimizationExpression

Arithmetic or functional expression in terms of optimization variables

Description

AnOptimizationExpressionis an arithmetic or functional expression in terms of optimization variables. Use anOptimizationExpressionas an objective function, or as a part of an inequality or equality in a constraint or equation.

Creation

Create an optimization expression by performing operations onOptimizationVariableobjects. Use standard MATLAB®arithmetic including taking powers, indexing, and concatenation of optimization variables to create expressions. SeeSupported Operations for Optimization Variables and ExpressionsandExamples.

You can also create an optimization expression from a MATLAB function applied to optimization variables by usingfcn2optimexpr. For examples, seeCreate Expression from Nonlinear FunctionandProblem-Based Nonlinear Optimization.

Create an empty optimization expression by usingoptimexpr. Typically, you then fill the expression in a loop. For examples, seeCreate Optimization Expression by Loopingand theoptimexprfunction reference page.

After you create an expression, use it as either an objective function, or as part of a constraint or equation. For examples, see thesolvefunction reference page.

Properties

expand all

Index names, specified as a cell array of strings or character vectors. For information on using index names, seeNamed Index for Optimization Variables.

Data Types:cell

This property is read-only.

Optimization variables in the object, specified as a structure ofOptimizationVariableobjects.

Data Types:struct

Object Functions

evaluate Evaluate optimization expression
show Display information about optimization object
write Save optimization object description

Examples

collapse all

Create optimization expressions by arithmetic operations on optimization variables.

x = optimvar('x',3,2); expr = sum(sum(x))
线性OptimizationExpression expr = x (1) + (2, 1) + x(3, 1) + x(1, 2) + x(2, 2) + x(3, 2)
f = [2,10,4]; w = f*x; show(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 Linear OptimizationExpression array with properties: IndexNames: {{} {}} Variables: [1x1 struct] containing 1 OptimizationVariable See expression formulation with show.

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 variablewthat is the first and third row ofx. Note thatwis 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 show. 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 Linear OptimizationExpression array with properties: IndexNames: {{} {}} Variables: [1x1 struct] containing 2 OptimizationVariables See expression formulation with show.

Useoptimexprto create an empty expression, then fill the expression in a loop.

y = optimvar('y',6,4); expr = optimexpr(3,2);fori = 1:3forj = 1:2 expr(i,j) = y(2*i,j) - y(i,2*j);endendshow(expr)
(1,1) (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)

Create an optimization expression corresponding to the objective function

f ( x ) = x 2 / 1 0 + exp ( - exp ( - x ) ) .

x = optimvar('x'); f = x^2/10 + exp(-exp(-x))
f = Nonlinear OptimizationExpression ((x.^2 ./ 10) + exp((-exp((-x)))))

Find the point that minimizesfunstarting from the pointx0 = 0.

x0 = struct('x',0); prob = optimproblem('Objective',f); [sol,fval] = solve(prob,x0)
Solving problem using fminunc. Local minimum found. Optimization completed because the size of the gradient is less than the value of the optimality tolerance.
sol =struct with fields:x: -0.9595
fval = 0.1656

Iffwere not a supported function, you would convert it usingfcn2optimexpr. SeeSupported Operations for Optimization Variables and ExpressionsandConvert Nonlinear Function to Optimization Expression.

f = @(x)x^2/10 + exp(-exp(-x)); fun = fcn2optimexpr(f,x)
fun = Nonlinear OptimizationExpression ((x.^2 ./ 10) + exp((-exp((-x)))))
prob = optimproblem('Objective',fun); [sol,fval] = solve(prob,x0)
Solving problem using fminunc. Local minimum found. Optimization completed because the size of the gradient is less than the value of the optimality tolerance.
sol =struct with fields:x: -0.9595
fval = 0.1656

Copyright 2018–2020 The MathWorks, Inc.

Create an optimization expression in two variables.

x = optimvar('x',3,2); y = optimvar('y',1,2); expr = sum(x,1) - 2*y;

Evaluate the expression at a point.

xmat = [3,-1; 0,1; 2,6]; sol.x = xmat; sol.y = [4,-3]; val = evaluate(expr,sol)
val =1×2-3 12

More About

expand all

Version History

Introduced in R2017b