# Tactiverge Academy — companion code for "What a simulation is"
# Lesson: https://tactiverge.win/academy/computational-engineering/scientific-simulation/what-is-a-simulation/
# Licence: MIT (see https://tactiverge.win/open-engineering/). Examples appear in lesson order.

# ── Example 1 ────────────────────────────────────────
G = 9.81   # m/s², assumed constant (model assumption)

def max_height(v0, dt):
    """Step a ball thrown straight up until it stops rising."""
    y, v = 0.0, v0          # the state: height (m) and velocity (m/s)
    while v > 0:
        y = y + v * dt      # new height from current velocity
        v = v - G * dt      # new velocity from gravity
    return y

exact = 12.0**2 / (2 * G)
print(f"exact:      {exact:.4f} m")
for dt in [0.1, 0.01, 0.001]:
    h = max_height(12.0, dt)
    print(f"dt = {dt:<5}  {h:.4f} m   error {h - exact:+.4f} m")
