Finite Difference Approximation
Using a one-sided first-order approximation for the time derivative (linking $f_j^n$ and $f_j^{n+1}$) and second-order centered approximations for the spatial derivatives
\[
\left.\frac{\partial f}{\partial t}\right)_j^n \approx \frac{f_j^{n+1} - f_j^n}{\Delta t}; \quad
\left.\frac{\partial f}{\partial x}\right)_j^n \approx \frac{f_{j+1} - f_{j-1}}{2\Delta x}; \quad
\left.\frac{\partial^2 f}{\partial x^2}\right)_j^n \approx \frac{f_{j+1} - 2f_j + f_{j-1}}{\Delta x^2}
\]
The discrete approximation of the advection-diffusion equation becomes
$$
\frac{f_j^{n+1} - f_j^n}{\Delta t}
+ U \left(\frac{f_{j+1}^n - f_{j-1}^n}{2\Delta x}\right)
= D \left(\frac{f_{j+1}^n - 2 f_j^n + f_{j-1}^n}{\Delta x^2}\right)
$$
Given the discrete values of $f$ at one time ($\ldots, f_{j-1}^n, f_j^n, f_{j+1}^n, \ldots$), this equation allows us to update all the $f$’s by computing $f_j^{n+1}$ as a linear combination of $f_{j-1}^n, f_j^n,$ and $f_{j+1}^n$
- The numerical scheme requires two loops (one over time and one over spatial points), along with specified initial and boundary conditions such as
$$
f(1:N-1) = 0, \quad f(N) = 1.0
$$
- For simulating a periodic sine wave, periodic boundary conditions are imposed by substituting $f(2)$ for $f(N+1)$ and setting
$$
f(1) = f(N)
$$
- Since both endpoints are included, the grid spacing is defined as
$$
\Delta x = \frac{L}{N-1}
$$
where $L$ is the length of the domain
◀
▶