ODE Integration & Euler's Method
When analytical solutions fail, we use numerical methods to approximate physics via step-by-step calculation.
The Euler method is the simplest ODE integrator: . Here, is the current state, is the step size, is the slope, and is the next state.
| Integrator | Order | Error Scaling | Description |
|---|---|---|---|
| Euler | Single slope per step | ||
| Midpoint (RK2) | Slope at midpoint | ||
| RK4 | Four slopes; physics workhorse |
Common pitfall: A simulation that runs without errors is not necessarily correct. Always validate against a known analytic case, like exponential decay , before trusting new output.
Check convergence by running your simulation with step size and . If results match your desired precision, the solution has converged.
Monte Carlo Methods
Monte Carlo methods use random sampling to estimate numerical quantities when analytical integration is intractable.
To estimate , place random points in a unit square. The fraction falling inside the inscribed quarter-circle () converges to :
inside = sum(1 for _ in range(N)
if random.random()**2
+ random.random()**2 <= 1)
pi_est = 4 * inside / N
Accuracy scales as . This means quadrupling points only halves the error, requiring substantial samples for high precision.
Tip: Always validate your simulation against a known analytical case or conservation law before applying it to uncharted problems.