Docsity

Find the Solution using KKT Conditions - Notes | MATH 4553, Study notes of Mathematics

Toggle sidebar
bg1
Find the solution using KKT
conditions
Problem 1
Minimizing f(x,y) =
Hx-2L2+Hy-3L2subjectto Hx-4L2+Hy-5L2-6£0.
In[1]:=
f=Hx-2L^2 +Hy-3L^ 2; g =Hx-4L^2 +Hy-5L^ 2 -6;
LagrangianFunction =f+u*Hg+s^2L
Out[2]=
H-2+xL2+uI-6+s2+H-4+xL2+H-5+yL2M+H-3+yL2
Next, compute the gradient of the Lagrangian function:
In[3]:=
GradLF =D@LagrangianFunction, 88x, y, u, s<<D;
GradLF MatrixForm
Out[4]//MatrixForm=
ikjjjjjjjjjjjjjjj
2uH-4+xL+2H-2+xL
2uH-5+yL+2H-3+yL
-6+s2+H-4+xL2+H-5+yL2
2su y{zzzzzzzzzzzzzzz
Find all stationary points by solving the system of equations: GradLF = 0
In[5]:=
eqns =Thread@GradLF 80, 0, 0, 0<D
Out[5]=
92uH-4+xL+2H-2+xL0, 2 u H-5+yL+2H-3+yL0, -6+s2+H-4+xL2+H-5+yL20, 2 s u 0=
In[6]:=
FindRoot@eqns, 8x, 0<,8y, 0<,8u, 1<,8s, 1<D
Out[6]=
9x®2.26795, y ®3.26795, u ®0.154701, s ® - 1.91617 ´10-23=
(A few remarks about the finction FindRoot: (1) an initial guess for each variable must be given, for example, {x=0, y=0, u=1,
s=1} in the above example. You might want to try a few sets of initial values to see if there exists more than one roots. (2)
FindRoot is a numerical approach and its solution is affected by "finite precision" limit of the computer. In the above example, the
solution s=-1.91617
1023 can be considerd as s=0.M
pf3
pf4

Partial preview of the text

Download Find the Solution using KKT Conditions - Notes | MATH 4553 and more Study notes Mathematics in PDF only on Docsity!

Find the solution using KKT

conditions

Problem 1

Minimizing f(x,y) = H x - 2 L

2

+ H y - 3 L

2

subject to H x - 4 L

2

+ H y - 5 L

2

  • 6 £ 0.

In[1]:= f = H x - 2 L ^ 2 + H y - 3 L ^ 2; g = H x - 4 L ^ 2 + H y - 5 L ^ 2 - 6;

LagrangianFunction = f + u *** H** g + s ^ 2 L

Out[2]= H- 2 + xL

2

  • u I- 6 + s

2

  • H- 4 + xL

2

  • H- 5 + yL

2

M + H- 3 + yL

2

Next, compute the gradient of the Lagrangian function:

In[3]:= GradLF = D @ LagrangianFunction, 88 x, y, u, s <<D ;

GradLF êê MatrixForm

Out[4]//MatrixForm=

i

k

j j j j j j j j j j j j j j j

2 u H- 4 + xL + 2 H- 2 + xL

2 u H- 5 + yL + 2 H- 3 + yL

  • 6 + s

2

  • H- 4 + xL

2

  • H- 5 + yL

2

2 s u

y

z z z z z z z z z z z z z z z

Find all stationary points by solving the system of equations: GradLF = 0

In[5]:= eqns = Thread @ GradLF ä 8 0, 0, 0, 0 <D

Out[5]= 9 2 u H- 4 + xL + 2 H- 2 + xL ä 0, 2 u H- 5 + yL + 2 H- 3 + yL ä 0, - 6 + s

2

  • H- 4 + xL

2

  • H- 5 + yL

2

ä 0, 2 s u ä 0 =

In[6]:= FindRoot @ eqns, 8 x, 0 < , 8 y, 0 < , 8 u, 1 < , 8 s, 1 <D

Out[6]= 9 x Æ 2.26795, y Æ 3.26795, u Æ 0.154701, s Æ -1.91617 ¥ 10

  • 23

=

(A few remarks about the finction FindRoot: (1) an initial guess for each variable must be given, for example, {x=0, y=0, u=1,

s=1} in the above example. You might want to try a few sets of initial values to see if there exists more than one roots. (2)

FindRoot is a numerical approach and its solution is affected by "finite precision" limit of the computer. In the above example, the

solution s=-1.91617 10

23

can be considerd as s = 0.M

Problem 2

Minimize f(x,y) = H x - 2 L^2 + H y - 3 L^2 subject to H x - 4 L^2 + H y - 5 L

2

  • 6 = 0.

For this problem, we follow the same procedure as shown in Problem 1 (this only difference is that Problem 2 has an equality

constraint while Problem 1 has a "less than" type constraint). Pay attention to the different definition of the Lagrangian function.

In[7]:= f = H x - 2 L ^ 2 + H y - 3 L ^ 2; h = H x - 4 L ^ 2 + H y - 5 L ^ 2 - 6;

LagrangianFunction = f + v ***** h

Out[8]= H- 2 + xL

2

  • v I- 6 + H- 4 + xL

2

  • H- 5 + yL

2

M + H- 3 + yL

2

In[9]:= GradLF = D @ LagrangianFunction, 88 x, y, v <<D ;

GradLF êê MatrixForm

Out[10]//MatrixForm=

i

k

j j j j j j j j jj

2 v H- 4 + xL + 2 H- 2 + xL

2 v H- 5 + yL + 2 H- 3 + yL

  • 6 + H- 4 + xL

2

  • H- 5 + yL

2

y

z z z z z z z z zz

In[11]:= eqns = Thread @ GradLF ä 8 0, 0, 0 <D

Out[11]= 9 2 v H- 4 + xL + 2 H- 2 + xL ä 0, 2 v H- 5 + yL + 2 H- 3 + yL ä 0, - 6 + H- 4 + xL

2

  • H- 5 + yL

2

ä 0 =

Finally we are ready to find the stationary points using FindRoot:

In[12]:= FindRoot @ eqns, 8 x, 0 < , 8 y, 0 < , 8 v, 0 <D

Out[12]= 8 x Æ 2.26795, y Æ 3.26795, v Æ 0.154701<

In[13]:= FindRoot @ eqns, 8 x, 10 < , 8 y, 10 < , 8 v, 0 <D

Out[13]= 8 x Æ 5.73205, y Æ 6.73205, v Æ -2.1547<

Notice that by setting different initial guess in FindRoot, we end up with two totally different stationary points. You can try more

initial guesses to see if there exists more stationary points (The answer is no).

We need to decide which one of the stationary point is the solution. One thing we can try is to use the second order condition: if

the Hessian of the Lagrangian function with respect to (x,y) is positive definite along directions of feasible changes, then the

stationary point is a local minimum.

In[18]:= Show @

ContourPlot @ h, 8 x, - 2, 10 < , 8 y, - 2, 10 < , Contours Æ 8 0 < ,

Axes -> True, Frame Æ False, AxesLabel Æ 8 "x", "y" < ,

ContourShading Æ 8 White, White < , ContourStyle Æ 8 Black, Thick <D ,

ContourPlot @ f, 8 x, - 2, 10 < , 8 y, - 2, 10 < , Contours Æ 8 0.14, 3, 10, 27.8, 50 < ,

Axes -> True, Frame Æ False, AxesLabel Æ 8 "x", "y" < ,

ContourShading Æ False, ContourStyle Æ Directive @ Blue, Dashed D ,

ContourLabels Æ Function @8 x, y, z < , Text @ Framed @ z D , 8 x, y < , Background Æ White DDD< ,

Epilog Æ 8

Text @ Style @ " H x - 4 L ^2 +H y - 5 L ^2 - 6 = 0", Medium, Bold D , 8 5.5, 2.2 <D ,

Disk @8 2.27, 3.27 < , 8 .2, .2 <D ,

Disk @8 5.73, 6.73 < , 8 .2, .2 <D<

D

Out[18]=

3

10

50

50

  • 2 2 4 6 8 10

x

  • 2

2

4

6

8

10

y

Hx- 4 L^2+Hy- 5 L^2- 6 = 0