.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/fractals_chaos/maps/plot_01_henon_attractor.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_fractals_chaos_maps_plot_01_henon_attractor.py: The Hénon attractor ========================= Iterates Hénon's map (x, y) -> (1 - 1.4 x^2 + y, 0.3 x), zooms in on its strange attractor to reveal repeated layers, and estimates its fractal and Lyapunov characteristics. .. GENERATED FROM PYTHON SOURCE LINES 11-16 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.fractals_chaos import box_counting_dimension, henon_map .. GENERATED FROM PYTHON SOURCE LINES 17-19 The attractor and a zoom ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 19-29 .. code-block:: Python orbit = henon_map(300000) fig, axes = plt.subplots(1, 2, figsize=(11, 4.5)) axes[0].plot(*orbit.T, ",k", alpha=0.5) axes[0].add_patch(plt.Rectangle((0.55, 0.15), 0.1, 0.05, fill=False, color="tab:red")) axes[0].set_title("Hénon attractor") zoom = orbit[(orbit[:, 0] > 0.55) & (orbit[:, 0] < 0.65) & (orbit[:, 1] > 0.15) & (orbit[:, 1] < 0.2)] axes[1].plot(*zoom.T, ",k") axes[1].set_title("zoom: the lines split into bundles of lines") .. image-sg:: /api/gallery/fractals_chaos/maps/images/sphx_glr_plot_01_henon_attractor_001.png :alt: Hénon attractor, zoom: the lines split into bundles of lines :srcset: /api/gallery/fractals_chaos/maps/images/sphx_glr_plot_01_henon_attractor_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 1.0, 'zoom: the lines split into bundles of lines') .. GENERATED FROM PYTHON SOURCE LINES 30-32 Dimension and sensitivity ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 32-47 .. code-block:: Python print(f"box-counting estimate {box_counting_dimension(orbit).dimension:.3f}; careful studies give about 1.26, and coarse box sizes run high") a, b = 1.4, 0.3 x, y = 0.1, 0.1 v = np.array([1.0, 0.0]) total = 0.0 steps = 100000 for _ in range(steps): v = np.array([[-2 * a * x, 1.0], [b, 0.0]]) @ v x, y = 1 - a * x * x + y, b * x norm = np.linalg.norm(v) total += np.log(norm) v /= norm print(f"largest Lyapunov exponent ~ {total / steps:.3f} (positive: chaotic)") .. rst-class:: sphx-glr-script-out .. code-block:: none box-counting estimate 1.338; careful studies give about 1.26, and coarse box sizes run high largest Lyapunov exponent ~ 0.421 (positive: chaotic) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.516 seconds) .. _sphx_glr_download_api_gallery_fractals_chaos_maps_plot_01_henon_attractor.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_henon_attractor.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_henon_attractor.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_henon_attractor.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_