.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/ode_dynamics/epidemics/plot_01_sir_model.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_epidemics_plot_01_sir_model.py: The Kermack-McKendrick SIR model ====================================== An epidemic grows only if :math:`R_0 S_0 > 1`, peaks when the susceptible fraction falls to :math:`1/R_0`, and burns out before infecting everyone. The simulated peak and final size agree with the closed-form Kermack-McKendrick relations. .. GENERATED FROM PYTHON SOURCE LINES 12-18 .. code-block:: Python import matplotlib.pyplot as plt from mathematicskit.ode_dynamics.systems.epidemics import SIRModel, sir_final_size, sir_peak_infected s0, i0 = 0.999, 0.001 .. GENERATED FROM PYTHON SOURCE LINES 19-21 Epidemic curves for several reproduction numbers ------------------------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 21-38 .. code-block:: Python fig, ax = plt.subplots(figsize=(7, 4.5)) for beta in (0.15, 0.3, 0.5): system = SIRModel([s0, i0, 0.0], beta=beta, gamma=0.1) traj = system.integrate((0.0, 300.0), dt=0.05, method="rk4") ax.plot(traj.t, traj.y[:, 1], label=f"R0={system.r0:.1f}") print( f"R0={system.r0:.1f}: peak I {traj.y[:, 1].max():.4f} (theory {sir_peak_infected(system.r0, s0, i0):.4f}), " f"final S {traj.y[-1, 0]:.4f} (theory {sir_final_size(system.r0, s0, i0):.4f})" ) ax.set_xlabel("t") ax.set_ylabel("infected fraction I(t)") ax.set_title("SIR epidemics: larger R0, earlier and higher peak") ax.legend() fig.tight_layout() plt.show() .. image-sg:: /api/gallery/ode_dynamics/epidemics/images/sphx_glr_plot_01_sir_model_001.png :alt: SIR epidemics: larger R0, earlier and higher peak :srcset: /api/gallery/ode_dynamics/epidemics/images/sphx_glr_plot_01_sir_model_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none R0=1.5: peak I 0.0637 (theory 0.0637), final S 0.4164 (theory 0.4161) R0=3.0: peak I 0.3008 (theory 0.3008), final S 0.0594 (theory 0.0594) R0=5.0: peak I 0.4783 (theory 0.4783), final S 0.0070 (theory 0.0070) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.702 seconds) .. _sphx_glr_download_api_gallery_ode_dynamics_epidemics_plot_01_sir_model.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_sir_model.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_sir_model.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_sir_model.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_