These exercises and projects supplement those in the book. They
introduce different functions, data sets, and physical
applications, with particular attention to irregular grids, noisy
data, error estimation, boundary treatment, and derivatives in more
than one dimension.
Physics projects
These projects use numerical differentiation as part of a larger
physical investigation. Each project includes an analytical or
limiting result that can be used for validation.
1. Electric field and charge density from a potential map
Objective: reconstruct an electric field and charge
density from potential values sampled on a two-dimensional grid.
V(x,y) = V0 sin(πx/L) sin(πy/L),
0 ≤ x,y ≤ L.
Calculate E = −∇V and use Poisson's
equation, ρ = −ε0∇2V,
to infer the corresponding charge density. Plot the potential,
electric-field vectors, and charge-density map. Examine how the
errors change with grid resolution and near the boundaries. Use
L = V0 = ε0 = 1 and grids
with 33, 65, and 129 points in each direction as an initial
dimensionless test. For the main error comparison, compute the
charge density on interior nodes. As a separate boundary test,
use second-order one-sided formulas to evaluate it on the edges.
Validation: derive the exact field components and
verify that
ρ = 2π2ε0V0
sin(πx/L)sin(πy/L)/L2.
Because the exact charge density is zero on the boundary, use
absolute error there rather than relative error.
Possible extension: add small random errors to the
potential values and compare how strongly the field and charge
density are affected.
2. Group velocity and effective mass in an energy band
Objective: extract transport quantities from a
numerically tabulated dispersion relation.
E(k) = E0 − 2J cos(ka),
vg = (1/ℏ) dE/dk,
m* = ℏ2/(d2E/dk2).
Use dimensionless values
ℏ = a = J = 1 and E0 = 0.
Tabulate E(k) at 64 equally spaced points on the
endpoint-exclusive grid −π/a ≤ k < π/a
across the first Brillouin zone and
calculate the group velocity and effective mass numerically.
Locate the extrema of the band, the maximum magnitude of the
group velocity, and the points where the effective mass changes
sign. Because m* diverges when the band
curvature passes through zero, plot
1/m* as well as, or instead of, plotting
m* directly. Compare second- and
fourth-order formulas and use periodic wraparound at the
Brillouin-zone boundaries. Do not include both
−π/a and π/a, because they
represent the same physical state and duplicate an endpoint in a
wrapped stencil. This is the same convention used in the periodic
cos(3x) exercise above.
Validation: differentiate the cosine band
analytically and verify the numerical velocity and curvature.
Show that |vg| is maximal at
ka = ±π/2, precisely where the curvature is
zero and the effective mass changes sign through infinity.
Possible extension: perturb the tabulated energies
to imitate calculated or measured band-structure uncertainty and
test a local polynomial fit before differentiating.
3. Galaxy mass density from a rotation curve
Objective: infer a spherical mass distribution from
a circular-speed profile and study the sensitivity to noisy data.
v(r) = v0r/√(r2 + a2),
M(r) = rv2(r)/G,
ρ(r) = [4πr2]−1dM/dr.
Generate a synthetic rotation curve on both uniform and
irregular radial grids. Calculate the enclosed mass and then
differentiate it to obtain the density. Compare direct finite
differences, local polynomial differentiation, and a smoothed
calculation after adding 1% and 5% normally distributed relative
errors to the speeds. For an initial dimensionless calculation,
use G = v0 = a = 1 and 201 points over
0 ≤ r ≤ 10a.
Validation: verify that
ρ(r) = v02(r2+3a2)/
[4πG(r2+a2)2].
It approaches
3v02/(4πGa2)
near the origin and decreases as r−2
at large radius. Treat the origin by its limiting value because
direct division by 4πr2 is numerically
fragile. Also compare direct differentiation of M(r)
with
dM/dr = [v2+2rv(dv/dr)]/G. For the noisy
cases, plot relative density error versus radius and explain why
noise is especially strongly amplified near the origin.
Possible extension: use a rotation curve from the
public SPARC database
and discuss the uncertainty of the inferred density. Since SPARC
galaxies are disks whereas the formula above assumes spherical
symmetry, interpret the result as a spherical-equivalent density,
not the actual three-dimensional density of the disk. See
F. Lelli, S. S. McGaugh, and J. M. Schombert,
“SPARC: Mass Models for 175 Disk Galaxies with Spitzer
Photometry and Accurate Rotation Curves,”
The Astronomical Journal 152 (2016), 157,
doi:10.3847/0004-6256/152/6/157.
4. Divergence and vorticity of a two-dimensional flow
Objective: identify local expansion, compression,
and rotation from velocity-field data.
∇·u = ∂u/∂x + ∂v/∂y,
ωz = ∂v/∂x − ∂u/∂y.
Use the regularized radial source
ur(r) = [Q/(2πr)]
[1 − e−r2/σ2],
∇·usource
= [Q/(πσ2)]
e−r2/σ2.
Construct a velocity field by combining solid-body rotation,
u = −Ωy, v = Ωx, with the
smooth regularized radial source above. Convert its radial
velocity to Cartesian components and add them to the rotational
field. When constructing the source on the grid, set its Cartesian
components to their limiting values u = v = 0 at
r = 0 to avoid division by zero. Use
Ω = Q = 1, σ = 0.30,
and 51- and 101-point grids on
−1 ≤ x,y ≤ 1 as initial values. Calculate
the divergence and vorticity numerically, and use contour maps to
distinguish the rotating and expanding regions.
Validation: verify that the combined field has the
Gaussian divergence shown above and uniform vorticity
ωz = 2Ω. Examine the convergence
of both quantities and the treatment of the removable
r = 0 limit.
Possible extension: add a Lamb–Oseen vortex with
uθ = [Γ/(2πr)]
[1−e−r2/σv2]
and exact vorticity
ωz = [Γ/(πσv2)]
e−r2/σv2,
or use a measured particle-image-velocimetry field and investigate
the influence of spatial resolution and noise.