.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/ode_dynamics/population/plot_02_lotka_volterra.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_ode_dynamics_population_plot_02_lotka_volterra.py: Lotka-Volterra predator-prey cycles ========================================= Predator and prey populations rise and fall periodically, out of phase. Every orbit is closed because the Lotka-Volterra invariant :math:`V = \delta x - \gamma\ln x + \beta y - \alpha\ln y` is conserved: orbits are its level sets around the coexistence fixed point. .. GENERATED FROM PYTHON SOURCE LINES 12-19 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.ode_dynamics.systems.population import LotkaVolterra, lotka_volterra_invariant params = dict(alpha=1.1, beta=0.4, delta=0.1, gamma=0.4) .. GENERATED FROM PYTHON SOURCE LINES 20-22 Nested closed orbits and out-of-phase oscillations --------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 22-45 .. code-block:: Python fig, axes = plt.subplots(1, 2, figsize=(11, 4.5)) for x0 in (5.0, 10.0, 20.0): system = LotkaVolterra([x0, 2.75], **params) traj = system.integrate((0.0, 40.0), dt=1e-3, method="rk4") axes[0].plot(traj.y[:, 0], traj.y[:, 1], lw=1) V = lotka_volterra_invariant(traj.y[:, 0], traj.y[:, 1], **params) print(f"x0={x0:5.1f}: invariant drift over t=40 is {np.ptp(V):.1e}") fp = system.fixed_point() axes[0].plot(*fp, "k+", ms=12, label="coexistence fixed point") axes[0].set_xlabel("prey x") axes[0].set_ylabel("predators y") axes[0].set_title("Orbits are level sets of the invariant") axes[0].legend() axes[1].plot(traj.t, traj.y[:, 0], label="prey") axes[1].plot(traj.t, traj.y[:, 1], label="predators") axes[1].set_xlabel("t") axes[1].set_title("Predator peaks lag prey peaks") axes[1].legend() fig.tight_layout() plt.show() .. image-sg:: /api/gallery/ode_dynamics/population/images/sphx_glr_plot_02_lotka_volterra_001.png :alt: Orbits are level sets of the invariant, Predator peaks lag prey peaks :srcset: /api/gallery/ode_dynamics/population/images/sphx_glr_plot_02_lotka_volterra_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none x0= 5.0: invariant drift over t=40 is 2.7e-15 x0= 10.0: invariant drift over t=40 is 1.4e-14 x0= 20.0: invariant drift over t=40 is 3.4e-13 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.398 seconds) .. _sphx_glr_download_api_gallery_ode_dynamics_population_plot_02_lotka_volterra.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_lotka_volterra.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_lotka_volterra.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_lotka_volterra.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_