.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/chaos/billiards/plot_rectangle_billiard.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_billiards_plot_rectangle_billiard.py: Rectangle Billiard (Integrable) ================================ A billiard particle moves in a straight line at constant speed inside a closed boundary and undergoes specular reflection, :math:`\mathbf{v}' = \mathbf{v} - 2(\mathbf{v}\cdot\mathbf{n})\,\mathbf{n}`, whenever it strikes the boundary. Here the boundary is the rectangle :math:`|x| \le w/2,\ |y| \le h/2` of width :math:`w` and height :math:`h`, whose walls are all axis-aligned, so :math:`\mathbf{n}` is always :math:`\pm\hat{x}` or :math:`\pm\hat{y}` -- reflections off the horizontal and vertical walls therefore only ever flip the sign of one velocity component. The rectangular billiard is integrable: each individual ray only ever visits a handful of discrete :math:`\sin\phi` values (the sine of the angle between the outgoing velocity and the local boundary tangent). This example plots a single trajectory and a Poincare section built from several rays launched at different angles. .. GENERATED FROM PYTHON SOURCE LINES 19-29 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from physicskit.chaos.systems.billiards import RectangleBilliard from physicskit.chaos.visualizers.dynamic_plots import animate_billiard_trajectory from physicskit.chaos.visualizers.phase_space import plot_billiard_trajectory, plot_poincare_section billiard = RectangleBilliard(width=2.0, height=1.0) .. GENERATED FROM PYTHON SOURCE LINES 30-32 Animation --------- .. GENERATED FROM PYTHON SOURCE LINES 32-36 .. code-block:: Python anim = animate_billiard_trajectory(billiard, pos=billiard.sample_interior_point(), vel=(1.0, 0.35), n_bounces=100, interval=50) plt.show() .. container:: sphx-glr-animation .. raw:: html .. GENERATED FROM PYTHON SOURCE LINES 37-41 To save the animation to a file instead of (or in addition to) displaying it interactively, use e.g.:: anim.save("rectangle_billiard_animation.gif", writer="pillow", fps=25) .. GENERATED FROM PYTHON SOURCE LINES 43-45 Trajectory ---------- .. GENERATED FROM PYTHON SOURCE LINES 45-47 .. code-block:: Python fig, ax = plot_billiard_trajectory(billiard, pos=billiard.sample_interior_point(), vel=np.array([1.0, 0.35]), n_bounces=60) .. image-sg:: /api/gallery/chaos/billiards/images/sphx_glr_plot_rectangle_billiard_002.png :alt: RectangleBilliard trajectory :srcset: /api/gallery/chaos/billiards/images/sphx_glr_plot_rectangle_billiard_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 48-54 Poincare section ---------------- Each ray only ever hits walls at two possible incidence angles (one for the horizontal walls, one for the vertical walls), so the section is made up of a discrete set of horizontal line segments rather than the dense chaotic sea seen in defocusing billiards. .. GENERATED FROM PYTHON SOURCE LINES 54-57 .. code-block:: Python fig, ax = plot_poincare_section(billiard, n_rays=25, n_bounces=150) plt.show() .. image-sg:: /api/gallery/chaos/billiards/images/sphx_glr_plot_rectangle_billiard_003.png :alt: RectangleBilliard Poincare section :srcset: /api/gallery/chaos/billiards/images/sphx_glr_plot_rectangle_billiard_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 8.155 seconds) .. _sphx_glr_download_api_gallery_chaos_billiards_plot_rectangle_billiard.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_rectangle_billiard.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_rectangle_billiard.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_rectangle_billiard.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_