I am trying to use fmincon to solve a simple problem: minimize f(x)=x(1)+x(2) under the constraint x(1)^2 +x(2)^2 = 1. I used the following code for this:
f = @(x) x(1) + x(2);x0 = [0; 0];lb = [-10; -10];ub = [10; 10];[x, retn] = fmincon(f, x0, [], [], [], [], lb, ub, @nonlincon)function [cineq, ceq] = nonlincon(x) cineq = []; ceq = x(1)^2 + x(2)^2 - 1;end
This outputs the value of x as [0.7071; 0.7071] and the value of retn as 1.4142. These values are clearly not the local or global minima. The minima occurs at [-0.7071; -0.7071]. In fact, MATLAB found the maxima.
If we change the initial value to [0; 1], then the correct minima is found. Also, correct minima is found when we change the algorithm to sqp. Both interior-point and active-set give the same (wrong) answer.
It also works correctly if we change the constraint to two inequality constraints: (x(1)^2 + x(2)^2 – 1.001 <= 0) and (0.999 – (x(1)^2 + x(2)^2) <= 0).
Please explain if I'm using fmincon in a way it isn't supposed to be used. I'm using MATLAB R2017a with a license for academic use.
Best Answer