.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/calculus/quadrature/plot_03_riemann_sums.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_03_riemann_sums.py: Riemann sums: the definition of the integral ================================================== Draws left, right, and midpoint Riemann sums for the same integrand and shows their convergence: first order for the endpoint rules, second order for the midpoint rule. .. GENERATED FROM PYTHON SOURCE LINES 11-17 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.calculus import RiemannSum from mathematicskit.calculus.visualizers.plots import plot_quadrature_convergence .. GENERATED FROM PYTHON SOURCE LINES 18-20 Three ways to sample each subinterval ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 20-33 .. code-block:: Python f, a, b, n = np.exp, 0.0, 1.0, 8 exact = np.e - 1 fig, axes = plt.subplots(1, 3, figsize=(11, 3.5), sharey=True) grid = np.linspace(a, b, 200) h = (b - a) / n for ax, rule, offset in zip(axes, ("left", "right", "midpoint"), (0.0, 1.0, 0.5)): ax.plot(grid, f(grid), "k") left_edges = a + h * np.arange(n) ax.bar(left_edges, f(left_edges + offset * h), width=h, align="edge", alpha=0.4, edgecolor="k") value = RiemannSum(n, rule).integrate(f, a, b).value ax.set_title(f"{rule}: {value:.4f} (exact {exact:.4f})") .. image-sg:: /api/gallery/calculus/quadrature/images/sphx_glr_plot_03_riemann_sums_001.png :alt: left: 1.6131 (exact 1.7183), right: 1.8279 (exact 1.7183), midpoint: 1.7172 (exact 1.7183) :srcset: /api/gallery/calculus/quadrature/images/sphx_glr_plot_03_riemann_sums_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 34-36 Convergence orders ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 36-42 .. code-block:: Python ns = [4, 8, 16, 32, 64, 128, 256] ax = None for rule in ("left", "right", "midpoint"): ax = plot_quadrature_convergence(lambda n, r=rule: RiemannSum(n, r), f, a, b, exact, ns, ax=ax, label=rule) ax.set_title("Riemann sums: O(h) at the endpoints, O(h^2) at midpoints") .. image-sg:: /api/gallery/calculus/quadrature/images/sphx_glr_plot_03_riemann_sums_002.png :alt: Riemann sums: O(h) at the endpoints, O(h^2) at midpoints :srcset: /api/gallery/calculus/quadrature/images/sphx_glr_plot_03_riemann_sums_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 1.0, 'Riemann sums: O(h) at the endpoints, O(h^2) at midpoints') .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.097 seconds) .. _sphx_glr_download_api_gallery_calculus_quadrature_plot_03_riemann_sums.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_03_riemann_sums.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_03_riemann_sums.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_03_riemann_sums.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_