Lesson 1
Spatial and Temporal Grids
Choose and document the discretization that defines the numerical problem.
1 Modelling context
A partial differential equation describes how a quantity varies in space and time. The heat equation, the wave equation, and the diffusion equation are all stated on a continuous domain, with the solution defined at every point. To compute a numerical solution, the continuous domain must be replaced by a finite set of points.
The choice of grid is not a technical detail. It defines what the numerical problem actually is, and it determines the accuracy, the stability, and the cost of the solution. Two grids of different resolution produce two different discrete problems, even when the underlying PDE is the same.
This lesson establishes the grid conventions used throughout Module 3, and the documentation discipline that makes a grid choice reviewable and reproducible.
The grid is part of the model
Every simulation has a grid, and every grid is a decision. The grid spacing must be chosen deliberately, justified in terms of the problem and the accuracy required, and documented alongside the parameters and boundary conditions.
2 Learning objectives
By the end of this lesson you will be able to
- Construct a uniform spatial grid and a uniform temporal grid for a PDE problem.
- Distinguish grid points from the midpoints between them.
- Document the grid so that a reader can reconstruct it exactly.
- Explain how grid resolution affects the accuracy of a numerical solution.
- Estimate the computational cost of a grid refinement before running it.
3 Prerequisite review
- Domain
-
The region of space and time over which the PDE holds. For a
one-dimensional problem, typically an interval
[a, b]in space and[0, T]in time. - Grid spacing
-
The distance between consecutive grid points. Denoted
Δxin space andΔtin time. - Uniform grid
- A grid in which the spacing is constant. Uniform grids simplify the finite-difference formulas and are the default in this course.
- Grid function
-
The discrete representation of the solution, denoted
u_i^nfor the value at spatial indexiand temporal indexn.
4 Mathematical formulation
A uniform one-dimensional grid
Suppose the spatial domain is the interval [a, b].
Divide it into N_x equal subintervals of width
Δx = (b − a) / N_x
The grid points are
x_i = a + i Δx, i = 0, 1, …, N_x
Similarly, for the time interval [0, T] divided into
N_t equal steps of size Δt = T / N_t,
the time levels are
t_n = n Δt, n = 0, 1, …, N_t
The grid function
The numerical solution is a grid function
u_i^n, defined at every point
(x_i, t_n). It approximates the exact solution:
u_i^n ≈ u(x_i, t_n)
The finite-difference method provides a rule for computing
u_i^{n+1} from values at level n — a
time-marching scheme. The initial condition
supplies u_i^0 for all i; the boundary
conditions supply values or relations at i = 0 and
i = N_x for all n.
Grid resolution and accuracy
The accuracy of the numerical solution depends on both grid
spacings. For a scheme with temporal truncation error
O(Δtp) and spatial truncation error
O(Δxq), the global error scales
approximately as
E ≈ C_t Δtp + C_x Δxq
where C_t and C_x are constants that
depend on the problem and the scheme. Both terms must be
controlled to achieve a target accuracy. Reducing one without the
other will eventually produce diminishing returns.
Computational cost
For a one-dimensional problem, the total number of grid points is approximately
N_x · N_t = ( (b − a) / Δx ) · ( T / Δt )
The cost of a simulation is proportional to this product,
multiplied by the cost per grid point (which depends on the
scheme). For explicit schemes, the CFL stability restriction
imposes an additional relation between Δt and
Δx, further increasing the cost of grid refinement.
5 Worked example
Laying out a grid for the heat equation
Consider the heat equation on the spatial interval
[0, 1] and the time interval [0, 0.5]:
∂u/∂t = α ∂²u/∂x², α = 0.1
Initial condition: u(x, 0) = sin(π x). Boundary
conditions: u(0, t) = 0, u(1, t) = 0.
Step 1 — Choose the spatial grid.
Let N_x = 20. Then
Δx = 1 / 20 = 0.05
x_i = i · 0.05, i = 0, 1, …, 20
Step 2 — Choose the time step.
The CFL condition for the explicit scheme is
Δt ≤ Δx² / (2α). Substituting:
Δt ≤ (0.05)² / (2 · 0.1) = 0.0025 / 0.2 = 0.0125
Choose Δt = 0.01, which is comfortably below the
limit. Then
N_t = 0.5 / 0.01 = 50
t_n = n · 0.01, n = 0, 1, …, 50
Step 3 — Determine the grid dimensions.
The grid function has dimensions
(N_x + 1) × (N_t + 1) = 21 × 51, giving 1071 grid
points. The unknown values are the interior points at time
levels 1 through 50: 19 · 50 = 950 values to be
computed.
Step 4 — Document the grid.
The documentation for this grid includes:
- Spatial domain:
[0, 1]. - Number of spatial intervals:
N_x = 20. - Spatial spacing:
Δx = 0.05. - Temporal domain:
[0, 0.5]. - Time step:
Δt = 0.01. - Number of time steps:
N_t = 50. - Diffusion number:
r = α Δt / Δx² = 0.4. -
Stability margin:
r = 0.4 < 0.5, stable.
6 Verification check
Checks to perform
-
Confirm grid dimensions. The number of grid
points in space is
N_x + 1(inclusive endpoints), notN_x. An off-by-one error here is the most common source of index errors in PDE code. -
Check that the last grid point coincides with the domain
endpoint.
Confirm
x_{N_x} = bto within floating-point tolerance. -
Verify the time levels reach the final time
exactly.
Confirm
t_{N_t} = T, or document any endpoint mismatch. -
Check the CFL condition before running.
Compute
r = α Δt / Δx²and confirm it is below the stability limit for the scheme. -
Test the grid with a known solution. For
u(x, 0) = sin(π x), the exact solution isu(x, t) = e−α π² t sin(π x). Compare the numerical and exact solutions at a few grid points.
7 Interpretation
The grid is the discrete analogue of the continuum. Every quantity
that is a function of x or t in the
continuous problem becomes an indexed array in the discrete
problem. Choosing the grid therefore determines the shape of every
data structure in the implementation.
The resolution of the grid determines what features of the
solution can be represented. A feature of spatial extent
L requires several grid points to be resolved
adequately: at minimum, Δx ≪ L. A boundary layer, a
sharp front, or an oscillation with wavelength
λ requires even more points:
Δx ≲ λ / 10 for reasonable accuracy.
Refinement is not free
Refining the grid improves accuracy but increases cost. For an
explicit scheme, the CFL condition ties the time step to the
spatial step, so refining Δx by a factor of 2
increases the total number of grid operations by a factor of 8.
Planning the resolution before running is essential for problems
that would otherwise exceed the available computational budget.
8 Uncertainty and limitations
The grid introduces two distinct sources of error into the numerical solution.
| Source | Cause | Reduced by |
|---|---|---|
| Spatial truncation error | Finite-difference approximation of spatial derivatives | Smaller Δx |
| Temporal truncation error | Finite-difference approximation of the time derivative | Smaller Δt |
| Representation error | Features smaller than the grid spacing cannot be represented |
Smaller Δx until the feature is resolved
|
The third source is often overlooked. Even a formally accurate scheme cannot represent a solution feature that is smaller than the grid spacing. If the initial condition contains a sharp front and the grid is coarse, the front will be smeared across several grid cells regardless of the scheme's theoretical accuracy.
Resolution matters as much as order
A high-order scheme on a grid that does not resolve the solution is no better than a low-order scheme on the same grid. Before refining the order, ensure the grid resolves the features of interest.
9 Common implementation errors
Watch for these
-
Off-by-one in the number of grid points.
A spatial domain divided into
N_xintervals hasN_x + 1grid points, including both endpoints. Forgetting the endpoints produces either a short array or an index error. -
Assuming the last grid point reaches the endpoint
exactly.
Rounding in
Δxcan producex_{N_x} = b ± ε. Document any mismatch, or set the last grid point explicitly. -
Mixing
ΔxandΔx²in the implementation. The second difference divides byΔx², the first difference byΔx. A common error is to divide both byΔx. - Forgetting that interior points are the only unknowns. Boundary values are supplied by the boundary conditions at each time level. They are not computed by the scheme.
- Choosing a grid that violates the CFL condition. Verify the stability condition before running, not after the solution has blown up.
-
Documenting only
N_xand notΔx. Both are needed for a reader to reconstruct the grid. The same applies to time.
10 Independent modelling task
Your turn
-
Choose a one-dimensional PDE problem. A simple heat-equation
problem on
[0, 1]is sufficient. -
Choose a spatial resolution
N_x. State the domain and justify the resolution in terms of the features of the initial condition you want to represent. -
Compute the CFL limit for the explicit scheme. Choose a time
step
Δtthat is below this limit, and compute the resulting diffusion numberr. -
Compute the grid dimensions
(N_x + 1) × (N_t + 1)and the number of unknown values. -
Document the grid: domain,
N_x,Δx,N_t,Δt,r, stability status. -
Refine the grid by a factor of 2 in
Δx. Compute the new CFL limit and the new grid dimensions. Compute the ratio of total grid operations to the previous configuration.
This exercise forms part of Laboratory 5 and the grid-documentation component of Assignment 3.
11 Reflection prompt
Reflect before continuing
Halving Δx requires quartering Δt to
maintain stability. Does this mean that halving the spatial grid
spacing is always a bad idea?
Two or three sentences. Consider the circumstances under which the accuracy gain justifies the cost, and the circumstances under which it does not.
12 Lesson summary
Key takeaways
- A grid is a modelling decision. It defines the discrete problem and determines the accuracy, stability, and cost of the solution.
-
A domain of
N_xintervals hasN_x + 1grid points. Both endpoints are included. The interior points are the unknowns. -
The CFL condition ties
ΔttoΔx². Refining the spatial grid by a factor of 2 requires quartering the time step, increasing total operations by a factor of 8. - Grid resolution determines what features can be represented. A feature smaller than the grid spacing cannot be captured, regardless of the scheme's theoretical order.
- Document the grid completely. Domain, spacing, number of intervals, diffusion number, and stability status are all part of the numerical problem.
Coming next
Lesson 2 derives the finite-difference approximations that define the stencils applied on the grid constructed here.
§ Self-quiz, practice, and homework
Self-quiz
Q1
A spatial domain of N_x intervals has how many
grid points?
Show answer
N_x + 1. Both endpoints are
included.
Q2
For the FTCS scheme, halving Δx requires:
Show answer
Quartering Δt. The CFL
condition is quadratic in Δx.
Q3
The initial condition supplies:
Show answer
The bottom row of the grid at
t = 0.
Q4
A solution feature smaller than the grid spacing:
Show answer
Cannot be represented on the grid. Resolution and order are complementary.
Q5
Grid refinement for an explicit scheme increases total cost by roughly:
Show answer
A factor of 8 per halving of Δx.
N_x doubles and N_t quadruples.
Practice exercises
-
For a domain
[0, 2]divided intoN_x = 40intervals, computeΔx, list the grid points, and count them. -
For the same domain, choose
r = 0.4andα = 0.05. ComputeΔtandN_tfor a final timeT = 1. -
Halve
Δxand recomputeΔt,N_t, and the total number of grid operations. Compare. -
Sketch a grid with
N_x = 5andN_t = 3. Mark which points are given by the initial condition, by the boundary conditions, and by the scheme.
Homework problems
-
Grid design report. Choose a one-dimensional
PDE problem. Design a grid appropriate to the problem: state
the domain,
N_x,Δx, the CFL limit,Δt,N_t, andr. Justify each choice in one sentence. Submit a two-page report. -
Refinement cost analysis. For the grid above,
compute the total cost at three successive refinements
(
Δx,Δx/2,Δx/4). Plot the cost againstΔxon a log-log scale. Estimate the exponent. - Resolution test. For a problem with a sharp initial feature (e.g. a narrow Gaussian or a step function), demonstrate numerically that the feature cannot be resolved on a coarse grid, and show how refinement recovers it.
Mini project
Design a grid for a real PDE problem of your choice — heat
conduction in a rod, diffusion of a pollutant, or similar.
Document the domain, the grid resolution, the CFL limit, the
chosen Δt, and the stability margin. Run the same
problem at three resolutions and compare the results. Submit a
report and the spreadsheet. This is the basis for Laboratory 5
and Assignment 3.