# Simulation and numerical experimentation

Physics I · Computer Science · https://tryals.app/learn/physics-i/simulation-and-numerical-experimentation

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

| Integrator | Order | Error Scaling | Description |
| --- | --- | --- | --- |
| **Euler** | $p=1$ | $O(h^1)$ | Single slope per step |
| **Midpoint (RK2)** | $p=2$ | $O(h^2)$ | Slope at midpoint |
| **RK4** | $p=4$ | $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 $e^{-t}$, before trusting new output.

Check **convergence** by running your simulation with step size $h$ and $h/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 ($x^2 + y^2 \leq 1$) converges to $\pi/4$:

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

```python
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/\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.

## Practice questions

4 of this lesson's 11 practice questions, with answers. The full set is in the app.

### 1. A Monte Carlo estimate uses 10,000 samples. Going to **40,000**, four times the work, halves the error rather than quartering it. Why?

A. Because the extra samples are increasingly correlated with the ones already drawn
B. Because the error falls as $1/\sqrt{N}$, so tenfold costs a hundred
C. Because the random number generator repeats after about 10,000 draws
D. Because rounding error grows with sample count and cancels the gain

**Answer:** B. Because the error falls as $1/\sqrt{N}$, so tenfold costs a hundred

**Why:** The estimate is an average, so its standard error goes as $\sigma/\sqrt{N}$, four times the samples buys a factor of two. That square-root wall is why Monte Carlo is chosen for problems where nothing else works rather than where accuracy is cheap, and why variance reduction is worth more than raw sample count.

Page: https://tryals.app/practice/physics-i/simulation-and-numerical-experimentation/a-monte-carlo-estimate-uses-10-000-samples-going-to-40-000-four

### 2. Classify each technique as Deterministic or Stochastic.

**Answer:**

- Deterministic: Bisection root-finding, Gauss elimination, Trapezoidal integration
- Stochastic: Monte Carlo sampling, Random walk simulation

**Why:** Stochastic methods involve randomness; deterministic methods produce the same result each time.

Page: https://tryals.app/practice/physics-i/simulation-and-numerical-experimentation/classify-each-technique-as-deterministic-or-stochastic

### 3. A pseudorandom generator started from the same seed produces exactly the same sequence every time.

**Answer:** True

**Why:** True. It is a deterministic algorithm, so the seed fixes the whole sequence, which is what makes a stochastic simulation reproducible and debuggable. The same property makes it unsuitable for cryptography without a hardware entropy source, since predicting the state predicts every future draw.

Page: https://tryals.app/practice/physics-i/simulation-and-numerical-experimentation/a-pseudorandom-generator-started-from-the-same-seed-produces-exactly

### 4. A numerical simulation with a smaller time step is usually more accurate but not always. Why does shrinking the step eventually stop helping?

A. The simulation becomes unstable at any small step size
B. Smaller steps change the physical system being modelled
C. Accumulated rounding error from more floating-point steps
D. Nothing, accuracy improves without limit as the step shrinks

**Answer:** C. Accumulated rounding error from more floating-point steps

**Why:** Truncation error falls with step size while round-off accumulates with step count. The total has a minimum, and going below it makes the answer worse.

Page: https://tryals.app/practice/physics-i/simulation-and-numerical-experimentation/a-numerical-simulation-with-a-smaller-time-step-is-usually-more
