In [1]:
from IPython.display import display
import numpy as np
import matplotlib.pyplot as plt
from sympy import *
init_printing()
%matplotlib notebook
x,y,z,mu = symbols('x y z mu')

Extrema unter Nebenbedingungen

In [2]:
g = x**4+y**4-1
gn = lambdify((x,y),g)
gn(1,2)
Out[2]:
$$16$$
In [3]:
g = x**4+y**4-1
f = x+y
gn = lambdify((x,y),g)
fn = lambdify((x,y),f)
xv,yv = np.linspace(-1.3,1.3,100),np.linspace(-1.3,1.3,100)
X,Y = np.meshgrid(xv,yv)
fig = plt.figure()
ax = fig.gca()
pg = ax.contour(X,Y,gn(X,Y),0)
ax.contour(X,Y,fn(X,Y),[-2,-1,0,1,2])
ax.set_aspect('equal')
In [4]:
grad_g = Matrix(2,1,(g.diff(x),g.diff(y)))
grad_f = Matrix(2,1,(f.diff(x),f.diff(y)))
In [5]:
grad_gn = lambdify((x,y),grad_g)
grad_fn = lambdify((x,y),grad_f)
In [6]:
fig = plt.figure()
ax = fig.gca()
pg = ax.contour(X,Y,gn(X,Y),0)
ax.contour(X,Y,fn(X,Y),[-2,-1,0,1,2])
GF = grad_fn(X[::5,::5],Y[::5,::5])
ax.quiver(X[::5,::5],Y[::5,::5],GF[0],GF[1],angles='xy')
ax.set_aspect('equal')
In [7]:
SG = np.array((pg.allsegs)[0][0])
XG, YG = SG[::20,0], SG[::20,1]
XG**4+YG**4
Out[7]:
array([ 0.99986689,  0.99995981,  0.99950674,  0.99898692,  0.99903615,
        0.99918032,  0.99900014,  0.99898292,  0.99945686,  0.99959936,
        0.99897277,  0.99896993,  0.99921457,  0.99960604,  0.99898485,
        0.99904591])
In [8]:
fig = plt.figure()
ax = fig.gca()
pg = ax.contour(X,Y,gn(X,Y),0)
ax.contour(X,Y,fn(X,Y),[-2,-1,0,1,2])
GG = grad_gn(XG,YG)
ax.quiver(XG,YG,GG[0],GG[1],angles='xy',scale=50)
ax.set_aspect('equal')
In [9]:
GLF = grad_f-mu*grad_g
In [10]:
M = solve([GLF,g])
M
Out[10]:
$$\left [ \left \{ \mu : - \frac{2^{\frac{3}{4}}}{4}, \quad x : - \frac{2^{\frac{3}{4}}}{2}, \quad y : - \frac{2^{\frac{3}{4}}}{2}\right \}, \quad \left \{ \mu : \frac{2^{\frac{3}{4}}}{4}, \quad x : \frac{2^{\frac{3}{4}}}{2}, \quad y : \frac{2^{\frac{3}{4}}}{2}\right \}, \quad \left \{ \mu : \frac{\sqrt{2}}{8} \left(-1 + i\right), \quad x : - \frac{\sqrt{2}}{4} + \frac{\sqrt{6}}{4} + \frac{\sqrt{2} i}{4} + \frac{\sqrt{6} i}{4}, \quad y : - \frac{\sqrt{6}}{4} - \frac{\sqrt{2}}{4} - \frac{\sqrt{6} i}{4} + \frac{\sqrt{2} i}{4}\right \}, \quad \left \{ \mu : \frac{\sqrt{2}}{8} \left(-1 + i\right), \quad x : - \frac{\sqrt{6}}{4} - \frac{\sqrt{2}}{4} - \frac{\sqrt{6} i}{4} + \frac{\sqrt{2} i}{4}, \quad y : - \frac{\sqrt{2}}{4} + \frac{\sqrt{6}}{4} + \frac{\sqrt{2} i}{4} + \frac{\sqrt{6} i}{4}\right \}, \quad \left \{ \mu : \frac{\sqrt{2}}{8} \left(1 - i\right), \quad x : \frac{\sqrt{2}}{4} + \frac{\sqrt{6}}{4} - \frac{\sqrt{2} i}{4} + \frac{\sqrt{6} i}{4}, \quad y : - \frac{\sqrt{6}}{4} + \frac{\sqrt{2}}{4} - \frac{\sqrt{6} i}{4} - \frac{\sqrt{2} i}{4}\right \}, \quad \left \{ \mu : \frac{\sqrt{2}}{8} \left(1 - i\right), \quad x : - \frac{\sqrt{6}}{4} + \frac{\sqrt{2}}{4} - \frac{\sqrt{6} i}{4} - \frac{\sqrt{2} i}{4}, \quad y : \frac{\sqrt{2}}{4} + \frac{\sqrt{6}}{4} - \frac{\sqrt{2} i}{4} + \frac{\sqrt{6} i}{4}\right \}, \quad \left \{ \mu : - \frac{\sqrt{2}}{8} \left(1 + i\right), \quad x : - \frac{\sqrt{2}}{4} + \frac{\sqrt{6}}{4} - \frac{\sqrt{6} i}{4} - \frac{\sqrt{2} i}{4}, \quad y : - \frac{\sqrt{6}}{4} - \frac{\sqrt{2}}{4} - \frac{\sqrt{2} i}{4} + \frac{\sqrt{6} i}{4}\right \}, \quad \left \{ \mu : - \frac{\sqrt{2}}{8} \left(1 + i\right), \quad x : - \frac{\sqrt{6}}{4} - \frac{\sqrt{2}}{4} - \frac{\sqrt{2} i}{4} + \frac{\sqrt{6} i}{4}, \quad y : - \frac{\sqrt{2}}{4} + \frac{\sqrt{6}}{4} - \frac{\sqrt{6} i}{4} - \frac{\sqrt{2} i}{4}\right \}, \quad \left \{ \mu : \frac{\sqrt{2}}{8} \left(1 + i\right), \quad x : \frac{\sqrt{2}}{4} + \frac{\sqrt{6}}{4} - \frac{\sqrt{6} i}{4} + \frac{\sqrt{2} i}{4}, \quad y : - \frac{\sqrt{6}}{4} + \frac{\sqrt{2}}{4} + \frac{\sqrt{2} i}{4} + \frac{\sqrt{6} i}{4}\right \}, \quad \left \{ \mu : \frac{\sqrt{2}}{8} \left(1 + i\right), \quad x : - \frac{\sqrt{6}}{4} + \frac{\sqrt{2}}{4} + \frac{\sqrt{2} i}{4} + \frac{\sqrt{6} i}{4}, \quad y : \frac{\sqrt{2}}{4} + \frac{\sqrt{6}}{4} - \frac{\sqrt{6} i}{4} + \frac{\sqrt{2} i}{4}\right \}, \quad \left \{ \mu : - \frac{2^{\frac{3}{4}} i}{4}, \quad x : - \frac{2^{\frac{3}{4}} i}{2}, \quad y : - \frac{2^{\frac{3}{4}} i}{2}\right \}, \quad \left \{ \mu : \frac{2^{\frac{3}{4}} i}{4}, \quad x : \frac{2^{\frac{3}{4}} i}{2}, \quad y : \frac{2^{\frac{3}{4}} i}{2}\right \}\right ]$$
In [11]:
rsol = []
for sol in M:
    if (im(sol[mu])==0) & (im(sol[x])==0) & (im(sol[y])==0):
        rsol.append(sol)
