Courses / Physics I
Computer Science

Simulation and numerical experimentation

Physics I 299 words Free to read

When analytical solutions are impossible, we use numerical methods to approximate the physics.

Euler method — The simplest ODE integrator:

yn+1=yn+hf(tn,yn)y_{n+1} = y_n + h\,f(t_n,\,y_n)

def euler(f, y0, t0, t_end, h):
    t, y = t0, y0
    trajectory = [(t, y)]
    while t < t_end:
        y = y + h * f(t, y)
        t = t + h
        trajectory.append((t, y))
    return trajectory

Better integrators

Monte Carlo methods — Use random sampling to estimate quantities:

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

Convergence check — Run the simulation with step size hh and h/2h/2. If results agree to the desired precision, the solution is converged.

Tip: Always validate your simulation against a known analytical case before applying it to a new problem. If Euler's method can't match ete^{-t} for exponential decay, it won't be reliable for anything more complex.
Common pitfall: A simulation that runs without errors is not a simulation that is right. Validate against a case with a known answer (analytic solution, conservation law) before trusting any new output.

Monte Carlo Simulation

Monte Carlo methods use random sampling to estimate numerical quantities.

To estimate π\pi: place random points in a unit square. The fraction that falls 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}}

Accuracy improves as O(1/N)O(1/\sqrt{N}) — quadrupling points halves the error.

Monte Carlo methods are essential when analytical integration is intractable.
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
Start Physics I free

Computer Science