.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/classical/lagrangian/plot_05_elastic_pendulum.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_api_gallery_classical_lagrangian_plot_05_elastic_pendulum.py: The Elastic Pendulum and 1:2 Autoparametric Resonance ============================================================ :class:`~physicskit.classical.systems.lagrangian.ElasticPendulum` is a mass :math:`m` on a Hookean spring of natural length :math:`L_0` and stiffness :math:`k`, free to both stretch and swing. With generalized coordinates :math:`q = (s, \theta)` -- :math:`s` the spring's stretch beyond :math:`L_0` (so the pivot-to-mass distance is :math:`r = L_0 + s`) and :math:`\theta` the swing angle from the downward vertical -- its Lagrangian is .. math:: L = \frac{m}{2}\left[\dot s^2 + (L_0 + s)^2 \dot\theta^2\right] - \frac{k}{2} s^2 + m g (L_0 + s)\cos\theta . The :math:`(L_0+s)^2\dot\theta^2` term is what couples the (otherwise independent, linear) stretch and swing motions: whenever the mass swings, its varying distance from the pivot pumps the spring, and vice versa. Written alone each mode would oscillate at its own natural frequency, :math:`\omega_s = \sqrt{k/m}` for the stretch and :math:`\omega_\theta = \sqrt{g/L_0}` for the swing; the coupling becomes strong -- and famously resonant -- whenever :math:`\omega_s \approx 2\,\omega_\theta` (*1:2 autoparametric resonance*), where energy started as pure vertical stretching oscillation periodically leaks into, and back out of, swinging. ``ElasticPendulum``'s default parameters (:math:`k = 4mg/L_0`) are set to this 1:2 resonance condition exactly. .. GENERATED FROM PYTHON SOURCE LINES 33-38 .. code-block:: Python import matplotlib.pyplot as plt from physicskit.classical.systems.lagrangian import ElasticPendulum from physicskit.classical.visualizers.animations import animate_elastic_pendulum .. GENERATED FROM PYTHON SOURCE LINES 39-43 Energy exchange between stretching and swinging ------------------------------------------------------ Start mostly stretched, with only a small swing seed, and integrate long enough to see the swing amplitude visibly beat. .. GENERATED FROM PYTHON SOURCE LINES 43-56 .. code-block:: Python system = ElasticPendulum(q0=[0.4, 0.05], qdot0=[0.0, 0.0]) result = system.integrate((0.0, 60.0), dt=0.1, method="implicit_midpoint") fig1, (ax1, ax2) = plt.subplots(2, 1, figsize=(8, 6), sharex=True) ax1.plot(result.t, result.q[:, 0], color="steelblue") ax1.set_ylabel("s (stretch)") ax1.set_title("Elastic pendulum: energy leaking between modes") ax2.plot(result.t, result.q[:, 1], color="firebrick") ax2.set_ylabel(r"$\theta$ (swing)") ax2.set_xlabel("t") fig1.tight_layout() .. image-sg:: /api/gallery/classical/lagrangian/images/sphx_glr_plot_05_elastic_pendulum_001.png :alt: Elastic pendulum: energy leaking between modes :srcset: /api/gallery/classical/lagrangian/images/sphx_glr_plot_05_elastic_pendulum_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 57-59 Animation: the coil stretching and swinging together ------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 59-63 .. code-block:: Python anim = animate_elastic_pendulum(system, result, interval=20, trail=300) plt.show() .. container:: sphx-glr-animation .. raw:: html .. GENERATED FROM PYTHON SOURCE LINES 64-68 To save the animation to a file instead of (or in addition to) displaying it interactively, use e.g.:: anim.save("elastic_pendulum_animation.gif", writer="pillow", fps=30) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 27.288 seconds) .. _sphx_glr_download_api_gallery_classical_lagrangian_plot_05_elastic_pendulum.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_05_elastic_pendulum.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_05_elastic_pendulum.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_05_elastic_pendulum.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_