rsol
Out[11]:
$$\left [ \left \{ \mu : - \frac{2^{\frac{3}{4}}}{4}, \quad x : - \frac{2^{\frac{3}{4}}}{2}, \quad y : - \frac{2^{\frac{3}{4}}}{2}\right \}, \quad \left \{ \mu : \frac{2^{\frac{3}{4}}}{4}, \quad x : \frac{2^{\frac{3}{4}}}{2}, \quad y : \frac{2^{\frac{3}{4}}}{2}\right \}\right ]$$
In [12]:
g.subs(rsol[0])
Out[12]:
$$0$$
In [13]:
g.subs(rsol[1])
Out[13]:
$$0$$
In [14]:
H = Matrix(2,2,[ (f-mu*g).diff(x1,x2) for x1 in [x,y] for x2 in [x,y]])
H
Out[14]:
$$\left[\begin{matrix}- 12 \mu x^{2} & 0\\0 & - 12 \mu y^{2}\end{matrix}\right]$$
In [15]:
H.subs(rsol[0])
Out[15]:
$$\left[\begin{matrix}3 \sqrt[4]{2} & 0\\0 & 3 \sqrt[4]{2}\end{matrix}\right]$$

Also haben wir hier ein Minimum (Achtung: Fuer das Minimum muss nur ein Teil der Hessematrix positiv definit sein -> Optimierung

In [16]:
H.subs(rsol[1])
Out[16]:
$$\left[\begin{matrix}- 3 \sqrt[4]{2} & 0\\0 & - 3 \sqrt[4]{2}\end{matrix}\right]$$
In [17]:
xx=rsol[0][x]
yy=rsol[0][y]
In [18]:
fig = plt.figure()
ax = fig.gca()
pg = ax.contour(X,Y,gn(X,Y),0)
ax.contour(X,Y,fn(X,Y),[-2,-1,0,1,2])
GG = grad_gn(XG,YG)
GF = grad_fn(XG,YG)
ax.quiver(XG,YG,GG[0],GG[1],angles='xy',scale=40,color='blue')
ax.quiver(XG,YG,GF[0],GF[1],angles='xy',scale=15,color='orange')
ax.set_aspect('equal')
ax.annotate('Lösung',(xx,yy),(xx+.2,yy+.2),\
            arrowprops={'arrowstyle':'->'},color='red',fontsize=14)
Out[18]:
<matplotlib.text.Annotation at 0x7fa6435563c8>
In [19]:
xx
Out[19]:
$$- \frac{2^{\frac{3}{4}}}{2}$$