Task: $$f(x)=x_1^2-2x_2+x_3^2\to \min$$ $$x_1+x_3=1$$ $$2x_1+x_2-x_3\le 2$$ $$x_1\ge0$$ Wolfram Mathematica result: $x_1=0, x_2=3, x_3=1, F(x)=-5$.
GNU Octave result: $x_1=0, x_2=3, x_3=1, F(x)=-5$.
How can I get the same results using Karush-Kun-Takker conditions (by solving a system of equations)?
It is advisable to see a complete consistent solution.
First of all we make it easy for ourselves by inserting the constraint $$ x_1 + x_3 = 1 \Rightarrow x_3 = 1 - x_1$$ into our function. Not only does this eliminate a constraint but also reduces the number of variables on which our function depends. The constrained problem is simplified to $$ f(x) = x_1^2 - 2x_2 + (1 - x_1)^2 \rightarrow \min $$ $$ \text{subject to} \quad \begin{aligned} 3x_1 + x_2 &\leq 3 \\ 0 &\leq x_1 \end{aligned} $$ For this problem we can define the Lagrangian $$ \mathcal{L}(x_1, x_2, \lambda_1, \lambda_2) = f(x) + \lambda_1(3x_1 + x_2 - 3) -\lambda_2x_1 $$ with the first order necessary KKT conditions $$ \begin{align} \nabla_x \mathcal{L} = 0 \\ \nabla_{\lambda} \mathcal{L} = 0. \end{align} $$ Written out these are stated as $$ \begin{align} 2x_1 - 2(1-x_1) + 3\lambda_1 - \lambda_2 &= 0 \\ -2 + \lambda_1 &= 0\\ 3x_1 + x_2 - 3 &= 0 \\ -x_1 &= 0 \end{align} $$ or in matrix form $$ \begin{pmatrix} 4 & 0 & 3 & -1 \\ 0 & 0 & 1 & 0 \\ 3 & 1 & 0 & 0 \\ -1 & 0 & 0 & 0 \end{pmatrix} \begin{pmatrix} x_1 \\ x_2 \\ \lambda_1 \\ \lambda_2 \end{pmatrix} = \begin{pmatrix} 2 \\ 2 \\ 3 \\ 0 \end{pmatrix} \tag{KKT}\label{kkt} $$ We observe that inserting $x_1 = 0, x_2 = 3, \lambda_1 = 2, \lambda_2 = 4$ solves \eqref{kkt}.