.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/chaos/interactive_and_animation/plot_billiard_animation.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_chaos_interactive_and_animation_plot_billiard_animation.py: Animated Billiard Trajectory ============================= The billiard animated here is the Bunimovich stadium: two straight edges, :math:`y=\pm r` for :math:`|x|\le a`, joined by semicircular caps of radius :math:`r` centered at :math:`(\pm a, 0)`. A billiard particle moves in a straight line at constant speed and undergoes specular reflection off this boundary, .. math:: \mathbf{v}' = \mathbf{v} - 2(\mathbf{v}\cdot\mathbf{n})\,\mathbf{n}, whenever it strikes it. :func:`physicskit.chaos.visualizers.dynamic_plots.animate_billiard_trajectory` builds a Matplotlib ``FuncAnimation`` showing the ball bouncing inside the stadium (left panel) alongside its Poincare section filling in live, point by point, as each new bounce occurs (right panel). .. GENERATED FROM PYTHON SOURCE LINES 20-28 .. code-block:: Python import matplotlib.pyplot as plt from physicskit.chaos.systems.billiards import BunimovichStadium from physicskit.chaos.visualizers.dynamic_plots import animate_billiard_trajectory billiard = BunimovichStadium(radius=1.0, straight_length=2.0) .. GENERATED FROM PYTHON SOURCE LINES 29-34 Build the animation --------------------- Assign the animation to a variable to keep it alive; a reference must survive until it is displayed (``plt.show()``) or saved (``anim.save(...)``) or Matplotlib will silently drop it. .. GENERATED FROM PYTHON SOURCE LINES 34-38 .. code-block:: Python anim = animate_billiard_trajectory(billiard, pos=billiard.sample_interior_point(), vel=(0.5, 0.9), n_bounces=100, interval=50) plt.show() .. container:: sphx-glr-animation .. raw:: html .. GENERATED FROM PYTHON SOURCE LINES 39-43 To save the animation to a file instead of (or in addition to) displaying it interactively, use e.g.:: anim.save("stadium_animation.gif", writer="pillow", fps=30) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 16.192 seconds) .. _sphx_glr_download_api_gallery_chaos_interactive_and_animation_plot_billiard_animation.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_billiard_animation.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_billiard_animation.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_billiard_animation.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_