diff options
| author | Christian Kolset <christian.kolset@gmail.com> | 2025-10-01 14:04:14 -0600 |
|---|---|---|
| committer | Christian Kolset <christian.kolset@gmail.com> | 2025-10-01 14:04:14 -0600 |
| commit | b7652c078a74ec0fd8419c4e0d8f9dc1d7b28020 (patch) | |
| tree | 436a9952db1fb6f47fb9b6a15e40b9801f925f20 /tutorials/module_3/1_numerical_differentiation.md | |
| parent | 0678159d2801c99437e728823dfccccb9d608174 (diff) | |
Re-structure of Module 3 to make each file a seperate lecture
Diffstat (limited to 'tutorials/module_3/1_numerical_differentiation.md')
| -rw-r--r-- | tutorials/module_3/1_numerical_differentiation.md | 101 |
1 files changed, 0 insertions, 101 deletions
diff --git a/tutorials/module_3/1_numerical_differentiation.md b/tutorials/module_3/1_numerical_differentiation.md deleted file mode 100644 index 441f838..0000000 --- a/tutorials/module_3/1_numerical_differentiation.md +++ /dev/null @@ -1,101 +0,0 @@ -# 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. 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. - -## Finite Difference -### Forward Difference -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} -$$ -*Note: If we apply this to an array, consider what happens at the last point.* - -```python -for s in t: - ds_dt = f(x+) -``` - - - -```python -# Forward difference using python arrays -dsdt = (s[1:] - s[:-1]) / (t[1:] - t[:-1]) - -import matplotlib.pyplot as plt - -# Plot the function -plt.plot(t, s, label=r'$s(t)$') -plt.plot(t, 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() -``` - - -### Backwards Difference -Uses the point at which we want to find the derivative and the previous point in the array. -$$ -f'(x_i) = \frac{f(x_{i})-f(x_{i-1})}{x_i - x_{i-1}} -$$ - - -```python -dsdt = (s[1:] - s[:-1]) / (t[1:] - t[:-1]) - -# Plot the function -plt.plot(t, s, label=r'$s(t)$') -plt.plot(t, dydx, 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() -``` -Try plotting both forward and backwards - -### Central Difference -$$ -f'(x_i) = \frac{f(x_{i+1})-f(x_{i-1})}{x_{i+1}-x_{i-1}} -$$ -### Problem 1 -Use the forward difference formula to approximate the derivative of $f(x)$ at $x = 1$ using step sizes: $h=0.5$ and $h=0.1$ for the following function. -$$ -f(x) = \ln(x^2 + 1) -$$ -Compare your results with the analytical solution at $x=1$. Comment on how the choice of $h$ affects the accuracy. -```python - -``` -### Problem 2 -Use the central difference formula to approximate the derivative of $f(x)$ at $x = 1.2$ using step sizes: $h=0.5$ and $h=0.1$ for the following function. -$$ -f(x) = e^{-x^2} -$$ -Compare your results with the analytical solution at $x=1.2$. Comment on how the choice of $h$ affects the accuracy. -```python - -``` - - ---- -# Advanced Derivatives - -## High-Accuracy Differentiation Formulas -## Richardson Extrapolation -## Derivative of Unequally Spaced Data -## Partial Derivatives - |
