HM2_Serien/helpers/kap5_bsp_2.py
2021-02-23 16:50:39 +01:00

66 lines
1.6 KiB
Python

"""
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()