.. 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_01_verhulst_logistic.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_01_verhulst_logistic.py: Verhulst's logistic growth ================================ Integrating :math:`\dot N = rN(1 - N/K)` numerically reproduces Verhulst's closed-form sigmoid exactly: growth looks exponential at first, is fastest at :math:`N = K/2`, and saturates at the carrying capacity :math:`K` from any positive starting population. .. GENERATED FROM PYTHON SOURCE LINES 12-17 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.ode_dynamics.systems.population import LogisticGrowth, logistic_growth_solution .. GENERATED FROM PYTHON SOURCE LINES 18-20 Numerical integration versus the closed form --------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 20-38 .. code-block:: Python r, K = 0.8, 100.0 t = np.linspace(0.0, 15.0, 400) fig, ax = plt.subplots(figsize=(7, 4.5)) for n0 in (2.0, 20.0, 150.0): traj = LogisticGrowth([n0], r=r, K=K).integrate((0.0, 15.0), dt=1e-2, method="rk4") ax.plot(traj.t, traj.y[:, 0], lw=3, alpha=0.4, label=f"RK4, N0={n0:g}") ax.plot(t, logistic_growth_solution(t, n0, r, K), "k--", lw=1) err = np.max(np.abs(traj.y[:, 0] - logistic_growth_solution(traj.t, n0, r, K))) print(f"N0={n0:6.1f}: max |RK4 - exact| = {err:.2e}") ax.axhline(K, color="gray", lw=0.8) ax.set_xlabel("t") ax.set_ylabel("N(t)") ax.set_title("Logistic growth approaches the carrying capacity K (dashed: exact)") ax.legend() fig.tight_layout() plt.show() .. image-sg:: /api/gallery/ode_dynamics/population/images/sphx_glr_plot_01_verhulst_logistic_001.png :alt: Logistic growth approaches the carrying capacity K (dashed: exact) :srcset: /api/gallery/ode_dynamics/population/images/sphx_glr_plot_01_verhulst_logistic_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none N0= 2.0: max |RK4 - exact| = 1.62e-09 N0= 20.0: max |RK4 - exact| = 2.50e-10 N0= 150.0: max |RK4 - exact| = 2.62e-09 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.294 seconds) .. _sphx_glr_download_api_gallery_ode_dynamics_population_plot_01_verhulst_logistic.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_verhulst_logistic.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_verhulst_logistic.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_verhulst_logistic.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_