.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/calculus/quadrature/plot_02_simpson_parabolas.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_02_simpson_parabolas.py: Simpson's rule: parabolas through pairs of panels ======================================================= Simpson's rule integrates cubics exactly and converges at fourth order. This example compares it with the trapezoidal rule on a smooth integrand and shows the parabolic arcs it fits. .. GENERATED FROM PYTHON SOURCE LINES 11-18 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from scipy.integrate import quad from mathematicskit.calculus import SimpsonsRule, TrapezoidalRule from mathematicskit.calculus.visualizers.plots import plot_quadrature_convergence .. GENERATED FROM PYTHON SOURCE LINES 19-21 Exact for cubics ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 21-25 .. code-block:: Python cubic = SimpsonsRule(n=2).integrate(lambda x: x**3 - 2 * x + 1, 0.0, 2.0) print(f"Simpson with 2 panels on x^3 - 2x + 1 over [0, 2]: {cubic.value} (exact 2)") .. rst-class:: sphx-glr-script-out .. code-block:: none Simpson with 2 panels on x^3 - 2x + 1 over [0, 2]: 2.0 (exact 2) .. GENERATED FROM PYTHON SOURCE LINES 26-28 The parabolic arcs ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 28-43 .. code-block:: Python f, a, b, n = (lambda x: np.exp(-x) * np.sin(3 * x) + 1), 0.0, 3.0, 6 xs = np.linspace(a, b, n + 1) fig, ax = plt.subplots() grid = np.linspace(a, b, 400) ax.plot(grid, f(grid), "k", label="f") for i in range(0, n, 2): seg = xs[i : i + 3] coeffs = np.polyfit(seg, f(seg), 2) local = np.linspace(seg[0], seg[-1], 50) ax.fill_between(local, np.polyval(coeffs, local), alpha=0.3) ax.plot(xs, f(xs), "o") ax.set_title("Simpson's rule: one parabola per pair of panels") ax.legend() .. image-sg:: /api/gallery/calculus/quadrature/images/sphx_glr_plot_02_simpson_parabolas_001.png :alt: Simpson's rule: one parabola per pair of panels :srcset: /api/gallery/calculus/quadrature/images/sphx_glr_plot_02_simpson_parabolas_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 44-46 Fourth-order convergence ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 46-51 .. code-block:: Python exact, _ = quad(f, a, b, epsabs=1e-14) ns = [2, 4, 8, 16, 32, 64, 128] ax = plot_quadrature_convergence(lambda n: TrapezoidalRule(n), f, a, b, exact, ns, label="trapezoidal O(h^2)") plot_quadrature_convergence(lambda n: SimpsonsRule(n), f, a, b, exact, ns, ax=ax, label="Simpson O(h^4)") .. image-sg:: /api/gallery/calculus/quadrature/images/sphx_glr_plot_02_simpson_parabolas_002.png :alt: Quadrature convergence :srcset: /api/gallery/calculus/quadrature/images/sphx_glr_plot_02_simpson_parabolas_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.085 seconds) .. _sphx_glr_download_api_gallery_calculus_quadrature_plot_02_simpson_parabolas.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_simpson_parabolas.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_simpson_parabolas.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_simpson_parabolas.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_