evaluate

Evaluate optimization expression

Syntax

val = evaluate(expr,pt)

Description

example

val = evaluate(expr,pt) returns the value of the optimization expression expr at the value pt.

Examples

collapse all

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

Solve a linear programming problem.

x = optimvar('x');
y = optimvar('y');
prob = optimproblem;
prob.Objective = -x -y/3;
prob.Constraints.cons1 = x + y <= 2;
prob.Constraints.cons2 = x + y/4 <= 1;
prob.Constraints.cons3 = x - y <= 2;
prob.Constraints.cons4 = x/4 + y >= -1;
prob.Constraints.cons5 = x + y >= 1;
prob.Constraints.cons6 = -x + y <= 2;

sol = solve(prob)
Optimal solution found.
sol = struct with fields:
    x: 0.6667
    y: 1.3333

Find the value of the objective function at the solution.

val = evaluate(prob.Objective,sol)
val = -1.1111

Input Arguments

collapse all

Optimization expression, specified as an OptimizationExpression object.

Example: expr = 5*x+3, where x is an OptimizationVariable

Values of variables in expression, specified as a structure. The structure pt has the following requirements:

  • All variables in expr match field names in pt.

  • The values of the matching field names are numeric.

For example, pt can be the solution to an optimization problem, as returned by solve.

Example: pt.x = 3, pt.y = -5

Data Types: struct

Output Arguments

collapse all

Numeric value of expression, returned as a double.

Introduced in R2017b