In [1]:
import numpy as np
import matplotlib.pyplot as plt
from sympy import *
init_printing()
%matplotlib notebook

Lektion 11

Extremwerte (Gradient Hessematrix)

In [2]:
x,y,z = symbols('x y z')
var = [x,y,z]
In [3]:
f = exp(x**2+y**2+z**2)
In [4]:
gr = Matrix([f.diff(t) for t in var])
gr
Out[4]:
$$\left[\begin{matrix}2 x e^{x^{2} + y^{2} + z^{2}}\\2 y e^{x^{2} + y^{2} + z^{2}}\\2 z e^{x^{2} + y^{2} + z^{2}}\end{matrix}\right]$$
In [5]:
J = Matrix([f]).jacobian(var)
J
Out[5]:
$$\left[\begin{matrix}2 x e^{x^{2} + y^{2} + z^{2}} & 2 y e^{x^{2} + y^{2} + z^{2}} & 2 z e^{x^{2} + y^{2} + z^{2}}\end{matrix}\right]$$
In [6]:
H = factor(gr.jacobian(var))
H
Out[6]:
$$\left[\begin{matrix}2 \left(2 x^{2} + 1\right) e^{x^{2}} e^{y^{2}} e^{z^{2}} & 4 x y e^{x^{2}} e^{y^{2}} e^{z^{2}} & 4 x z e^{x^{2}} e^{y^{2}} e^{z^{2}}\\4 x y e^{x^{2}} e^{y^{2}} e^{z^{2}} & 2 \left(2 y^{2} + 1\right) e^{x^{2}} e^{y^{2}} e^{z^{2}} & 4 y z e^{x^{2}} e^{y^{2}} e^{z^{2}}\\4 x z e^{x^{2}} e^{y^{2}} e^{z^{2}} & 4 y z e^{x^{2}} e^{y^{2}} e^{z^{2}} & 2 \left(2 z^{2} + 1\right) e^{x^{2}} e^{y^{2}} e^{z^{2}}\end{matrix}\right]$$
In [7]:
hessian(f,var)
Out[7]:
$$\left[\begin{matrix}4 x^{2} e^{x^{2} + y^{2} + z^{2}} + 2 e^{x^{2} + y^{2} + z^{2}} & 4 x y e^{x^{2} + y^{2} + z^{2}} & 4 x z e^{x^{2} + y^{2} + z^{2}}\\4 x y e^{x^{2} + y^{2} + z^{2}} & 4 y^{2} e^{x^{2} + y^{2} + z^{2}} + 2 e^{x^{2} + y^{2} + z^{2}} & 4 y z e^{x^{2} + y^{2} + z^{2}}\\4 x z e^{x^{2} + y^{2} + z^{2}} & 4 y z e^{x^{2} + y^{2} + z^{2}} & 4 z^{2} e^{x^{2} + y^{2} + z^{2}} + 2 e^{x^{2} + y^{2} + z^{2}}\end{matrix}\right]$$
In [8]:
factor(hessian(f,var))-H
Out[8]:
$$\left[\begin{matrix}0 & 0 & 0\\0 & 0 & 0\\0 & 0 & 0\end{matrix}\right]$$
In [9]:
f = Function('f')
f(*var)
Out[9]:
$$f{\left (x,y,z \right )}$$
In [10]:
hessian(f(x,y,z),var)
Out[10]:
$$\left[\begin{matrix}\frac{\partial^{2}}{\partial x^{2}} f{\left (x,y,z \right )} & \frac{\partial^{2}}{\partial x\partial y} f{\left (x,y,z \right )} & \frac{\partial^{2}}{\partial x\partial z} f{\left (x,y,z \right )}\\\frac{\partial^{2}}{\partial x\partial y} f{\left (x,y,z \right )} & \frac{\partial^{2}}{\partial y^{2}} f{\left (x,y,z \right )} & \frac{\partial^{2}}{\partial y\partial z} f{\left (x,y,z \right )}\\\frac{\partial^{2}}{\partial x\partial z} f{\left (x,y,z \right )} & \frac{\partial^{2}}{\partial y\partial z} f{\left (x,y,z \right )} & \frac{\partial^{2}}{\partial z^{2}} f{\left (x,y,z \right )}\end{matrix}\right]$$
In [11]:
from mpl_toolkits.mplot3d import Axes3D
In [12]:
f = 1/2 * x**4  - x**2*y**2 - 1/2*y**4 + x**3 -2*x*y**2
In [13]:
fn = lambdify((x,y),f)
In [14]:
gn = np.linspace(-5,5)
X,Y = np.meshgrid(gn,gn)
F = fn(X,Y)
In [15]:
fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
ax.plot_surface(X,Y,F,linewidth=0,cmap=plt.cm.cool);
In [16]:
fig = plt.figure()
ax = fig.gca()
ax.contour(X,Y,F,np.array([-10, -1, 0, 1, 10]))
Out[16]:
<matplotlib.contour.QuadContourSet at 0x7f9f10e09588>
In [17]:
ax.contour?
In [18]:
ext = solve({Eq(f.diff(y),0),Eq(f.diff(x),0)}) # Kandidaten fuer Extrema (grad(f) = 0)
In [19]:
ext
Out[19]:
$$\left [ \left \{ x : -1.64038820320221, \quad y : -0.768051397498532\right \}, \quad \left \{ x : -1.64038820320221, \quad y : 0.768051397498532\right \}, \quad \left \{ x : -1.5, \quad y : 0.0\right \}, \quad \left \{ x : -0.609611796797792, \quad y : -0.920650341226544\right \}, \quad \left \{ x : -0.609611796797792, \quad y : 0.920650341226544\right \}, \quad \left \{ x : 0.0, \quad y : 0.0\right \}\right ]$$
In [20]:
H = hessian(f,(x,y))
H
Out[20]:
$$\left[\begin{matrix}6.0 x^{2} + 6 x - 2 y^{2} & - 4 x y - 4 y\\- 4 x y - 4 y & - 2 x^{2} - 4 x - 6.0 y^{2}\end{matrix}\right]$$
In [21]:
H0 = H.subs(ext[0])
H0, H0.det()
Out[21]:
$$\left ( \left[\begin{matrix}5.12310562561766 & -1.96740421764412\\-1.96740421764412 & -2.35961179679779\end{matrix}\right], \quad -15.9592198260524\right )$$
In [22]:
H1 = H.subs(ext[1])
H1, H1.det()
Out[22]:
$$\left ( \left[\begin{matrix}5.12310562561766 & 1.96740421764412\\1.96740421764412 & -2.35961179679779\end{matrix}\right], \quad -15.9592198260524\right )$$
In [23]:
H2 = H.subs(ext[2])
H2, H2.det()
Out[23]:
$$\left ( \left[\begin{matrix}4.5 & 0\\0 & 1.5\end{matrix}\right], \quad 6.75\right )$$
In [24]:
H3 = H.subs(ext[3])
H3, H3.det()
Out[24]:
$$\left ( \left[\begin{matrix}-3.12310562561766 & 1.43764412995572\\1.43764412995572 & -3.39038820320221\end{matrix}\right], \quad 8.52171982605243\right )$$
In [25]:
H4 = H.subs(ext[4])
H4, H4.det()
Out[25]:
$$\left ( \left[\begin{matrix}-3.12310562561766 & -1.43764412995572\\-1.43764412995572 & -3.39038820320221\end{matrix}\right], \quad 8.52171982605243\right )$$
In [26]:
H5 = H.subs(ext[5])
H5, H5.det()
Out[26]:
$$\left ( \left[\begin{matrix}0 & 0\\0 & 0\end{matrix}\right], \quad 0\right )$$
In [27]:
xv = []
yv = []
fv = []
for ex in ext:
    fw = f.subs(ex)
    print('x', ex[x].n(), 'y', ex[y].n(), 'f',fw.n())
    xv.append(ex[x].n())
    yv.append(ex[y].n())
    fv.append(fw.n())
