Why does fmincon give me wrong results?

I want to solve the problem for a given with neighborhood with fmincon. So I used
[dp,~] = fmincon(@(p) -fun([x,p]),0,[],[],[],[],[],[],@constr);
and for some points x it gives wrong results as you can see in the plot.
Does anyone know why this happens and how to avoid it?

2 Comments

Could you please share the code you wrote for solving the optimization problem?
x = 0;
objFun = @(dx) -2*(x+dx).^4;
x0 = 10^-3;
nonlcon = @(dx) deal(norm(dx) - 1, []);
options = optimoptions('fmincon', 'Algorithm', 'sqp');
[dx,] = fmincon(objFun,x0,[],[],[],[],[],[],nonlcon,options);
the function value I get is 2*10^-12, but it should be 2.

Sign in to comment.

 Accepted Answer

The problem has a very simple analytical solution. Why use fmincon at all?
[fval,i]=max(2*(x-1)^4, 2*(x+1)^4);
dx=2*i-3;
In any case, it appears that fmincon fails because your initial guess is too close to the first order stationary point at x=0.
x = 0;
objFun = @(dx) -2*(x+dx).^4;
dx0 = 0.01;
nonlcon = @(dx) deal(norm(dx)^2 - 1, []); %Make constraints differentiable
options = optimoptions('fmincon', 'Algorithm', 'sqp');
[dx,fval] = fmincon(objFun,dx0,[],[],[],[],[],[],nonlcon,options)
Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
dx = 1
fval = -2

5 Comments

Because I have to solve a more complex problem in this way that i can't solve analytically. And if fmincon does not solve this easy problem i have no idea how to solve the more complex problem.
Matt J
Matt J on 29 Dec 2023
Edited: Matt J on 29 Dec 2023
Well, it can with a different initial point. See above.
So i have to chose the starting point x0 depending on x? I think this solves my problem.
I don't know what your real problem looks like. Basically, avoid an initial guess that is too near any local solutions, be they minima, maxima, or inflection points.
I'm minimizing over x, so maybe i can chose x0 = x+0.01 or something

Sign in to comment.

More Answers (1)

A basic structure of how you could set up your optimization problem in MATLAB:
% Define the objective function, which should be negated for maximization
objFun = @(p) -yourFunctionToMaximize(p);
% Define the initial guess
x0 = yourInitialGuess; % This should be within the neighborhood N
% Define the nonlinear constraint function for the neighborhood N
nonlcon = @(x) deal([], norm(x) - 1); % An empty matrix [] for inequality constraints and norm(x) - 1 for equality
% Set options for fmincon, you can try different algorithms if needed
options = optimoptions('fmincon', 'Algorithm', 'sqp', 'Display', 'iter');
% Run the optimization
[x, fval] = fmincon(objFun, x0, [], [], [], [], [], [], nonlcon, options);
Make sure to replace yourFunctionToMaximize with the actual function you're maximizing and yourInitialGuess with a suitable starting point for the algorithm. The nonlcon function is defined to enforce the neighborhood constraint. The deal function is used to return empty inequality constraints and the equality constraint for the neighborhood definition.
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.

1 Comment

that's exactly what I did, with the difference that i did not use 'sqp' and the empty matrix [] for equality constraints. But even with 'sqp' there are wrong values around x0=0. And I need to get exact values around zero.

Sign in to comment.

Products

Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!