Lesson 3 of 13 20 min

Finite Difference Basics

The governing equations of fluid mechanics contain derivatives — rates of change like $\partial u / \partial x$. Computers can't handle continuous derivatives directly. They need numbers, not infinitesimals. Finite differences bridge this gap by approximating derivatives using values at discrete points.

This lesson builds the mathematical foundation for numerical discretization. Every CFD solver, whether it uses finite differences, finite volumes, or finite elements, relies on these fundamental concepts.

From Continuous to Discrete

The Basic Idea

A derivative is defined as a limit:

$$\frac{df}{dx} = \lim_{\Delta x \to 0} \frac{f(x + \Delta x) - f(x)}{\Delta x}$$

In numerical methods, we can't take $\Delta x \to 0$ — we stop at some small but finite $\Delta x$:

$$\frac{df}{dx} \approx \frac{f(x + \Delta x) - f(x)}{\Delta x}$$

This is a finite difference approximation. The key questions are:

  • How accurate is this approximation?
  • Can we do better?
  • What happens as $\Delta x$ changes?

Taylor Series: The Foundation

Taylor Series Expansion

The Taylor series expands a function around a point $x_i$:

$$f(x_i + h) = f(x_i) + hf'(x_i) + \frac{h^2}{2!}f''(x_i) + \frac{h^3}{3!}f'''(x_i) + O(h^4)$$

And in the backward direction:

$$f(x_i - h) = f(x_i) - hf'(x_i) + \frac{h^2}{2!}f''(x_i) - \frac{h^3}{3!}f'''(x_i) + O(h^4)$$

Visualize how Taylor series terms build up to approximate a function. Add more terms to improve accuracy.

Deriving Finite Differences

By manipulating the Taylor series, we can extract approximations for derivatives.

Forward difference — Solve for $f'(x_i)$ from the forward expansion:

$$f'(x_i) = \frac{f(x_i + h) - f(x_i)}{h} - \frac{h}{2}f''(x_i) - O(h^2)$$

Dropping higher-order terms:

