added helpers

This commit is contained in:
navid.sassan 2021-02-23 16:50:39 +01:00
parent d302bcbf87
commit 2f8e72adfb
2 changed files with 109 additions and 0 deletions

44
helpers/kap5_bsp_1.py Normal file
View File

@ -0,0 +1,44 @@
"""
Einleitendes Beispiel:
(Implizite) Graphische Darstellung f1(x1,x2) = 0 und f2(x1,x2) = 0
mit Hilfe eines Hoehenlinien-Plots (sog. Contour-Plots)
"""
# Definition der Funktionen f1, f2 : R^2 -> R
def f1(x1,x2):
return x1**2 + x2 - 11
def f2(x1,x2):
return x1 + x2**2 - 7
"""
(Implizite) Graphische Darstellung f1(x1,x2) = 0 und f2(x1,x2) = 0
mit Hilfe eines Hoehenlinien-Plots (sog. Contour-Plots), wobei
nur die "Nullline" dargestellt wird.
Dazu wird in folgenden Schritten vorgegangen:
"""
# 1. Erstellen eines 2d-Gitters in der x1,x2-Ebene mit Hifle von
# numpy.meshgrid (s. numpy-Doku)
import numpy as np
x1 = np.linspace(-6,6) # aequidistante Stuetzstellen in x1-Richtung
x2 = np.linspace(-5,12) # aequidistante Stuetzstellen in x2-Richtung
x1, x2 = np.meshgrid(x1,x2) # 2d-Gitter von Stuetzstellen
# 2. Berechnen der Funktionswerte an den Stuetzstellen
z1 = f1(x1,x2)
z2 = f2(x1,x2)
# 3. Erstellen des Countour-Plots (s. matplotlib-Doku)
import matplotlib.pyplot as plt
plt.contour(x1,x2,z1,levels=[0])
plt.contour(x1,x2,z2,levels=[0])
plt.xlabel('x1')
plt.ylabel('x2')
plt.show()

65
helpers/kap5_bsp_2.py Normal file
View File

@ -0,0 +1,65 @@
"""
Einleitendes Beispiel:
Graphische Darstellung von funktionen f : R^2 -> R als
a) Hoehenlinien in der Ebene (R^2), oder
b) Flaeche im Raum (R^3)
"""
# Definition der Funktionen f1 : R^2 -> R
def f(x,y):
return x**2 + y - 11
"""
Graphische Darstellung von funktionen f : R^2 -> R als
a) Hoehenlinien in der Ebene (R^2), oder
b) Flaeche im Raum (R^3)
Dazu wird in folgenden Schritten vorgegangen:
"""
# 1. Erstellen eines 2d-Gitters in der x,y-Ebene mit Hifle von
# numpy.meshgrid (s. numpy-Doku)
import numpy as np
x = np.linspace(-5, 5, 50) # aequidistante Stuetzstellen in x-Richtung
y = np.linspace(-5, 5, 50) # aequidistante Stuetzstellen in y-Richtung
x, y = np.meshgrid(x, y) # 2d-Gitter von Stuetzstellen in x,y-Ebene
# 2. Berechnen der Funktionswerte an den Stuetzstellen
z = f(x, y)
# 3. Erstellen des gewuenschten Plots
# a) Hoehenlinen-Plot (Contour-Plot)
import matplotlib.pyplot as plt
from matplotlib import cm # Verwendeung anderer Farbtabelle (colormap)
plt.figure()
cont = plt.contour(x, y, z, cmap=cm.coolwarm)
plt.clabel(cont) # Fuegt Hoehenlinien-Label hinzu
# b) Flache in 3d (s. mplot3d tutorial der matplotlib-Doku)
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig2 = plt.figure()
ax = fig2.add_subplot(111, projection='3d')
ax.plot_surface(x, y, z, cmap=cm.coolwarm)
# Optional: inklusive Hoehenlinien in x,y-Ebene
ax.contour(x, y, z, zdir='z', offset=-20, cmap=cm.coolwarm)
ax.set_zlim(-20, )
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
#plt.show()