A small Haskell simulation of travelling and standing waves, originally written for fun to share with my high-school physics class in 2022.
The simulation models the motion of particles in a medium and visualises both transverse and longitudinal waves using Reanimate.
Archived project originally written in 2022 while learning Haskell and exploring mathematical and physical simulation.
A travelling wave is modelled as
where
Standing waves are produced by superposing two waves travelling in opposite directions:
The simulation also models waves travelling inwards from both ends of the container. Both transverse and longitudinal waves are represented.
The project separates the physical model from the visualisation.
Physics.hs defines functions for:
- travelling transverse and longitudinal waves;
- standing transverse and longitudinal waves, obtained by superposition;
- waves propagating from both ends of the container.
Each wave is represented as a collection of particles whose positions are determined as functions of time.
Simulation.hs converts the particle representation into SVG graphics using Reanimate. Particles are rendered as circles, with separate visualisations for transverse and longitudinal motion.
The animation is 25 seconds long and displays the wave motion over time.
Waves are represented using:
data Particle = Particle !Double !Double
data Wave
= Transverse ![Particle]
| Longitudinal ![[Particle]]A transverse wave is represented by a one-dimensional collection of particles, while a longitudinal wave is represented by particles arranged in columns.
The simulation uses fixed parameters for the amplitude, frequency, wavelength and particle spacing. The current values are defined in Constants.hs.
For example:
- wavelength:
1.5 - amplitude:
0.125 - frequency:
0.5 - container length:
15
This project was originally written using GHC 8.10.7 and an older version of Reanimate.
With the pinned dependencies in cabal.project.freeze, generate a GIF with:
cabal run standing-waves -- render --format gif -o <filename>.gifSee the Reanimate documentation for a complete list of rendering options.
