Add jypiter notebook to serie1

This commit is contained in:
Hussein Kabbout 2021-02-23 15:19:18 +01:00
parent 69c9d4d465
commit d302bcbf87

294
serie1/flaechen.ipynb Normal file
View File

@ -0,0 +1,294 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Zweidimensionale Flächen darstellen\n",
"\n",
"In diesem Tutorial wollen wir uns damit beschäftigen, wie wir Flächen darstellen können. Eine zweidimensionale Fläche ist zum Beispiel durch den Graphen einer Funktion $f\\colon \\mathbb{R}^2 \\to \\mathbb{R}$ gegeben. Wir definieren uns also zuerst eine Funktion $f(x,y) = x^2 + y^2$.\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def f(x,y):\n",
" return x**2 + y**2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Nun berechnen wir die Koordinaten für eine Menge von Stützpunkten. Dazu legen wir mit dem Befehl `numpy.meshgrid()` ein Gitter in die x-y-Ebene und berechnen anschliessend für jeden Punkt den Funktionswert $z=f(x,y)$."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"[x,y] = np.meshgrid(np.linspace(-5,5), np.linspace(-5,5));\n",
"z = f(x,y)\n",
"\n",
"print(np.shape(x))\n",
"print(np.shape(y))\n",
"print(np.shape(z))\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Wir sehen, dass x, y und z hier jeweils 50x50 Matrizen sind. Für jedes $x\\in[-5,5]$ und $y\\in[-5,5]$ steht der entsprechende Funktionswert $z=f(x,y)$ in dieser Matrix z, die zugehörige $x$-Koordinate in der Matrix x resp. die zugehörige $y$-Koordinate in der Matrix y. Nun stehen uns in Python verschiedene Möglichkeiten zu Verfügung, um diese Fläche $z$ (resp. die Matrix z) zu visualisieren. Wir schauen uns hier mal eine kleine Auswahl an."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Höhenlinien\n",
"\n",
"Die uns bekannte Bibliothek `matplotlib` stellt die Funktion `contour` zur Verfügung, welche die Höhenlinien der Fläche einzuzeichnet. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"\n",
"plt.contour(x, y, z)\n",
"\n",
"plt.title('Höhenlinien')\n",
"plt.xlabel('x')\n",
"plt.ylabel('y')\n",
"\n",
"plt.show()\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Wir können auch eine andere Einfärbung wählen und dazu eine Legende generieren."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from matplotlib import cm\n",
"\n",
"fig = plt.figure(0)\n",
"cont = plt.contour(x, y, z, cmap=cm.coolwarm)\n",
"fig.colorbar(cont, shrink=0.5, aspect=5)\n",
"\n",
"plt.title('Höhenlinien')\n",
"plt.xlabel('x')\n",
"plt.ylabel('y')\n",
"\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Auch die Niveaus können selber ausgewähl werden."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fig = plt.figure(1)\n",
"cont = plt.contour(x, y, z, [1,4,9,16,25,36,49,64,81,100,121,144,169],cmap=cm.coolwarm)\n",
"fig.colorbar(cont, shrink=0.5, aspect=5)\n",
"\n",
"plt.title('Höhenlinien')\n",
"plt.xlabel('x')\n",
"plt.ylabel('y')\n",
"\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Oberfläche im 3D\n",
"\n",
"Nun wollen wir die Fläche im dreidimensionalen Raum darstellen. Dazu verwenden wir das Paket `mpl_toolkits.mplot3d`, welches uns eine interaktive Ansicht bietet. Dazu müssen wir hier in jupyter noch mit dem Befehlt `%matplotlib notebook` die Einstellungen anpassen."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib notebook\n",
"from mpl_toolkits.mplot3d import Axes3D\n",
"\n",
"fig = plt.figure(2)\n",
"ax = fig.add_subplot(111, projection='3d')\n",
"ax.plot_surface(x,y,z)\n",
"\n",
"plt.title('Fläche')\n",
"ax.set_xlabel('x')\n",
"ax.set_ylabel('y')\n",
"ax.set_zlabel('z')\n",
"\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Mit der Option `projection` wählen wir den dreidimensionalen Plot aus. Auch hier können wir wieder mit der Einfärbung spielen."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fig = plt.figure(3)\n",
"ax = fig.add_subplot(111, projection='3d')\n",
"surf = ax.plot_surface(x,y,z, cmap=cm.coolwarm, linewidth=0, antialiased=False)\n",
"\n",
"fig.colorbar(surf, shrink=0.5, aspect=5)\n",
"\n",
"plt.title('Fläche')\n",
"ax.set_xlabel('x')\n",
"ax.set_ylabel('y')\n",
"ax.set_zlabel('z')\n",
"\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Um den Durchblick zu behalten, können wir alternativ auch nur ein Gitter plotten."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fig = plt.figure(4)\n",
"ax = fig.add_subplot(111, projection='3d')\n",
"ax.plot_wireframe(x,y,z)\n",
"\n",
"plt.title('Gitter')\n",
"ax.set_xlabel('x')\n",
"ax.set_ylabel('y')\n",
"ax.set_zlabel('z')\n",
"\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Dabei lässt sich natürlich der Abstand zwischen den Linien einstellen."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fig = plt.figure(5)\n",
"ax = fig.add_subplot(111, projection='3d')\n",
"ax.plot_wireframe(x,y,z, rstride=5, cstride=5)\n",
"\n",
"plt.title('Gitter')\n",
"ax.set_xlabel('x')\n",
"ax.set_ylabel('y')\n",
"ax.set_zlabel('z')\n",
"\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Zum Schluss zeichnen wir auch noch die Höhenlinien im dreidimensionalen Raum ein."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fig = plt.figure(6)\n",
"ax = fig.add_subplot(111, projection='3d')\n",
"surf = ax.contour(x,y,z, cmap=cm.coolwarm)\n",
"\n",
"fig.colorbar(surf, shrink=0.5, aspect=5)\n",
"\n",
"plt.title('Fläche')\n",
"ax.set_xlabel('x')\n",
"ax.set_ylabel('y')\n",
"ax.set_zlabel('z')\n",
"\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}