I would like to use fminsearch in order to find the local maximum of a function.
Is it possible to find local maximum using fminsearch with "just" searching on the negative return value of the function.
for example:
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]);
Is it possible?
Update1: In order to elaborate my question and making it clearer (following some comments below) - I'm adding this update:
By asking if it's possible to do so, I meant is it a proper use of fminsearch
function - is it the intended use to find max using fminsearch
?
Update2: for who ever concern with the same question - In addition to the correct answer below , here is the documentation from 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.
Yes you can, that's also why there is no fmaxsearch function:
For example:
func = @(x) sin(x);
sol = fminsearch(@(x) func(x),0)
% sol = pi/2
sol = fminsearch(@(x) func(x)*-1,0)
% sol = -pi/2