CodexBloom - Programming Q&A Platform

implementing MATLAB's 'fminunc' not converging for complex optimization solution

👀 Views: 80 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-14
optimization fminunc nonlinear MATLAB

I'm trying to optimize a complex nonlinear function using MATLAB's `fminunc` function, but it keeps failing to converge, returning a message: `Warning: Maximum number of function evaluations has been exceeded.` I've made sure to provide a good initial guess and set the options with `optimoptions`, but I still need to get it to work properly. Here's the function I'm trying to optimize: ```matlab function f = myObjectiveFunction(x) f = (x(1) - 3)^2 + (x(2) - 2)^2 + sin(x(1) * x(2)); end ``` And I'm calling `fminunc` like this: ```matlab options = optimoptions('fminunc', 'Algorithm', 'quasi-newton', 'MaxFunEvals', 1000, 'TolFun', 1e-6); initialGuess = [0, 0]; [x_opt, fval] = fminunc(@myObjectiveFunction, initialGuess, options); ``` I've tried increasing the `MaxFunEvals` to 5000 and adjusted the `TolFun` to a more lenient value, but the function still doesn't converge and often returns values that seem like they are bouncing around instead of approaching a minimum. I also tried changing the algorithm to `trust-region` but faced the same scenario. Are there specific strategies or tweaks I can apply to help `fminunc` converge on this question? Any advice on diagnosing the function behavior would be greatly appreciated!