An interactive simulation of a driven-damped pendulum built with VPython/GlowScript. Originally written as a high school project: a first attempt at computational physics and learning to turn physical equations into working code. It's not perfect, but it was the project that got me interested in the overlap between physics and computer science.
The simulation models a pendulum governed by:
θ'' = -(g/L)sin(θ) - bθ' + F₀cos(ω₀t)
where:
- g is gravitational acceleration
- L is pendulum length
- b is the damping coefficient
- F₀ is the driving force amplitude
- ω₀ = √(g/L) is the natural frequency
The driving force is applied at the natural frequency, producing resonance behaviour when combined with low damping. Time integration uses the Euler-Cromer method.
- Real-time 3D pendulum visualisation
- Adjustable parameters via sliders:
- Pendulum length and bob mass
- Gravitational acceleration (e.g. set to 24.79 m/s² for Jupiter)
- Damping coefficient
- Driving force amplitude
- Timestep dt
- 6 live graphs: position, velocity, acceleration, theta, KE/PE/total energy, and a phase space plot (θ vs dθ/dt), the phase space portrait shows the transition from simple oscillation to chaotic motion as driving force increases
- Reset button to restore all parameters and clear graphs
Requires Python 3.11 (vpython has known compatibility issues with Python 3.12+).
pip install vpython
python main.pyThis opens the simulation in your browser via a local server.
The code runs as-is on glowscript.org. Just remove the from vpython import * import line, as GlowScript provides all built-ins globally.