$$\boxed{f'(x_i) \approx \frac{f_{i+1} - f_i}{h} + O(h)}$$

Backward difference — From the backward expansion:

$$\boxed{f'(x_i) \approx \frac{f_i - f_{i-1}}{h} + O(h)}$$

Central difference — Subtract the backward expansion from the forward:

$$f(x_i + h) - f(x_i - h) = 2hf'(x_i) + \frac{h^3}{3}f'''(x_i) + O(h^5)$$

$$\boxed{f'(x_i) \approx \frac{f_{i+1} - f_{i-1}}{2h} + O(h^2)}$$

Comparing Finite Difference Schemes

Compare forward, backward, and central difference approximations. Adjust grid spacing to see how errors change.

First-Order vs Second-Order Accuracy

SchemeFormulaOrderError
Forward$\frac{f_{i+1} - f_i}{h}$$O(h)$First-order
Backward$\frac{f_i - f_{i-1}}{h}$$O(h)$First-order
Central$\frac{f_{i+1} - f_{i-1}}{2h}$$O(h^2)$Second-order
Order of accuracy tells us how error scales with grid spacing:
  • First-order $O(h)$: Halving $h$ halves the error
  • Second-order $O(h^2)$: Halving $h$ reduces error by factor of 4
  • Fourth-order $O(h^4)$: Halving $h$ reduces error by factor of 16

Why Central Difference is Better

Central difference uses information from both sides of the point, achieving symmetry. This symmetry cancels the even-order error terms, leaving only odd-order terms:

$$\text{Central error} \sim O(h^2) + O(h^4) + ...$$

$$\text{Forward error} \sim O(h) + O(h^2) + ...$$

For the same $h$, central difference is typically an order of magnitude more accurate.

Second Derivatives

For the second derivative, add the forward and backward Taylor expansions:

$$f(x_i + h) + f(x_i - h) = 2f(x_i) + h^2 f''(x_i) + O(h^4)$$

Solving for $f''(x_i)$:

$$\boxed{f''(x_i) \approx \frac{f_{i+1} - 2f_i + f_{i-1}}{h^2} + O(h^2)}$$

This is the standard central difference for second derivative, second-order accurate.

The Laplacian in 2D

The Laplacian $\nabla^2 f = \frac{\partial^2 f}{\partial x^2} + \frac{\partial^2 f}{\partial y^2}$ becomes:

$$\nabla^2 f \approx \frac{f_{i+1,j} - 2f_{i,j} + f_{i-1,j}}{h_x^2} + \frac{f_{i,j+1} - 2f_{i,j} + f_{i,j-1}}{h_y^2}$$

This is the 5-point stencil for the Laplacian on a uniform grid.

Truncation Error Analysis

What is Truncation Error?

When we replace a derivative with a finite difference, we truncate the Taylor series. The truncation error is what we throw away:

$$\text{Exact derivative} = \text{FD approximation} + \text{Truncation error}$$

For central difference:

$$f'(x_i) = \frac{f_{i+1} - f_{i-1}}{2h} + \underbrace{\frac{h^2}{6}f'''(\xi)}_{\text{truncation error}}$$

where $\xi$ is some point between $x_{i-1}$ and $x_{i+1}$.

Factors Affecting Accuracy

  • Grid spacing $h$: Smaller $h$ → smaller truncation error
  • Order of scheme: Higher order → faster convergence
  • Smoothness of solution: Rough solutions require finer grids
  • Grid non-uniformity: Non-uniform grids reduce accuracy

Higher-Order Schemes

Fourth-Order Central Difference

Using more points gives higher accuracy:

$$f'(x_i) \approx \frac{-f_{i+2} + 8f_{i+1} - 8f_{i-1} + f_{i-2}}{12h} + O(h^4)$$

This uses a 5-point stencil and is fourth-order accurate.

Compact Schemes

Compact (Padé) schemes achieve high accuracy with smaller stencils by using implicit relationships:

$$\frac{1}{4}f'_{i-1} + f'_i + \frac{1}{4}f'_{i+1} = \frac{3}{4h}(f_{i+1} - f_{i-1})$$

This is fourth-order accurate but requires solving a tridiagonal system.

Stencils and Computational Molecules

A stencil (or computational molecule) shows which points are used in a finite difference:

Central 1st derivative:
    [-1]  [ 0 ]  [+1]
     o ----x---- o
   i-1    i    i+1

Coefficients: -1/(2h), 0, +1/(2h)

5-point Laplacian:
           [ 1]
            o
            |
   [1]--o---x---o--[1]    Coefficients:
        i-1  i  i+1        1/h² for neighbors
            |              -4/h² for center
            o
           [1]

Stencil Properties

  • Width: Number of points in each direction
  • Symmetry: Symmetric stencils → even-order accuracy
  • One-sided: For boundaries, we need biased stencils

Boundary Treatment

At boundaries, central differences can't be used directly (missing neighboring points).

Options at Boundaries

One-sided differences:

$$f'_0 \approx \frac{-3f_0 + 4f_1 - f_2}{2h} + O(h^2)$$

This is still second-order accurate but uses only forward points.

Ghost points: Extrapolate fictitious points outside the domain using boundary conditions. Special treatment: Apply boundary conditions directly in the discretized equations.

Stability Considerations

The CFL Condition

For time-dependent problems, finite differences must satisfy the Courant-Friedrichs-Lewy (CFL) condition:

$$\text{CFL} = \frac{u \Delta t}{\Delta x} \leq 1$$

If CFL > 1, the numerical solution can become unstable and blow up.

Physical interpretation: Information must not travel more than one cell per time step.

Von Neumann Stability Analysis

For linear problems, we analyze stability by assuming wave-like solutions:

$$f_j^n = G^n e^{ijk\Delta x}$$

where $G$ is the amplification factor. The scheme is stable if $|G| \leq 1$.

Connection to CFD

How Finite Differences Appear in CFD

  • Time integration: $\frac{\partial \phi}{\partial t} \approx \frac{\phi^{n+1} - \phi^n}{\Delta t}$
  • Spatial derivatives in FVM: Finite differences compute gradients at cell faces
  • Diffusion terms: $\nabla^2 \phi$ uses second derivative approximations
  • Higher-order schemes: Central differences plus upwinding corrections

Limitations for CFD

Pure finite differences struggle with:

  • Complex geometries (require structured grids)
  • Conservation laws (not inherently conservative)
  • Irregular boundaries (need special treatment)

This leads to the Finite Volume Method — the subject of our next lesson.

Summary: Finite Difference Formulas

DerivativeFormulaOrderStencil
$f'$ forward$\frac{f_{i+1} - f_i}{h}$$O(h)$$[0, 1]$
$f'$ backward$\frac{f_i - f_{i-1}}{h}$$O(h)$$[-1, 0]$
$f'$ central$\frac{f_{i+1} - f_{i-1}}{2h}$$O(h^2)$$[-1, 0, 1]$
$f''$ central$\frac{f_{i+1} - 2f_i + f_{i-1}}{h^2}$$O(h^2)$$[-1, 0, 1]$
$f'$ 4th-order$\frac{-f_{i+2} + 8f_{i+1} - 8f_{i-1} + f_{i-2}}{12h}$$O(h^4)$$[-2,-1,0,1,2]$

Key Takeaways

  • Taylor series is the foundation for deriving all finite difference formulas
  • Central differences are more accurate than forward/backward ($O(h^2)$ vs $O(h)$)
  • Order of accuracy determines how fast error decreases with grid refinement
  • Truncation error is the difference between exact derivative and FD approximation
  • Higher-order schemes use more points but converge faster
  • Boundary treatment requires special one-sided or extrapolation formulas
  • Stability (CFL) limits time step for explicit time integration

What's Next

While finite differences approximate derivatives, CFD typically needs to conserve mass, momentum, and energy. The next lesson introduces the Finite Volume Method — the dominant discretization approach in commercial CFD that naturally enforces conservation laws.

Governing Equations