Explanation of the Issue
In your nested fmincon setup, the inner call to fmincon returns the optimal y, but you are passing that directly to the outer fmincon as if it were the objective function value. Essentially, the outer optimization is minimizing the solution y* rather than minimizing x + y*. That’s why you get x=0 and f=−10 instead of the expected (x,y)=(−10,−10) and -20.
How to Fix
- Capture Both the Solution and Objective from the inner problem. In MATLAB, fmincon’s second output argument is the minimum function value. For example:[yOpt, fvalInner] = fmincon(@(y) f(x,y), ... );
Here, yOpt is the y that minimizes f(x,y), and fvalInner is the corresponding minimum value f(x,yOpt).
- Return the Inner Objective to the Outer Problem. Instead of returning yOpt from your inner function, return fvalInner. That way, the outer fmincon will correctly minimize f(x,y*), not y*.
Corrected Example
function val = min_fun_1(x)
[~, fvalInner] = fmincon(@(y) f(x, y), 0, [], [], [], [], -10, 10);
[x_opt_minmin, fval_minmin] = fmincon(@(x) min_fun_1(x), initial_x, ...
[], [], [], [], -10, 10);
disp('Optimal minmin x:');
disp('Optimal minmin value of f:');
With this change, you should get x=−10, and the inner solution will be y=−10, giving f=−20.
Follow me so you can message me anytime with future questions. If this helps, please accept the answer and upvote it as well.