MATLAB: Getting different solutions by suppying same constraint to fmincon in two similar way (linear and nonlinear))

fminconoptimizationOptimization Toolbox

I am using `fmincon` to solve a problem. The problem has some linear inequality constraints that are written in Matrix A and B. I can write these constraints in 2 way and I should get analogous results. But, weirdly I am getting different solutions. why is that?
1) In the first way, I can feed the constraint to 'fmincon' function as follows:
`[Xsqp, FUN ,FLAG ,Options] = fmincon(@(X)SQP(X,Dat),X,A,B,[],[],lb,ub,@(X)SQPnolcon(X,Dat,A,B),options);
% I comment the line 'C=A*X'-B;'
in the function 'SQPnolcon' and put C=[] instead, because A and B are defined already in fmincon function`
2) As the second way I can write it like this:
`[Xsqp, FUN ,FLAG ,Options] = fmincon(@(X)SQP(X,Dat),X,[],[],[],[],lb,ub,@(X)SQPnolcon(X,Dat,A,B),options);`
and also the constraint function as follows:
function [C,Ceq] = SQPnolcon(X,Dat,A,B)
C=A*X'-B;
Ceq = [];
end
Note: In the first, you're supplying A and B as both linear inequality constraints and as nonlinear inequality constraints, but in the second you're only supplying them as nonlinear inequality constraints. What option should I set for fmincon here to get same results?

Best Answer

One possibility that I could see here is in the difference between how fmincon would treat the derivatives of linear and non-linear constraints. Since fmincon is a gradient based solver it will approximate the Hessian matrix. For a linear constraint we do not need to calculate the second order derivatives as they are all 0, so MATLAB will not bother. However, when you pass this as a non-linear constraint then these are approximated using finite-differences and are therefore unlikely to be 0 and will affect your results. Your best bet is to pass linear constraints as linear.