Tag: Python

Pendulum Art

Pendulum Art

No time for intros, let’s do this quick! I came across some linkedin posts with videos like this one: https://www.youtube.com/watch?v=MxMg3ipxoNo Why quick? I was in the middle of something, I saw this, and I really couldn’t afford more than few hours to stray, so –

As an avid positivist, I find joy in observing the order and the elegance in the underlying laws of nature. In this case, the equations for this system, neglecting the change in the mass of the paint can can be readily derived from the Lagrangian in polar coordinates (assuming mass = 1, and letting R denote the rope length):

L=\frac{1}{2}(R^2\dot{\theta}^2+R^2sin^2(\theta)\dot{\phi}^2) - gRsin(\theta)

I’m adhering to the usual customs here, dot denotes derivation with respect to time, g is gravity etc. We did neglect the mass emission but let’s do take into account, the drag force so that we can observe the can losing energy and eventually coming to a halt. Consult Goldstein if you want a physics refresher, moving on: we pick ~v stokes drag, in terms of Euler-Lagrange equations (heard of Rayleight Dissipation ?) leading to:

\frac{d}{dt} \frac{\partial L}{\partial q} - \frac{\partial L}{\partial \dot{q}} = Q

sin^2(\theta)\dot{\phi} = Ce^{-kt}

\ddot{\theta} = -\frac{gsin(\theta)}{R} + sin(\theta)cos(\theta)\phi^2 - k\dot{\theta}

So we take some initial condition (θ,φ) and their derivatives, and then iteratively update their values according to the above equation in small time intervals, and voilà we have a simulated pendulum. In retrospect it would be easy to take into account the mass emission as well, as a 1st order approximation, we can redo the above equations with m and then assume that m is linearly decreasing with time, eventually only being the mass of the tin can:

m = m_0 - at : t \in [0,T] where m_{can} = m_0 - aT

And for plotting, we transform (θ,φ) to (x,y), i.e.:

x=Rsin(\theta)cos(\phi) and x=Rsin(\theta)sin(\phi)

My implementation (without the mass consideration) ended up being a mere 80 lines, using PyOpengl, resulting in shapes like this:

Look at it go: https://filebin.net/gblz3psvt06nw81e/art.mp4

Bonus: as the keen observer might notice from my scribbling:

I did initially make small mistakes in my calculations, found out the hard way, after realizing weirdly off-the-mark results. Also, extra for extra fun, try solving the equations of motion for t->∞ analytically. Cleaning up my scribbles and code on this is now pushed onto the stack of things I wish I someday find the time to do, I wonder when this stack overflows my return address 😉