如何使用 fminsearch 查找局部最大值?

How to use fminsearch to find local maximum?

我想使用 fminsearch 来查找函数的局部最大值。

是否可以使用 fminsearch 和 "just" 搜索函数的负 return 值来找到局部最大值。

例如:

function f = myfun(x,a)
f = x(1)^2 + a*x(2)^2;
a = 1.5;
x = fminsearch(@(x) -1 * myfun(x,a),[0,1]);

可能吗?

Update1: 为了详细说明我的问题并使其更清楚(根据下面的一些评论)-我添加了此更新:

通过询问是否可以这样做,我的意思是它是否正确使用了 fminsearch 函数 - 它是使用 fminsearch 查找最大值的预期用途吗?

Update2: 对于曾经关心过相同问题的人 - 除了下面的正确答案外,这里还有来自 https://www.mathworks.com/help/matlab/math/optimizing-nonlinear-functions.html#bsgpq6p-10

的文档

Maximizing Functions The fminbnd and fminsearch solvers attempt to minimize an objective function. If you have a maximization problem, that is, a problem of the form

max x f(x), then define g(x) = –f(x), and minimize g.

For example, to find the maximum of tan(cos(x)) near x = 5, evaluate:

[x fval] = fminbnd(@(x)-tan(cos(x)),3,8)

x = 6.2832

fval = -1.5574

The maximum is 1.5574 (the negative of the reported fval), and occurs at x = 6.2832. This answer is correct since, to five digits, the maximum is tan(1) = 1.5574, which occurs at x = 2π = 6.2832.

是的,这也是没有 fmaxsearch 函数的原因:

例如:

func = @(x) sin(x);

sol = fminsearch(@(x) func(x),0)
% sol = pi/2
sol = fminsearch(@(x) func(x)*-1,0)
% sol = -pi/2