.. 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_02_hermite_laguerre.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_02_hermite_laguerre.py: Hermite and Laguerre polynomials on infinite domains ========================================================== Plots the first Hermite polynomials H_n (weight e^{-x^2} on the whole line) and Laguerre polynomials L_n (weight e^{-x} on the half-line), checks their orthogonality over infinite intervals, and draws the quantum harmonic oscillator states H_n(x) e^{-x^2/2} they describe. .. GENERATED FROM PYTHON SOURCE LINES 12-20 .. code-block:: Python import math import matplotlib.pyplot as plt import numpy as np from mathematicskit.special_functions import hermite_polynomial, inner_product, laguerre_polynomial from mathematicskit.special_functions.visualizers.plots import plot_polynomial_family .. GENERATED FROM PYTHON SOURCE LINES 21-23 The two families ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 23-31 .. code-block:: Python fig, (ax_h, ax_l) = plt.subplots(1, 2, figsize=(10, 4)) plot_polynomial_family(hermite_polynomial, degrees=[0, 1, 2, 3], x_range=(-2.0, 2.0), ax=ax_h) plot_polynomial_family(laguerre_polynomial, degrees=[0, 1, 2, 3], x_range=(0.0, 8.0), ax=ax_l) ax_h.set_title(r"Hermite $H_n(x)$, weight $e^{-x^2}$ on $(-\infty, \infty)$") ax_l.set_title(r"Laguerre $L_n(x)$, weight $e^{-x}$ on $[0, \infty)$") fig.tight_layout() .. image-sg:: /api/gallery/special_functions/orthogonal_polynomials/images/sphx_glr_plot_02_hermite_laguerre_001.png :alt: Hermite $H_n(x)$, weight $e^{-x^2}$ on $(-\infty, \infty)$, Laguerre $L_n(x)$, weight $e^{-x}$ on $[0, \infty)$ :srcset: /api/gallery/special_functions/orthogonal_polynomials/images/sphx_glr_plot_02_hermite_laguerre_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 32-34 Orthogonality over infinite intervals ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 34-43 .. code-block:: Python hermite_ip = inner_product(lambda x: hermite_polynomial(1, x), lambda x: hermite_polynomial(3, x), lambda x: np.exp(-(x**2)), -np.inf, np.inf) hermite_norm = inner_product(lambda x: hermite_polynomial(3, x), lambda x: hermite_polynomial(3, x), lambda x: np.exp(-(x**2)), -np.inf, np.inf) laguerre_ip = inner_product(lambda x: laguerre_polynomial(1, x), lambda x: laguerre_polynomial(3, x), lambda x: np.exp(-x), 0.0, np.inf) laguerre_norm = inner_product(lambda x: laguerre_polynomial(3, x), lambda x: laguerre_polynomial(3, x), lambda x: np.exp(-x), 0.0, np.inf) print(f" = {hermite_ip:.2e}, = {hermite_norm:.6f} (2^3 3! sqrt(pi) = {8 * 6 * math.sqrt(math.pi):.6f})") print(f" = {laguerre_ip:.2e}, = {laguerre_norm:.6f} (expected 1)") .. rst-class:: sphx-glr-script-out .. code-block:: none = 3.12e-16, = 85.077785 (2^3 3! sqrt(pi) = 85.077785) = 7.89e-17, = 1.000000 (expected 1) .. GENERATED FROM PYTHON SOURCE LINES 44-48 Hermite functions: the quantum harmonic oscillator ----------------------------------------------------- The normalized states psi_n(x) = H_n(x) e^{-x^2/2} / sqrt(2^n n! sqrt(pi)), drawn at their energy levels E_n = n + 1/2. .. GENERATED FROM PYTHON SOURCE LINES 48-60 .. code-block:: Python x = np.linspace(-5, 5, 500) fig, ax = plt.subplots() ax.plot(x, x**2 / 2, color="gray", lw=1) for n in range(4): psi = hermite_polynomial(n, x) * np.exp(-(x**2) / 2) / math.sqrt(2**n * math.factorial(n) * math.sqrt(math.pi)) ax.plot(x, n + 0.5 + 0.8 * psi, label=rf"$\psi_{n}$, $E = {n}+\frac{{1}}{{2}}$") ax.set_ylim(0, 5) ax.set_xlabel("x") ax.set_ylabel("energy / wavefunction (offset)") ax.set_title("Harmonic oscillator states built from Hermite polynomials") ax.legend(fontsize="small") .. image-sg:: /api/gallery/special_functions/orthogonal_polynomials/images/sphx_glr_plot_02_hermite_laguerre_002.png :alt: Harmonic oscillator states built from Hermite polynomials :srcset: /api/gallery/special_functions/orthogonal_polynomials/images/sphx_glr_plot_02_hermite_laguerre_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.110 seconds) .. _sphx_glr_download_api_gallery_special_functions_orthogonal_polynomials_plot_02_hermite_laguerre.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_hermite_laguerre.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_hermite_laguerre.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_hermite_laguerre.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_