{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "#### Dependencies\n", "\n", "Numpy und matplotlib werden benötigt.\n", "Folgende Module sind nur für die interaktive Visualisierung nötig. " ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from ipywidgets import interact, interactive, fixed, interact_manual\n", "import ipywidgets as widgets" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Installation unter Jupyter Notebook: \n", "``` > conda install -c conda-forge ipywidgets ``` \n", "\n", "Installation unter Jupyter Lab: \n", "``` > conda install -c conda-forge nodejs ``` \n", "``` > conda install -c conda-forge ipywidgets ``` \n", "``` > jupyter labextension install @jupyter-widgets/jupyterlab-manager``` " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "\n", "Funktionen aus Übung 2" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "def f(x):\n", " return 3*x + np.e**(-2*x**2) + 3*np.sin(x)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "def df(x):\n", " return 3 + np.e**(-2*x**2)*(-4*x) + 3*np.cos(x)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "def g1(x, y):\n", " return f(x) + x" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "def g2(x, a):\n", " return a*f(x) + x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "\n", "#### Implementation" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "def fpi(start, a=-0.1497299, inverse_zoom = 0.2, iterations = 5):\n", " path = [(start, g2(start, a))]\n", " for _ in range(iterations):\n", " path.append((path[-1][1], path[-1][1]))\n", " path.append((path[-1][1], g2(path[-1][1], a)))\n", " \n", " lengthx = max([x[0] for x in path]) - min([x[0] for x in path])\n", " lengthy = max([x[1] for x in path]) - min([x[1] for x in path])\n", " minboundsx = min([x[0] for x in path]) - inverse_zoom * max(lengthx, lengthy)\n", " maxboundsx = max([x[0] for x in path]) + inverse_zoom * max(lengthx, lengthy)\n", " minboundsy = min([x[1] for x in path]) - inverse_zoom * max(lengthx, lengthy)\n", " maxboundsy = max([x[1] for x in path]) + inverse_zoom * max(lengthx, lengthy)\n", " length = max(lengthy, lengthx)\n", " if lengthx < lengthy:\n", " minboundsx -= (lengthy - lengthy) / 2\n", " maxboundsx += (lengthy - lengthy) / 2\n", " else:\n", " minboundsy -= (lengthx - lengthy) / 2\n", " maxboundsy += (lengthx - lengthy) / 2\n", " \n", " S = np.linspace(minboundsx, maxboundsx, 200)\n", " plt.figure(figsize=(8,8))\n", " plt.plot([minboundsx, maxboundsx], [minboundsx, maxboundsx], ls=\"--\", label=\"Bisection\")\n", " plt.plot(S, [a*f(s)+s for s in S])\n", " plt.xlim((minboundsx, maxboundsx))\n", " plt.ylim((minboundsy, maxboundsy))\n", " for x, y, z in zip(path[::2], path[1::2], path[2::2]):\n", " plt.plot([x[0], y[0]], [x[1], y[1]], c=\"green\")\n", " plt.plot([y[0], z[0]], [y[1], z[1]], c=\"green\", ls=\"--\")\n", " plt.annotate(\"start\", (path[0][0]+0.01*length, path[0][1]+0.01*length))\n", " plt.show()" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "def newton(start, f, df, iterations = 5, inverse_zoom = 0.2):\n", " X = []\n", " def x(i):\n", " if i == 0:\n", " X.append((start, f(start)))\n", " return start\n", " xi = x(i-1)\n", " xi = xi - (f(xi)/df(xi))\n", " X.append((xi, 0))\n", " X.append((xi, f(xi)))\n", " return xi\n", " x(iterations)\n", " \n", " lengthx = max([x[0] for x in X]) - min([x[0] for x in X])\n", " lengthy = max([x[1] for x in X]) - min([x[1] for x in X])\n", " minboundsx = min([x[0] for x in X]) - inverse_zoom*lengthx\n", " maxboundsx = max([x[0] for x in X]) + inverse_zoom*lengthx\n", " minboundsy = min([x[1] for x in X]) - inverse_zoom*lengthy\n", " maxboundsy = max([x[1] for x in X]) + inverse_zoom*lengthy\n", " length = max(lengthy, lengthx)\n", " \n", " \n", " S = np.linspace(minboundsx, maxboundsx, 200)\n", " plt.figure(figsize=(8,8))\n", " plt.axhline(0, ls='--')\n", " plt.plot(S, [f(s) for s in S], c=\"red\")\n", " plt.xlim((minboundsx, maxboundsx))\n", " plt.ylim((minboundsy, maxboundsy))\n", " for x, y, z in zip(X[::2], X[1::2], X[2::2]):\n", " plt.plot([x[0], y[0]], [x[1], y[1]], c=\"green\")\n", " plt.plot([y[0], z[0]], [y[1], z[1]], c=\"green\", ls=\"--\")\n", " plt.annotate(\"start\", (X[0][0]+0.01*length, X[0][1]+0.01*length))\n", " plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "### Interaktiver Bereich" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "FPI für die Funktion aus Übung 2" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "84c70b00cc1f46ca9a6591d1da23443b", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(FloatSlider(value=0.050000000000000044, description='start', max=2.05, min=-1.95), Float…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "_ = interact(fpi, start=(-1.95, 2.05), a=(-1.05, 1.05), inverse_zoom=(0., 2.), iterations = (1, 20))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "##### Newton Verfahren für f(x) = sin(x)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "a08335ed04294df090e3e08ef7068ce0", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(FloatSlider(value=0.050000000000000044, description='start', max=2.05, min=-1.95), IntSl…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "_ = interact(newton, start=(-1.95, 2.05, 0.1), inverse_zoom = (0., 2.), f=fixed(lambda x: np.sin(2*x)), df = fixed(lambda x: 2*np.cos(2*x)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "##### Newton Verfahren für Funktion aus Übung 2" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "ebddc9e54f03454dbfe134226908ffd2", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(FloatSlider(value=0.050000000000000044, description='start', max=2.05, min=-1.95), IntSl…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "_ = interact(newton, start=(-1.95, 2.05), inverse_zoom = (0., 2.), f=fixed(f), df = fixed(df))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "##### Newton Verfahren für f(x) = tan(x)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "b624bcd15b7441508faaf9c37eeeab10", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(FloatSlider(value=0.050000000000000044, description='start', max=2.05, min=-1.95), IntSl…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "_ = interact(newton, start=(-1.95, 2.05), inverse_zoom = (0., 2.), f=fixed(lambda x: np.tan(x)), df = fixed(lambda x: (1/np.cos(x))**2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "##### Anwendung der Newton Methode für eigene Funktionen\n", "Führe folgende Zeile aus. Ersetze dabei START_STELLE durch den Bereich, an welchem sich die Startstelle befinden kann (format: (anfang, ende, schrittweite)), FUNKTION durch eine Referenz auf eine eigene Funktion und ABLEITUNG auf deren Ableitung (letzteres ist nötig, da das Program die Ableitung nicht selbst bestimmen kann). \n", "``` _ = interact(newton, start=START_STELLE, inverse_zoom = (0., 2.), f=fixed(FUNKTION), df = fixed(ABLEITUNG)) ```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "_made by Lennart Mischnaewski and Dennis Hoebelt_" ] } ], "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.7.6" } }, "nbformat": 4, "nbformat_minor": 4 }