x -1.64038820320221 y -0.768051397498532 f -0.619684349426759
x -1.64038820320221 y 0.768051397498532 f -0.619684349426759
x -1.50000000000000 y 0 f -0.843750000000000
x -0.609611796797792 y -0.920650341226544 f 0.201715599426759
x -0.609611796797792 y 0.920650341226544 f 0.201715599426759
x 0 y 0 f 0
In [28]:
from matplotlib.colors import Normalize
norm = Normalize(-1,0.2)
xn = np.linspace(-2,0.3)
yn = np.linspace(-1.2,1.2)
X,Y = np.meshgrid(xn,yn)
F = fn(X,Y)
fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
ax.plot_surface(X,Y,F,linewidth=0,cmap=plt.cm.cool,norm=norm)
ax.plot3D(xv,yv,fv,'kx')
ax.set_ylim(-1.2,1.1)
ax.set_xlim(-2,0)
ax.set_zlim(-1,1);

Tangente an Flaechen

In [29]:
plt.figure()
xt = np.linspace(-1,1)
plt.plot(xn,fn(xn,-1))
tangent = lambda xt: fn(0,-1) + f.diff(x).subs({x:0,y:-1})*xt
plt.plot(xt,tangent(xt));
In [30]:
from matplotlib.colors import Normalize
norm = Normalize(-1,0.2)
xn = np.linspace(-2,0.3)
yn = np.linspace(-1.2,1.2)
X,Y = np.meshgrid(xn,yn)
F = fn(X,Y)
fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
ax.plot_surface(X,Y,F,linewidth=0,cmap=plt.cm.cool,norm=norm)
ax.plot3D(xv,yv,fv,'kx')
ax.set_ylim(-1.2,1.1)
ax.set_xlim(-2,0)
ax.set_zlim(-1,1)
ax.plot(xt,0*xt-1,fn(xt,-1))
ax.plot(xt,0*xt-1,tangent(xt))
Out[30]:
[<mpl_toolkits.mplot3d.art3d.Line3D at 0x7f9f110658d0>]

