diff options
Diffstat (limited to 'tutorials/module_3/1_numerical_differentiation.md')
| -rw-r--r-- | tutorials/module_3/1_numerical_differentiation.md | 49 |
1 files changed, 24 insertions, 25 deletions
diff --git a/tutorials/module_3/1_numerical_differentiation.md b/tutorials/module_3/1_numerical_differentiation.md index b34b315..aa45c3a 100644 --- a/tutorials/module_3/1_numerical_differentiation.md +++ b/tutorials/module_3/1_numerical_differentiation.md @@ -1,29 +1,35 @@ # Numerical Differentiation -Finding a derivative of tabular data can be done using a finite difference. Here we essentially pick two points on a function or a set of data points and calculate the slope from there. Let's imagine a domain $x$ as a vector such that $\vec{x}$ = $\pmatrix{x_0, x_1, x_2, ...}$. Then we can use the following methods to approximate derivatives +Finding a derivative of tabular data can be done using a finite difference. Here we essentially pick two points on a function or a set of data points and calculate the slope from there. You may have done this before in spreadsheets, we're going to do this using python. Let's imagine a time range $t$ as a vector such that $\vec{t}$ = $\pmatrix{t_0, t_1, t_2, ...}$ and a displacement domain as a function of time. We can represent the range and domain as two python arrays `t` and `s` respectively. + +```python +import numpy as np + +# Initiate time domain +t = np.linspace(0, 2, 100) +s = 34 * np.exp(3 * t) +``` + +Then we can use the following methods to approximate the definitive derivatives as follows. ## Forward Difference -Uses the point at which we want to find the derivative and a point forwards on the line. +Uses the point at which we want to find the derivative and a point forwards in the array. $$ f'(x_i) = \frac{f(x_{i+1})-f(x_i)}{x_{i+1}-x_i} $$ -*Hint: Consider what happens at the last point.* +*Note: If we apply this to an array, consider what happens at the last point.* ```python -import numpy as np -import matplotlib.pyplot as plt - -# Initiate vectors -x = np.linspace(0, 2, 100) -y = 34 * np.exp(3 * x) +# Forward difference using python arrays +dsdt = (y[1:] - y[:-1]) / (x[1:] - x[:-1]) -dydx = (y[1:] - y[:-1]) / (x[1:] - x[:-1]) +import matplotlib.pyplot as plt # Plot the function -plt.plot(x, y, label=r'$y(x)$') -plt.plot(x, dydx, label=b'$/frac{dy}{dx}$') -plt.xlabel('x') -plt.ylabel('y') -plt.title('Plot of $34e^{3x}$') +plt.plot(x, s, label=r'$y(x)$') +plt.plot(x, dsdt, label=b'$/frac{ds}{dt}$') +plt.xlabel('Time (t)') +plt.ylabel('Displacement (s)') +plt.title('Plot of $34e^{3t}$') plt.grid(True) plt.legend() plt.show() @@ -31,25 +37,18 @@ plt.show() ## Backwards Difference -Uses the point at which we want to find +Uses the point at which we want to find and the previous point in the array. $$ f'(x_i) = \frac{f(x_{i})-f(x_{i-1})}{x_i - x_{i-1}} $$ ```python -import numpy as np -import matplotlib.pyplot as plt - -# Initiate vectors -x = np.linspace(0, 2, 100) -y = 34 * np.exp(3 * x) - -dydx = (y[1:] - y[:-1]) / (x[1:] - x[:-1]) +dsdt = (y[1:] - y[:-1]) / (x[1:] - x[:-1]) # Plot the function plt.plot(x, y, label=r'$y(x)$') -plt.plot(x, dydx, label=b'$/frac{dy}{dx}$') +plt.plot(x, dydx, label=b'$/frac{ds}{dt}$') plt.xlabel('x') plt.ylabel('y') plt.title('Plot of $34e^{3x}$') |
