summaryrefslogtreecommitdiff
path: root/tutorials/module_3/notebook_3/numerical_differentiation.ipynb
blob: 08925540228a195d0ff3019df1e0f84ea724478e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
{
 "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
}