Courses / Physics I
Computer Science

Simulation and numerical experimentation

Physics I 272 words Free to read

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: yn+1=yn+hf(tn,yn)y_{n+1} = y_n + h f(t_n, y_n). Here, yny_n is the current state, hh is the step size, f(tn,yn)f(t_n, y_n) is the slope, and yn+1y_{n+1} is the next state.

IntegratorOrderError ScalingDescription
Eulerp=1p=1O(h1)O(h^1)Single slope per step
Midpoint (RK2)p=2p=2O(h2)O(h^2)Slope at midpoint
RK4p=4p=4O(h4)O(h^4)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 ete^{-t}, before trusting new output.

Check convergence by running your simulation with step size hh and h/2h/2. 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 π\pi, place random points in a unit square. The fraction falling inside the inscribed quarter-circle (x2+y21x^2 + y^2 \leq 1) converges to π/4\pi/4:

π4points insidetotal points\pi \approx 4 \cdot \frac{\text{points inside}}{\text{total points}}

inside = sum(1 for _ in range(N)
             if random.random()**2 
             + random.random()**2 <= 1)
pi_est = 4 * inside / N

Accuracy scales as O(1/N)O(1/\sqrt{N}). 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.
Monte Carlo Estimation of π

Practise this lesson

The explanation above is free to read. The graded practice for this lesson lives in the Tryals app.

11practice questions
2interactive scenes

Computer Science