{ "cells": [ { "cell_type": "markdown", "id": "074684ec", "metadata": {}, "source": [ "## Einige Hinweise zur Differentialrechnung am Computer" ] }, { "cell_type": "markdown", "id": "d7924e2c", "metadata": {}, "source": [ "*Notebook erstellt am 03.09.2022 von C. Rockstuhl, überarbeitet von Y. Augenstein* \n", "\n", "In der Erarbeitung habe ich mich an den Beispielen orientiert, die im Skript *Einführung in die\n", "mathematische Behandlung der Naturwissenschaften* von Prof. Dr. H. Ebert enthalten sind.\n", "\n", "Dieses Notebook soll in aller Kürze zeigen, wie einige Differenzialrechnungen effektiv am Computer durchgeführt werden können." ] }, { "cell_type": "markdown", "id": "2e0b5d45", "metadata": {}, "source": [ "Eine klassische Bibliothek, welche das symbolische Rechnen in Python erlaubt, ist `sympy`. Diese stellt viele Routinen bereit. Eine Übersicht über die Funktionalität finden Sie z.B. hier Overview over Sympy. Beachten Sie bitte, für symbolisches Rechnen können Sie auch Programmpakete wie Mathematica oder Maple verwenden. \n", "\n", "Wir beginnen als Erstes mit dem Importieren von Funktionen und wir generieren uns einige Symbole, mit denen wir im Folgenden symoblisches Rechnen durchführen:" ] }, { "cell_type": "code", "execution_count": 1, "id": "d6755bdf", "metadata": {}, "outputs": [], "source": [ "from sympy import limit, sin, cos, tan, exp, log\n", " # Wir importieren hier wieder als Erstes aus der erwähnten Bibliothek sympy eine\n", " # Reihe von Elementarfunktionen, mit denen wir dann später rechnen können.\n", "\n", "from sympy.abc import x, d, n # Wir müssen auch einige Variablen definieren, mit denen wir dann symbolisch\n", " # arbeiten wollen.\n", " # Im Speziellen benötigen wir x, weil unsere Funktionen üblicherweise von x\n", " # abhängen, d als eine kleine Größe die wir dann für die Grenzwertbildung\n", " # benötigen und abschließend n, welches wir als eine Potenz benötigen." ] }, { "cell_type": "markdown", "id": "4f29f737", "metadata": {}, "source": [ "Anschliessend berechnen wir die Ableitung einer Funktion als Grenzwert eines Differenzenquotienten. Diesen haben wir in der Vorlesung kennengelernt und würden den also gerade definieren als\n", "$$\n", "\\frac{\\mathrm{d}f}{\\mathrm{d}x}(x)=\\lim_{\\Delta x\\rightarrow 0}=\\frac{f(x+\\Delta x)-f(x)}{\\Delta x}\n", "$$\n", "und genau diese Größe würden wir berechnen lassen. Zunächst einmal nur am einfachen Beispiel von $f(x)=\\sin(x)$.\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "9079294f", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\cos{\\left(x \\right)}$" ], "text/plain": [ "cos(x)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "limit((sin(x + d) - sin(x)) / d, d, 0) # Die Funktion limit berechnet uns einfach den Grenzwert. Das erste Argument\n", " # ist die Funktion, dessen Grenzwert evaluiert werden soll. Und das zweite\n", " # und dritte Argument geben dann Information über die Variabel, deren\n", " # Grenzwert betrachtet werden soll." ] }, { "cell_type": "markdown", "id": "472a516f", "metadata": {}, "source": [ "Das Ergebnis entspricht gerade genau unserer Erwartung, d.h., $f'(x)=\\cos(x)$. Wir können das auch mit anderen Funktionen erproben. " ] }, { "cell_type": "code", "execution_count": 3, "id": "71a3ba77", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle e^{x}$" ], "text/plain": [ "exp(x)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "limit((exp(x + d) - exp(x)) / d, d, 0)" ] }, { "cell_type": "code", "execution_count": 4, "id": "6d74e303", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle n x^{n - 1}$" ], "text/plain": [ "n*x**(n - 1)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "limit(((x + d) ** n - x**n) / d, d, 0)" ] }, { "cell_type": "markdown", "id": "b75b0bec", "metadata": {}, "source": [ "Die folgenden Beispiele sind etwas komplexer, so dass wir das Programm forcieren müssen, eine einfache Formulierung für die entsprechenden Ausdrücke zu finden. Daher würden wir aus der Bibliothek sympy die Funktion simplify importieren, die die entsprechenden Ausdrücke vereinfachen soll." ] }, { "cell_type": "code", "execution_count": 5, "id": "c2d9f6e6", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\frac{1}{\\cos^{2}{\\left(x \\right)}}$" ], "text/plain": [ "cos(x)**(-2)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from sympy import simplify\n", "\n", "simplify(limit((tan(x + d) - tan(x)) / d, d, 0))" ] }, { "cell_type": "code", "execution_count": 6, "id": "53d661bb", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\frac{1}{\\cos^{2}{\\left(x \\right)}}$" ], "text/plain": [ "cos(x)**(-2)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "simplify(limit((sin(x + d) / cos(x + d) - sin(x) / cos(x)) / d, d, 0))" ] }, { "cell_type": "markdown", "id": "16369fc5", "metadata": {}, "source": [ "Wem diese Grenzwertbildung zu anstrengend ist, kann Funktionen mittels sympy auch direkt differenzieren. Wir müssen die entsprechende Funktion aus der Bibliothek einbinden." ] }, { "cell_type": "code", "execution_count": 7, "id": "6243ef4a", "metadata": {}, "outputs": [], "source": [ "from sympy import diff" ] }, { "cell_type": "markdown", "id": "8bc876c4", "metadata": {}, "source": [ "und können dann direkt einige sehr ähnliche Beispiele lösen." ] }, { "cell_type": "code", "execution_count": 8, "id": "121de7ec", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\cos{\\left(x \\right)}$" ], "text/plain": [ "cos(x)" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "diff(sin(x), x)" ] }, { "cell_type": "code", "execution_count": 9, "id": "42440e4d", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle n x^{n - 1}$" ], "text/plain": [ "n*x**(n - 1)" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "simplify(diff(x**n, x))" ] }, { "cell_type": "markdown", "id": "20733b42", "metadata": {}, "source": [ "Wir können auch mit Funktionen rechnen, ohne diese bereits explizit spezifizieren zu müssen. Damit können wir einfache Rechenregeln der Differenziation überprüfen." ] }, { "cell_type": "code", "execution_count": 10, "id": "ced38f25", "metadata": {}, "outputs": [], "source": [ "from sympy import Function\n", "from sympy.abc import x\n", "\n", "f = Function(\"f\")(x) # Hier definieren wir uns einfach eine allgemeine Funktion f die von x abhängt.\n", "g = Function(\"g\")(x) # Hier definieren wir uns einfach eine allgemeine Funktion f die von x abhängt." ] }, { "cell_type": "markdown", "id": "bf19f70c", "metadata": {}, "source": [ "Hier hätten wir die Summenregel:" ] }, { "cell_type": "code", "execution_count": 11, "id": "4de1d4a8", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\frac{d}{d x} f{\\left(x \\right)} + \\frac{d}{d x} g{\\left(x \\right)}$" ], "text/plain": [ "Derivative(f(x), x) + Derivative(g(x), x)" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "diff(f + g, x)" ] }, { "cell_type": "markdown", "id": "bda21db0", "metadata": {}, "source": [ "Hier hätten wir die Produktregel:" ] }, { "cell_type": "code", "execution_count": 12, "id": "e2ee0923", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle f{\\left(x \\right)} \\frac{d}{d x} g{\\left(x \\right)} + g{\\left(x \\right)} \\frac{d}{d x} f{\\left(x \\right)}$" ], "text/plain": [ "f(x)*Derivative(g(x), x) + g(x)*Derivative(f(x), x)" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "diff(f * g, x)" ] }, { "cell_type": "markdown", "id": "13551e17", "metadata": {}, "source": [ "Die Quotientenregel:" ] }, { "cell_type": "code", "execution_count": 13, "id": "107ac151", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\frac{- f{\\left(x \\right)} \\frac{d}{d x} g{\\left(x \\right)} + g{\\left(x \\right)} \\frac{d}{d x} f{\\left(x \\right)}}{g^{2}{\\left(x \\right)}}$" ], "text/plain": [ "(-f(x)*Derivative(g(x), x) + g(x)*Derivative(f(x), x))/g(x)**2" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "simplify(diff((f / g), x))" ] }, { "cell_type": "markdown", "id": "1f580932", "metadata": {}, "source": [ "Oder auch die Kettenregel:" ] }, { "cell_type": "code", "execution_count": 14, "id": "a1c44953", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\frac{d}{d g{\\left(x \\right)}} f{\\left(g{\\left(x \\right)} \\right)} \\frac{d}{d x} g{\\left(x \\right)}$" ], "text/plain": [ "Derivative(f(g(x)), g(x))*Derivative(g(x), x)" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f = Function(\"f\")(g)\n", "diff(f, x)" ] }, { "cell_type": "markdown", "id": "61f1ee64", "metadata": {}, "source": [ "Wir können auch einfach Ableitungen höherer Ordnung berechnen." ] }, { "cell_type": "code", "execution_count": 15, "id": "12a7d903", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\cos{\\left(x \\right)}$" ], "text/plain": [ "cos(x)" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "diff(sin(x), x)" ] }, { "cell_type": "code", "execution_count": 16, "id": "94e1a385", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle - \\sin{\\left(x \\right)}$" ], "text/plain": [ "-sin(x)" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "diff(diff(sin(x), x), x)" ] }, { "cell_type": "code", "execution_count": 17, "id": "f4dae7ca", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle - \\sin{\\left(x \\right)}$" ], "text/plain": [ "-sin(x)" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "diff(sin(x), x, 2)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.12" }, "vscode": { "interpreter": { "hash": "77c156d20ad9e0a33269d50c8fafe7044ce4815250dfaf90496dfc42f56ab107" } } }, "nbformat": 4, "nbformat_minor": 5 }