{ "cells": [ { "cell_type": "markdown", "id": "d66e2b4b-047b-4670-897d-7877d27a1597", "metadata": {}, "source": [ "# Numerical Differentiation\n", "\n", "Finding a derivative of tabular data can be done using a finite\n", "difference. Here we essentially pick two points on a function or a set\n", "of data points and calculate the slope from there. Let’s imagine a\n", "domain $x$ as a vector such that $\\vec{x}$ =\n", "$\\pmatrix{x_0, x_1, x_2, ...}$. Then we can use the following methods to\n", "approximate derivatives\n", "\n", "## Forward Difference\n", "\n", "Uses the point at which we want to find the derivative and a point\n", "forwards on the line. $$\n", "f'(x_i) = \\frac{f(x_{i+1})-f(x_i)}{x_{i+1}-x_i}\n", "$$ *Hint: Consider what happens at the last point.*" ] }, { "cell_type": "code", "execution_count": 2, "id": "d2fe3ad1-8af0-499a-8954-41a88a49b834", "metadata": {}, "outputs": [ { "ename": "IndexError", "evalue": "index 99 is out of bounds for axis 0 with size 99", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[2], line 10\u001b[0m\n\u001b[1;32m 6\u001b[0m y \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m34\u001b[39m \u001b[38;5;241m*\u001b[39m np\u001b[38;5;241m.\u001b[39mexp(\u001b[38;5;241m3\u001b[39m \u001b[38;5;241m*\u001b[39m x)\n\u001b[1;32m 8\u001b[0m dydx \u001b[38;5;241m=\u001b[39m (y[\u001b[38;5;241m1\u001b[39m:] \u001b[38;5;241m-\u001b[39m y[:\u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m]) \u001b[38;5;241m/\u001b[39m (x[\u001b[38;5;241m1\u001b[39m:] \u001b[38;5;241m-\u001b[39m x[:\u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m])\n\u001b[0;32m---> 10\u001b[0m dydx[\u001b[38;5;28mlen\u001b[39m(y)]\u001b[38;5;241m=\u001b[39mdydx[\u001b[38;5;28mlen\u001b[39m(dydx)]\n\u001b[1;32m 12\u001b[0m \u001b[38;5;66;03m# Plot the function\u001b[39;00m\n\u001b[1;32m 13\u001b[0m plt\u001b[38;5;241m.\u001b[39mplot(x, y, label\u001b[38;5;241m=\u001b[39m\u001b[38;5;124mr\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m$y(x)$\u001b[39m\u001b[38;5;124m'\u001b[39m)\n", "\u001b[0;31mIndexError\u001b[0m: index 99 is out of bounds for axis 0 with size 99" ] } ], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "\n", "# Initiate vectors\n", "x = np.linspace(0, 2, 100) \n", "y = 34 * np.exp(3 * x)\n", "\n", "dydx = (y[1:] - y[:-1]) / (x[1:] - x[:-1])\n", "\n", "dydx[len(y)]=dydx[len(dydx)]\n", "\n", "# Plot the function\n", "plt.plot(x, y, label=r'$y(x)$')\n", "plt.plot(x, dydx, label=r'$\\frac{dy}{dx}$')\n", "plt.xlabel('x')\n", "plt.ylabel('y')\n", "plt.title('Plot of $34e^{3x}$')\n", "plt.grid(True)\n", "plt.legend()\n", "plt.show()" ] }, { "cell_type": "markdown", "id": "a54ad04f-05e5-4589-a633-d6dea2a5acfc", "metadata": {}, "source": [ "## Backwards Difference\n", "\n", "Uses the point at which we want to find $$\n", "f'(x_i) = \\frac{f(x_{i})-f(x_{i-1})}{x_i - x_{i-1}}\n", "$$" ] }, { "cell_type": "code", "execution_count": null, "id": "415d0b3c-a70d-48ef-93ad-6e827127b222", "metadata": {}, "outputs": [], "source": [ "dydx = (y[1:] - y[:-1]) / (x[1:] - x[:-1])\n", "\n", "# Plot the function\n", "plt.plot(x, y, label=r'$y(x)$')\n", "plt.plot(x, dydx, label=b'$/frac{dy}{dx}$')\n", "plt.xlabel('x')\n", "plt.ylabel('y')\n", "plt.title('Plot of $34e^{3x}$')\n", "plt.grid(True)\n", "plt.legend()\n", "plt.show()" ] }, { "cell_type": "markdown", "id": "21c87595-add2-4f7a-8d10-2da3b4284e02", "metadata": {}, "source": [ "## Central Difference\n", "\n", "$$\n", "f'(x_i) = \\frac{f(x_{i+1})-f(x_{i-1})}{x_{i+1}-x_{i-1}}\n", "$$" ] }, { "cell_type": "code", "execution_count": null, "id": "6a67e63c-9516-415d-87d6-97d9d986ba33", "metadata": {}, "outputs": [], "source": [] } ], "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.13.2" } }, "nbformat": 4, "nbformat_minor": 5 }