This lesson combines algorithms, data structures, numerical methods, and visualisation into complete scientific workflows.
Typical computational physics pipeline
Define problem → Model equations → Implement code
→ Validate → Run simulation → Analyse & visualise
Example: projectile with drag
import numpy as np
import matplotlib.pyplot as plt
g, b = 9.8, 0.1 # gravity, drag coefficient
dt, t_max = 0.01, 10.0
N = int(t_max / dt)
x = np.zeros(N); y = np.zeros(N)
vx = np.full(N, 20.0); vy = np.full(N, 30.0)
for i in range(1, N):
speed = np.sqrt(vx[i-1]**2 + vy[i-1]**2)
vx[i] = vx[i-1] - b * speed * vx[i-1] * dt
vy[i] = vy[i-1] - (g + b * speed * vy[i-1]) * dt
x[i] = x[i-1] + vx[i] * dt
y[i] = y[i-1] + vy[i] * dt
if y[i] < 0:
break
plt.plot(x[:i], y[:i])
plt.xlabel("x (m)"); plt.ylabel("y (m)")
plt.title("Projectile with Air Resistance")
plt.grid(True); plt.show()
Workflow checklist
- Model — Write the governing equations (here: Newton's second law with drag).
- Discretise — Choose a numerical method and time step.
- Code — Implement with clear variable names and modular functions.
- Validate — Check limiting cases (e.g., should give parabolic trajectory).
- Visualise — Plot results and compare with expectations.
Strategy: Start simple (no drag, no friction) and add complexity incrementally. Each addition should be validated before proceeding to the next.
Common pitfall: Premature optimization wastes more effort than slow code ever did: first make it correct and clear, then measure, and only then speed up the part the measurement actually indicts.