.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/special_functions/orthogonal_polynomials/plot_01_legendre_chebyshev.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_special_functions_orthogonal_polynomials_plot_01_legendre_chebyshev.py: Legendre and Chebyshev polynomials on [-1, 1] =================================================== Plots the first Legendre polynomials P_n (weight 1) beside the first Chebyshev polynomials T_n (weight 1/sqrt(1 - x^2)), checks each family's orthogonality, and confirms Chebyshev's closed form T_n(cos t) = cos(nt), whose equal ripples between -1 and 1 are its minimal-oscillation property. .. GENERATED FROM PYTHON SOURCE LINES 12-18 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.special_functions import chebyshev_polynomial, inner_product, legendre_polynomial from mathematicskit.special_functions.visualizers.plots import plot_polynomial_family .. GENERATED FROM PYTHON SOURCE LINES 19-21 The two families side by side ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 21-29 .. code-block:: Python fig, (ax_p, ax_t) = plt.subplots(1, 2, figsize=(10, 4), sharey=True) plot_polynomial_family(legendre_polynomial, degrees=[0, 1, 2, 3, 4], x_range=(-1.0, 1.0), ax=ax_p) plot_polynomial_family(chebyshev_polynomial, degrees=[0, 1, 2, 3, 4], x_range=(-1.0, 1.0), ax=ax_t) ax_p.set_title(r"Legendre $P_n(x)$, weight $1$") ax_t.set_title(r"Chebyshev $T_n(x)$, weight $1/\sqrt{1-x^2}$") fig.tight_layout() .. image-sg:: /api/gallery/special_functions/orthogonal_polynomials/images/sphx_glr_plot_01_legendre_chebyshev_001.png :alt: Legendre $P_n(x)$, weight $1$, Chebyshev $T_n(x)$, weight $1/\sqrt{1-x^2}$ :srcset: /api/gallery/special_functions/orthogonal_polynomials/images/sphx_glr_plot_01_legendre_chebyshev_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 30-32 Orthogonality against each family's weight ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 32-42 .. code-block:: Python legendre_ip = inner_product(lambda x: legendre_polynomial(2, x), lambda x: legendre_polynomial(3, x), lambda x: 1.0, -1.0, 1.0) legendre_norm = inner_product(lambda x: legendre_polynomial(3, x), lambda x: legendre_polynomial(3, x), lambda x: 1.0, -1.0, 1.0) cheb_weight = lambda x: 1.0 / np.sqrt(1.0 - x**2) # noqa: E731 chebyshev_ip = inner_product(lambda x: chebyshev_polynomial(2, x), lambda x: chebyshev_polynomial(4, x), cheb_weight, -1.0, 1.0) chebyshev_norm = inner_product(lambda x: chebyshev_polynomial(4, x), lambda x: chebyshev_polynomial(4, x), cheb_weight, -1.0, 1.0) print(f" = {legendre_ip:.2e}, = {legendre_norm:.6f} (2/7 = {2 / 7:.6f})") print(f" = {chebyshev_ip:.2e}, = {chebyshev_norm:.6f} (pi/2 = {np.pi / 2:.6f})") .. rst-class:: sphx-glr-script-out .. code-block:: none = 0.00e+00, = 0.285714 (2/7 = 0.285714) = -2.13e-12, = 1.570796 (pi/2 = 1.570796) .. GENERATED FROM PYTHON SOURCE LINES 43-45 Chebyshev's closed form T_n(cos t) = cos(n t) ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 45-50 .. code-block:: Python t = np.linspace(0.0, np.pi, 400) for n in (3, 5, 8): err = np.max(np.abs(chebyshev_polynomial(n, np.cos(t)) - np.cos(n * t))) print(f"n = {n}: max |T_n(cos t) - cos(nt)| = {err:.1e}") .. rst-class:: sphx-glr-script-out .. code-block:: none n = 3: max |T_n(cos t) - cos(nt)| = 1.2e-15 n = 5: max |T_n(cos t) - cos(nt)| = 2.2e-15 n = 8: max |T_n(cos t) - cos(nt)| = 4.3e-15 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.084 seconds) .. _sphx_glr_download_api_gallery_special_functions_orthogonal_polynomials_plot_01_legendre_chebyshev.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_legendre_chebyshev.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_legendre_chebyshev.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_legendre_chebyshev.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_