.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/calculus/quadrature/plot_04_euler_maclaurin.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_calculus_quadrature_plot_04_euler_maclaurin.py: Euler-Maclaurin: correcting the trapezoidal rule ====================================================== Adds Euler-Maclaurin endpoint corrections, built from Bernoulli numbers and odd derivatives at the endpoints, to the trapezoidal rule. Each extra term raises the convergence order by two. .. GENERATED FROM PYTHON SOURCE LINES 11-16 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.calculus import euler_maclaurin_trapezoid .. GENERATED FROM PYTHON SOURCE LINES 17-19 Corrections for the integral of exp over [0, 1] ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 19-25 .. code-block:: Python exact = np.e - 1 for m in range(4): value = euler_maclaurin_trapezoid(np.exp, 0.0, 1.0, 8, [np.exp] * m).value print(f"{m} correction terms, n = 8: error = {abs(value - exact):.2e}") .. rst-class:: sphx-glr-script-out .. code-block:: none 0 correction terms, n = 8: error = 2.24e-03 1 correction terms, n = 8: error = 5.82e-07 2 correction terms, n = 8: error = 2.17e-10 3 correction terms, n = 8: error = 8.46e-14 .. GENERATED FROM PYTHON SOURCE LINES 26-28 Convergence with 0, 1, and 2 correction terms ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 28-38 .. code-block:: Python ns = np.array([2, 4, 8, 16, 32, 64]) fig, ax = plt.subplots() for m in range(3): errors = [abs(euler_maclaurin_trapezoid(np.exp, 0.0, 1.0, int(n), [np.exp] * m).value - exact) for n in ns] ax.loglog(ns, errors, "o-", label=f"{m} corrections: O(h^{2 * m + 2})") ax.set_xlabel("n") ax.set_ylabel("|error|") ax.legend() ax.set_title("Euler-Maclaurin corrections to the trapezoidal rule") .. image-sg:: /api/gallery/calculus/quadrature/images/sphx_glr_plot_04_euler_maclaurin_001.png :alt: Euler-Maclaurin corrections to the trapezoidal rule :srcset: /api/gallery/calculus/quadrature/images/sphx_glr_plot_04_euler_maclaurin_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 1.0, 'Euler-Maclaurin corrections to the trapezoidal rule') .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.054 seconds) .. _sphx_glr_download_api_gallery_calculus_quadrature_plot_04_euler_maclaurin.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_04_euler_maclaurin.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_04_euler_maclaurin.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_04_euler_maclaurin.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_