.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/ode_dynamics/integrators/plot_01_euler_method.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 or to run this example in your browser via JupyterLite. .. rst-class:: sphx-glr-example-title .. _sphx_glr_api_gallery_ode_dynamics_integrators_plot_01_euler_method.py: Euler's method: first-order convergence ======================================= Euler (1768) replaced the curve by its tangent line over a short step :math:`h`: :math:`y_{n+1} = y_n + h f(t_n, y_n)`. Evaluating the slope at the *end* of the step instead gives the implicit (backward) variant :math:`y_{n+1} = y_n + h f(t_{n+1}, y_{n+1})`. Both have a local error of :math:`O(h^2)`, which adds up to a global error of :math:`O(h)`: halving the step halves the error. .. GENERATED FROM PYTHON SOURCE LINES 14-29 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.integrators import euler_integrate, implicit_euler_integrate, njit @njit def decay(state, t, params): return -state def forward_euler(y0, h, n_steps): return euler_integrate(decay, np.array([y0]), 0.0, h, n_steps, np.zeros(1))[1][:, 0] .. GENERATED FROM PYTHON SOURCE LINES 30-32 Tangent-line steps ------------------ .. GENERATED FROM PYTHON SOURCE LINES 32-46 .. code-block:: Python h, n = 0.5, 6 ts = h * np.arange(n + 1) _, ys_back = implicit_euler_integrate(decay, np.array([1.0]), 0.0, h, n, np.zeros(1)) t_fine = np.linspace(0.0, n * h, 200) fig, (ax0, ax1) = plt.subplots(1, 2, figsize=(11, 4)) ax0.plot(t_fine, np.exp(-t_fine), "k", label=r"exact $e^{-t}$") ax0.plot(ts, forward_euler(1.0, h, n), "o-", label="forward Euler (undershoots)") ax0.plot(ts, ys_back[:, 0], "s-", label="backward Euler (overshoots)") ax0.set_xlabel("t") ax0.set_title(f"y' = -y with step h = {h}") ax0.legend() .. image-sg:: /api/gallery/ode_dynamics/integrators/images/sphx_glr_plot_01_euler_method_001.png :alt: y' = -y with step h = 0.5 :srcset: /api/gallery/ode_dynamics/integrators/images/sphx_glr_plot_01_euler_method_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 47-49 Global error is proportional to h --------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 49-65 .. code-block:: Python hs = 1.0 / 2 ** np.arange(3, 11) err_fwd = [abs(forward_euler(1.0, h, round(1 / h))[-1] - np.exp(-1)) for h in hs] err_back = [abs(implicit_euler_integrate(decay, np.array([1.0]), 0.0, h, round(1 / h), np.zeros(1))[1][-1, 0] - np.exp(-1)) for h in hs] ax1.loglog(hs, err_fwd, "o-", label="forward Euler") ax1.loglog(hs, err_back, "s-", label="backward Euler") ax1.loglog(hs, 0.2 * hs, "k--", label=r"slope 1: $O(h)$") ax1.set_xlabel("h") ax1.set_ylabel("|error| at t = 1") ax1.set_title("First-order convergence") ax1.legend() fig.tight_layout() print(f"error ratio for halved h: forward {err_fwd[-2] / err_fwd[-1]:.3f}, backward {err_back[-2] / err_back[-1]:.3f}") plt.show() .. rst-class:: sphx-glr-script-out .. code-block:: none error ratio for halved h: forward 2.001, backward 1.999 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 5.463 seconds) .. _sphx_glr_download_api_gallery_ode_dynamics_integrators_plot_01_euler_method.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../../../lite/lab/index.html?path=api/gallery/ode_dynamics/integrators/plot_01_euler_method.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_euler_method.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_euler_method.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_euler_method.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_