.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/chaos/continuous_systems/plot_duffing_oscillator.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_continuous_systems_plot_duffing_oscillator.py: Duffing Oscillator ================== The forced, damped Duffing oscillator models a particle in a double-well potential driven by a periodic force, .. math:: \ddot{x} + \delta \dot{x} + \alpha x + \beta x^3 = \gamma \cos(\omega t), or, as the first-order system in :math:`(x, v)` actually integrated, :math:`\dot{x} = v`, :math:`\dot{v} = -\delta v - \alpha x - \beta x^3 + \gamma \cos(\omega t)`. Here :math:`\delta` is the damping coefficient, :math:`\alpha` and :math:`\beta` set the (linear and cubic) restoring force -- with :math:`\alpha<0` and :math:`\beta>0`, as used below, the potential :math:`V(x)=\tfrac12\alpha x^2+\tfrac14\beta x^4` has two wells -- and :math:`\gamma`, :math:`\omega` are the forcing amplitude and frequency. For the classic chaotic parameter set below, its long-time trajectory settles onto a strange attractor in the ``(x, v)`` phase plane. .. GENERATED FROM PYTHON SOURCE LINES 22-30 .. code-block:: Python import matplotlib.pyplot as plt from physicskit.chaos.systems.continuous import Duffing from physicskit.chaos.utils.dimension import box_counting_dimension, correlation_dimension system = Duffing(delta=0.3, alpha=-1.0, beta=1.0, gamma=0.37, omega=1.2) .. GENERATED FROM PYTHON SOURCE LINES 31-35 Integrate --------- Discard an initial transient so the plotted trajectory has already settled onto the attractor. .. GENERATED FROM PYTHON SOURCE LINES 35-38 .. code-block:: Python t, states = system.trajectory(n_steps=40000, dt=0.02) states = states[5000:] .. GENERATED FROM PYTHON SOURCE LINES 39-41 Phase portrait --------------- .. GENERATED FROM PYTHON SOURCE LINES 41-49 .. code-block:: Python fig, ax = plt.subplots(figsize=(7, 6)) ax.plot(states[:, 0], states[:, 1], lw=0.2, color="crimson") ax.set_xlabel("x") ax.set_ylabel("v") ax.set_title("Duffing oscillator: chaotic attractor") plt.show() .. image-sg:: /api/gallery/chaos/continuous_systems/images/sphx_glr_plot_duffing_oscillator_001.png :alt: Duffing oscillator: chaotic attractor :srcset: /api/gallery/chaos/continuous_systems/images/sphx_glr_plot_duffing_oscillator_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 50-60 Quantifying it: a fractal attractor ---------------------------------------- The dense-but-structured phase portrait above is a strange attractor: its box-counting (capacity) dimension :math:`D_0` and correlation dimension :math:`D_2` (see :doc:`/api/gallery/chaos/chaos_metrics/plot_fractal_dimension` for both estimators applied to the Henon map) are non-integers, between the curve (:math:`D=1`) a single trajectory traces out locally and the 2D plane it never quite fills. Only every 4th stroboscopic-ish sample is kept for the correlation-dimension estimate, since it computes all :math:`O(n^2)` pairwise distances. .. GENERATED FROM PYTHON SOURCE LINES 60-78 .. code-block:: Python d0, eps0, counts0 = box_counting_dimension(states) d2, eps2, csum2 = correlation_dimension(states[::4]) print(f"Duffing attractor: box-counting D0 = {d0:.3f}, correlation D2 = {d2:.3f}") fig2, axes2 = plt.subplots(1, 2, figsize=(12, 5)) axes2[0].loglog(1.0 / eps0, counts0, "o-", color="steelblue") axes2[0].set_xlabel(r"$1/\epsilon$") axes2[0].set_ylabel(r"$N(\epsilon)$") axes2[0].set_title(f"Box counting: D0 = {d0:.3f}") axes2[1].loglog(eps2, csum2, "o-", color="darkorange") axes2[1].set_xlabel(r"$\epsilon$") axes2[1].set_ylabel(r"$C(\epsilon)$") axes2[1].set_title(f"Correlation sum: D2 = {d2:.3f}") fig2.suptitle("Duffing attractor: fractal dimension between 1 (curve) and 2 (plane)") fig2.tight_layout() plt.show() .. image-sg:: /api/gallery/chaos/continuous_systems/images/sphx_glr_plot_duffing_oscillator_002.png :alt: Duffing attractor: fractal dimension between 1 (curve) and 2 (plane), Box counting: D0 = 1.180, Correlation sum: D2 = 1.301 :srcset: /api/gallery/chaos/continuous_systems/images/sphx_glr_plot_duffing_oscillator_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Duffing attractor: box-counting D0 = 1.180, correlation D2 = 1.301 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.064 seconds) .. _sphx_glr_download_api_gallery_chaos_continuous_systems_plot_duffing_oscillator.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_duffing_oscillator.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_duffing_oscillator.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_duffing_oscillator.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_