Sunday, February 25, 2023

Fun with the Wave Equation

I have always thought that the discrete wave equation offers some compelling vehicle for interactive and generative art. The math behind it is not that hard, but it is pretty mathy, so I will leave that for others to explain --here's a decent chapter on it

Anyway, the solutions to the wave equation are sinusoids -- in one dimension, the modes on a guitar string; in two dimensions the familiar waves you get from tossing a stone in a puddle. I coded up some versions in Javascript using p5.js and they are pretty fun. Splash around in them by clicking the mouse anywhere:

You can click on the embeds to get at the js code, or follow the links to the line simulation or the LED simulation. Do remember to close this tab because the unoptimized javascript will eat up your cpu!

The second approach where the displacement is coded as hue and brightness was pretty easy to code in Circuit Python, to drive an array of addressable LEDs. Here's the code: code.py. Here, as in the javascript, there are several constants which affect the simulation. The Courant value affects the speed of the wave in the medium. Higher values for this mean faster waves. Because real world media are lossy, I also added a damping factor that removes a small fraction of the energy at every iteration. This is so waves will not oscillate forever, but will grow smaller with an exponential decay as they might in the real world.



Archives:


Topics:


RSSicon.png  RSS feed