MATLAB: Wrong Hessian output in fminunc

optimizationOptimization Toolbox

Can somebody explain weird Matlab's output for the following optimization problem? The goal is to minimize and to evaluate Hessian matrix at the solution. However, Matlab returns a matrix of NaNs for the Hessian at the solution point. Here is the code
[xOpt,~,~,~,grad,hessian] = fminunc(@(x) x(1)^2+x(2)^2,[1,1])

Best Answer

Another solution would be to use standalone routines for numerical gradient and hessian estimation
fun=@(x) (x(1)^2+x(2)^2);
xOpt = fminunc(fun ,[1,1]);
g=gradest(fun,xOpt)
H=hessian(fun,xOpt)
No hidden scale factors if you go that route:
xOpt =
0 0
g =
0 0
H =
2.0000 0
0 2.0000