Gradient als Vektorfeld (quiver) Hoehenlinien (contour)

In [31]:
grad = Matrix([f]).jacobian([x,y])
gradf = lambdify((x,y),grad)
GR = gradf(X,Y)[0]
M = np.sqrt(GR[0]**2+GR[1]**2)
plt.figure()
plt.contour(X,Y,F)
plt.quiver(X,Y,GR[0],GR[1])
Out[31]:
<matplotlib.quiver.Quiver at 0x7f9f1028b080>
In [32]:
grad = Matrix([f]).jacobian([x,y])
gradf = lambdify((x,y),grad)
GR = gradf(X,Y)[0]
M = np.sqrt(GR[0]**2+GR[1]**2)
plt.figure()
cs = plt.contour(X,Y,F,np.array([-1,-.8,-.7,-.4,-.1,0,.1]))
plt.clabel(cs)
plt.quiver(X,Y,GR[0],GR[1]);
In [33]:
from matplotlib.colors import Normalize
norm = Normalize(-1,0.2,)
xn = np.linspace(-2,0.3,20)
yn = np.linspace(-1.2,1.2,20)
X,Y = np.meshgrid(xn,yn)
F = fn(X,Y)
fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
ax.plot_surface(X,Y,F,linewidth=0,cmap=plt.cm.cool,norm=norm)
ax.set_ylim(-1.2,1.1)
ax.set_xlim(-2,0)
ax.set_zlim(-1,1)
GR = gradf(X,Y)[0]
nG = np.sqrt(GR[0,:,:]**2 + GR[1,:,:]**2)
ax.quiver(X,Y,F,GR[0,:,:]/nG,GR[1,:,:]/nG,np.sqrt(1+nG**2),length=0.1);

ebene parametrische Plots

In [34]:
tn = np.linspace(-np.pi,np.pi,1000)
In [35]:
plt.figure()
plt.plot(np.cos(7*tn),np.sin(11*tn));
plt.axis('equal')
Out[35]:
$$\left ( -1.0999997527658723, \quad 1.0999948080833182, \quad -1.0999986402114572, \quad 1.0999986402114572\right )$$