From 2f8e72adfb93488e87ae7813b915f06eb366dc3a Mon Sep 17 00:00:00 2001 From: "navid.sassan" Date: Tue, 23 Feb 2021 16:50:39 +0100 Subject: [PATCH] added helpers --- helpers/kap5_bsp_1.py | 44 +++++++++++++++++++++++++++++ helpers/kap5_bsp_2.py | 65 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 helpers/kap5_bsp_1.py create mode 100644 helpers/kap5_bsp_2.py diff --git a/helpers/kap5_bsp_1.py b/helpers/kap5_bsp_1.py new file mode 100644 index 0000000..aeb430a --- /dev/null +++ b/helpers/kap5_bsp_1.py @@ -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() \ No newline at end of file diff --git a/helpers/kap5_bsp_2.py b/helpers/kap5_bsp_2.py new file mode 100644 index 0000000..271bfa1 --- /dev/null +++ b/helpers/kap5_bsp_2.py @@ -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() +