evaluate
计算优化表达式
说明
使用 evaluate
求在某点处优化表达式的数值。
提示
有关完整的工作流,请参阅基于问题的优化工作流或基于问题的方程求解工作流。
示例
计算在某点处优化表达式的值
用两个变量创建一个优化表达式。
x = optimvar('x',3,2); y = optimvar('y',1,2); expr = sum(x,1) - 2*y;
计算该表达式在某点处的值。
xmat = [3,-1; 0,1; 2,6]; sol.x = xmat; sol.y = [4,-3]; val = evaluate(expr,sol)
val = 1×2
-3 12
计算目标函数在解处的值
求解线性规划问题。
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
求目标函数在解处的值。
val = evaluate(prob.Objective,sol)
val = -1.1111
输入参数
expr
— 优化表达式
OptimizationExpression
对象
优化表达式,指定为 OptimizationExpression
对象。
示例: expr = 5*x+3
,其中 x
是 OptimizationVariable
pt
— 表达式中变量的值
结构体
表达式中变量的值,指定为结构体。结构体 pt
具有以下要求:
expr
中的所有变量都与pt
中的字段名称匹配。匹配字段名称的值是数值。
例如,pt
可以是优化问题的解,由 solve
返回。
示例: pt.x = 3, pt.y = -5
数据类型: struct
输出参数
val
— 表达式的数值
双精度值
表达式的数值,以双精度值返回。
警告
基于问题的方法不支持目标函数、非线性等式或非线性不等式中使用复数值。如果某函数计算具有复数值,即使是作为中间值,最终结果也可能不正确。
版本历史记录
在 R2017b 中推出
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)