.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/calculus/quadrature/plot_06_clenshaw_curtis.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_06_clenshaw_curtis.py: Clenshaw-Curtis vs. Gauss-Legendre quadrature =================================================== Compares Clenshaw-Curtis quadrature, which integrates the Chebyshev interpolant, with Gauss-Legendre quadrature on smooth integrands. The two converge at nearly the same rate. .. GENERATED FROM PYTHON SOURCE LINES 11-16 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.calculus import ClenshawCurtisQuadrature, GaussianQuadrature, clenshaw_curtis_nodes_and_weights .. GENERATED FROM PYTHON SOURCE LINES 17-19 Nodes and weights ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 19-25 .. code-block:: Python x, w = clenshaw_curtis_nodes_and_weights(8) print("Clenshaw-Curtis n = 8") for xk, wk in zip(x, w): print(f" x = {xk:+.6f}, w = {wk:.6f}") .. rst-class:: sphx-glr-script-out .. code-block:: none Clenshaw-Curtis n = 8 x = +1.000000, w = 0.015873 x = +0.923880, w = 0.146219 x = +0.707107, w = 0.279365 x = +0.382683, w = 0.361718 x = +0.000000, w = 0.393651 x = -0.382683, w = 0.361718 x = -0.707107, w = 0.279365 x = -0.923880, w = 0.146219 x = -1.000000, w = 0.015873 .. GENERATED FROM PYTHON SOURCE LINES 26-28 Convergence on two smooth integrands ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 28-41 .. code-block:: Python cases = {r"$e^x$": (np.exp, np.e - 1 / np.e), r"$1/(1+16x^2)$": (lambda t: 1 / (1 + 16 * t**2), np.arctan(4) / 2)} points = np.arange(2, 40, 2) fig, axes = plt.subplots(1, 2, figsize=(10, 4)) for ax, (name, (f, exact)) in zip(axes, cases.items()): cc = [max(abs(ClenshawCurtisQuadrature(n - 1).integrate(f, -1.0, 1.0).value - exact), 1e-17) for n in points] gl = [max(abs(GaussianQuadrature(n).integrate(f, -1.0, 1.0).value - exact), 1e-17) for n in points] ax.semilogy(points, cc, "o-", label="Clenshaw-Curtis") ax.semilogy(points, gl, "s-", label="Gauss-Legendre") ax.set_xlabel("function evaluations") ax.set_title(name) ax.legend() axes[0].set_ylabel("|error|") .. image-sg:: /api/gallery/calculus/quadrature/images/sphx_glr_plot_06_clenshaw_curtis_001.png :alt: $e^x$, $1/(1+16x^2)$ :srcset: /api/gallery/calculus/quadrature/images/sphx_glr_plot_06_clenshaw_curtis_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(70.72222222222221, 0.5, '|error|') .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.086 seconds) .. _sphx_glr_download_api_gallery_calculus_quadrature_plot_06_clenshaw_curtis.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_06_clenshaw_curtis.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_06_clenshaw_curtis.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_06_clenshaw_curtis.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_