implementing 'fminunc' optimization in MATLAB when using custom Hessian function
I'm getting frustrated with I am working with problems with the `fminunc` function in MATLAB R2023b when I provide a custom Hessian function. My objective is to minimize a non-linear objective function, but I keep receiving an behavior claiming that the Hessian is not positive definite. Here is the code I have written: ```matlab function main % Initial guess x0 = [0; 0]; % Options for fminunc options = optimoptions('fminunc', 'Algorithm', 'trust-region', 'Hessian', 'on', 'Display', 'iter'); % Call fminunc [x, fval] = fminunc(@objectiveFunction, x0, options); end function [f, grad, hess] = objectiveFunction(x) % Sample quadratic objective function f = x(1)^2 + x(2)^2; % Gradient computation grad = [2*x(1); 2*x(2)]; % Custom Hessian: should be identity matrix hess = eye(2); end ``` When I run this code, I get the following behavior message: ``` Warning: The Hessian returned by the objective function is not positive definite. ``` I tried using different definitions for the Hessian function, including a scalar multiple of the identity matrix, but I still get the same warning. Additionally, I verified that the initial guess is well within the feasible region for the question. Does anyone have insights into why `fminunc` is rejecting my Hessian, and how I could resolve this scenario? Any help with correct usage of `fminunc` with a custom Hessian would be appreciated! For reference, this is a production application. I'd be grateful for any help.