Main Content

对目标进行最大化

所有求解器都会尝试最小化目标函数。如果您有最大化问题,即以下形式的问题:

maxxf(x),

然后定义 g(x) = f(x) 并对 g 取最小值。

例如,要计算 tan(cos(x))x = 5 附近的最大值,请计算

[x,fval] = fminunc(@(x)-tan(cos(x)),5)
Local minimum found.

Optimization completed because the size of the gradient is less than
the value of the optimality tolerance.
x = 6.2832
fval = -1.5574

最大值为 1.5574(报告的 fval 的负值),并出现在 x = 6.2832。此答案是正确的,因为取五位数时,最大值为 tan(1) = 1.5574,该值出现在 x = 2π = 6.2832 处。