Issues with 'fminunc' returning incorrect results for optimization in MATLAB R2023b
I can't seem to get I'm encountering unexpected behavior while using the `fminunc` function in MATLAB R2023b for optimizing a custom objective function... The function seems to converge to a solution, but the resulting parameter estimates are significantly off from what I expect based on initial conditions and theoretical values. After running the following code, I noticed that the optimization results do not align with the expected output. ```matlab function obj = myObjective(params) % A simple quadratic objective for demonstration x = 1; % Example input obj = (x - params(1))^2 + (x^2 - params(2))^2; end % Initial parameter guess initialParams = [0; 0]; options = optimoptions('fminunc', 'Display', 'iter', 'Algorithm', 'quasi-newton'); [paramsOpt, fval, exitflag, output] = fminunc(@myObjective, initialParams, options); ``` The function seems to be converging, as indicated by the output: ``` Optimization terminated: first-order optimality measure less than options.TolFun. ``` However, the optimized parameters are: ``` paramsOpt = 2.3456 -1.2345 ``` These results are quite far from the expected values of `[1; 1]` that I anticipate based on the shape of the objective function. I have tried adjusting the `TolFun` and `MaxIterations` settings within `optimoptions`, but the results remain unchanged. Could there be potential issues with the scaling of parameters or the optimization algorithm's convergence criteria? Any insights or suggestions on how to improve the optimization results would be appreciated. The project is a mobile app built with Matlab. I'd be grateful for any help.