Main Content

本页对应的英文页面已更新,但尚未翻译。 若要查看最新内容,请点击此处访问英文页面。

evaluate

Evaluate optimization expression

说明

示例

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

示例

全部折叠

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)
Solving problem using linprog.

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

输入参数

全部折叠

Optimization expression, specified as an OptimizationExpression object.

示例: 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.

示例: pt.x = 3, pt.y = -5

数据类型: struct

Output Arguments

全部折叠

Numeric value of expression, returned as a double.

警告

The problem-based approach does not support complex values in an objective function, nonlinear equalities, or nonlinear inequalities. If a function calculation has a complex value, even as an intermediate value, the final result can be incorrect.

在 R2017b 中推出