Pendulum¶
In this tutorial, we demonstrate how to generate data of a pendulum, and introduce the mathematics of a pendulum.
import math
import plotly.express as px
from hamilflow.models.pendulum import Pendulum
Constants¶
omega0 = 2 * math.pi
theta0 = math.pi / 3
n_periods = 2**2
n_samples_per_period = 2**8
A pendulum¶
pen = Pendulum(system=omega0, initial_condition=theta0)
Data¶
df_pen = pen.generate_from(
n_periods=n_periods,
n_samples_per_period=n_samples_per_period,
)
df_pen.head()
t | x | u | |
---|---|---|---|
0 | 0.000000 | 1.047198 | 1.570796 |
1 | 0.004192 | 1.046897 | 1.593608 |
2 | 0.008384 | 1.045996 | 1.616424 |
3 | 0.012576 | 1.044494 | 1.639247 |
4 | 0.016768 | 1.042393 | 1.662082 |
df_pen.describe()
t | x | u | |
---|---|---|---|
count | 1024.000000 | 1.024000e+03 | 1024.000000 |
mean | 2.144268 | 2.320193e-17 | 14.124895 |
std | 1.239809 | 7.453532e-01 | 7.261249 |
min | 0.000000 | -1.047198e+00 | 1.570796 |
25% | 1.072134 | -7.494689e-01 | 7.848279 |
50% | 2.144268 | -3.535251e-16 | 14.125761 |
75% | 3.216402 | 7.494689e-01 | 20.403244 |
max | 4.288536 | 1.047198e+00 | 26.680726 |
Plot¶
px.line(
df_pen,
x="t",
y="x",
title=rf"Simple Harmonic Oscillator ($\omega_0 = {omega0:.4f})$",
labels={"x": r"Angle $\theta(t)$", "t": r"Time $t$"},
)
TODO¶
- Compare with a harmonic oscillator in terms of frequency and profile
- Animate the plot as a single pendulum
- Add references to the derivation
- Complete the derivation
Mathematical-physical description¶
Lagrangian action¶
We describe a generic pendulum system by the Lagrangian action $$ S_L[\theta] \equiv \int_{t_0}^{t_1} \mathbb{d}t\,L(\theta, \dot\theta) \eqqcolon I \int_{t_0}^{t_1} \mathbb{d}t \left\{\frac{1}{2} \dot\theta^2 + \omega_0^2 \cos\theta \right\}\,, $$ where $L$ is the Lagrangian; $\theta$ is the angle from the vertical to the pendulum as the generalised position; $I$ is the inertia parameter, $\omega_0$ the frequency parameter, and we also call $U \coloneqq I\omega_0^2$ the potential parameter.
This setup contains both the single and the physical pendula. For a single pendulum, $$ I = m l^2\,,\qquad U = mgl\,, $$ where $m$ is the mass of the pendulum, $l$ is the length of the rod or cord, and $g$ is the gravitational acceleration.
Integral of motion¶
The Lagrangian action does not contain time $t$ explicitly. As a result, the system is invariant under a variation of time, or $\mathbb{\delta}S / \mathbb{\delta}{t} = 0$. This gives an integral of motion $$ \dot\theta\frac{\partial L}{\partial \dot\theta} - L \equiv E \eqqcolon I \omega_0^2 \cos\theta_0\,, $$ where $\theta_0$ is the initial angle.
Substitution gives $$ \left(\frac{\mathbb{d}t}{\mathbb{d}\theta}\right)^2 = \frac{1}{2\omega_0^2} \frac{1}{\cos\theta - \cos\theta_0}\,. $$
Coordinate transformation¶
For convenience, introduce the coordinate $u$ and the parameter $k$ $$ \sin u \coloneqq \frac{\sin\frac{\theta}{2}}{k}\,,\qquad k \coloneqq \sin\frac{\theta_0}{2} \in [-1, 1]\,. $$ One arrives at $$ \left(\frac{\mathbb{d}t}{\mathbb{d}u}\right)^2 = \frac{1}{\omega_0^2} \frac{1}{1-k^2\sin^2 u}\,. $$ The square root of the second factor on the right-hand side makes an elliptic integral.