.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/calculus/quadrature/plot_08_adaptive_quadpack.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_08_adaptive_quadpack.py: Adaptive quadrature (QUADPACK): subdividing where it is needed ================================================================ QUADPACK's adaptive strategy estimates each subinterval's integral two ways and splits only those subintervals where the estimates disagree. :class:`~mathematicskit.calculus.AdaptiveQuadrature` wraps :func:`scipy.integrate.quad` (QUADPACK's ``QAGS``). Recording every point at which it samples a sharply peaked integrand shows the evaluations crowding around the peak, and a fixed equally spaced rule needs far more evaluations to match its accuracy. .. GENERATED FROM PYTHON SOURCE LINES 15-20 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.calculus import AdaptiveQuadrature, TrapezoidalRule .. GENERATED FROM PYTHON SOURCE LINES 21-23 Where does the adaptive routine sample? --------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 23-54 .. code-block:: Python def peaked(x): return 1.0 / (1.0 + 1000.0 * (x - 0.3) ** 2) a, b = 0.0, 1.0 s = np.sqrt(1000.0) exact = (np.arctan(s * (b - 0.3)) - np.arctan(s * (a - 0.3))) / s samples = [] def recorded(x): samples.append(x) return peaked(x) result = AdaptiveQuadrature(tol=1e-10).integrate(recorded, a, b) print(f"adaptive: value={result.value:.12f}, |error|={abs(result.value - exact):.1e}, evaluations={result.n_evaluations}") grid = np.linspace(a, b, 1000) fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 5.5), sharex=True, gridspec_kw={"height_ratios": [2, 1]}) ax1.plot(grid, peaked(grid), "k") ax1.plot(samples, peaked(np.asarray(samples)), "|", color="C0", ms=10) ax1.set_title("QUADPACK samples cluster where the integrand varies") ax2.hist(samples, bins=50, color="C0") ax2.set_xlabel("x") ax2.set_ylabel("evaluations") fig.tight_layout() .. image-sg:: /api/gallery/calculus/quadrature/images/sphx_glr_plot_08_adaptive_quadpack_001.png :alt: QUADPACK samples cluster where the integrand varies :srcset: /api/gallery/calculus/quadrature/images/sphx_glr_plot_08_adaptive_quadpack_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none adaptive: value=0.094597212547, |error|=1.8e-16, evaluations=189 .. GENERATED FROM PYTHON SOURCE LINES 55-57 Matching the accuracy with a fixed, equally spaced rule ------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 57-63 .. code-block:: Python for n in (100, 1000, 10000, 100000): err = abs(TrapezoidalRule(n=n).integrate(peaked, a, b).value - exact) print(f"trapezoidal, {n + 1:6d} evaluations: |error|={err:.1e}") plt.show() .. rst-class:: sphx-glr-script-out .. code-block:: none trapezoidal, 101 evaluations: |error|=6.5e-07 trapezoidal, 1001 evaluations: |error|=6.5e-09 trapezoidal, 10001 evaluations: |error|=6.5e-11 trapezoidal, 100001 evaluations: |error|=6.5e-13 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.068 seconds) .. _sphx_glr_download_api_gallery_calculus_quadrature_plot_08_adaptive_quadpack.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_08_adaptive_quadpack.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_08_adaptive_quadpack.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_08_adaptive_quadpack.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_