title: Minimizing a function using Lagrange multipliers in matlab
body: <p>I have a problem with my MATLAB code that I write to minimize this function with two constraints (one of them is inequality and the other one is equality) with Lagrange Multipliers that use KKT conditions. This the Function :</p>
  
  <pre><code>       MIN F = 2*x1^2 + 2*x2^2-6*x1*x2
          ineq=x1+2*x2&lt;=3
          eq=3*x1+x2=9
  </code></pre>
  
  <p>I am personally suspect that my <strong>if Function(statement)</strong> has problem but don't know how to fix that .....</p>
  
  <pre><code>    clc
      warning off
      syms x1 x2 u s lambda
      f=2*x1^2+2*x2^2-6*x1*x2;
      g1=sqrt(x1^2*x2)+s^2-0.25;
      H1=x1*x2^2-0.1;
      Lagrange=f+u*g1+lambda*H1;
      Grad=gradient(Lagrange);
      S=solve(Grad);
      S=double([S.x1 S.x2 S.s S.u S.lambda]);M=size(S,1);
        for i=M:-1:1
      if imag(S(i,3))~=0 || S(i,1)&lt;=0 || S(i,2)&lt;=0
        end
      end
      x1=S(:,1);x2=S(:,2);s=S(:,3);u=S(:,4);lambda=S(:,5);
      out=table(x1,x2,s,u,lambda)
      x0=[1 0.5 -0.75];
  </code></pre>
  
  <p>It seems I have a problem because it gives me both write and wrong answers can you help me with that ?</p>