First of all, sorry if I am asking for trivial thing but I am just learning multivariable calculus and optimization toolbox in matlab--optimization as well :)
I was testing my understanding of using optimization toolbox in matlab on a simple 2D function and tried to find some local minima but, for particular points, matlab or fminunc
functions gives me the exact starting points with the following comment: "Initial point is a local minimum.". I have provided both a gradient and a user-supplied Hessian. I don't know whether it is MATLAB or me. Perhaps I made mistake defining Hessian but I think all is correct.
For random starting point, the function always give a proper (the closest) minimum. However for certain points, at exact maxima, the fminunc
returns exactly the same point with above statement. Should I use different optimization algorithm or it is simply the MATLAB thing I need to accept? I thought providing the Hessian would sort it out. The Hessian is always a negative-defined matrix at all problematic points and that should mean the point is a maximum!!! I tested it via returned eigenvalues by eig(hessian)
. It looks like fminunc
does not take Hessian into account at all. What should I do to make fminunc
work with hessians?
Cheers!
The points where fminunc
is wrong:
x0 = [0 -1.5*pi];
x = fminunc(F,x0,options)/pi
x =
0 -1.5000
x0 = [pi -pi/2];
x = fminunc(F,x0,options)/pi
x =
1.0000 -0.5000
x0 = [-pi 1.5*pi];
x = fminunc(F,x0,options)/pi
x =
-1.0000 1.5000
For instance the for the last point:
[minimum,gradient,hessian] = F([-pi 1.5*pi])
minimum =
1
gradient =
1.0e-15 *
-0.1225 0.1837
hessian =
-1.0000 -0.0000
-0.0000 -1.0000
I used the code below:
[X,Y]=meshgrid(-2*pi:4*pi/100:2*pi,-2*pi:4*pi/100:2*pi);
FF = @(X,Y) cos(X).*sin(Y);
surf(X,Y,FF(X,Y));
shading interp;
options = optimset('GradObj','on','Hessian','user-supplied','Display','iter');
F = @(x) deal(cos(x(1)).*sin(x(2)), [-sin(x(1)).*sin(x(2)) cos(x(1)).*cos(x(2))], [-cos(x(1)).*sin(x(2)) -sin(x(1)).*cos(x(2)); -sin(x(1)).*cos(x(2)) -cos(x(1)).*sin(x(2))]);
x0 = [0 pi/2];
x = fminunc(F,x0,options)