.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/pde/heat/plot_01_fourier_heat_series.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 or to run this example in your browser via JupyterLite. .. rst-class:: sphx-glr-example-title .. _sphx_glr_api_gallery_pde_heat_plot_01_fourier_heat_series.py: Fourier's heat equation: a sine series whose modes decay ======================================================== Joseph Fourier solved :math:`u_t = \alpha u_{xx}` on a rod with ice-cold ends by writing the initial temperature as a sine series. Each mode :math:`\sin(k\pi x)` then evolves on its own, decaying like :math:`e^{-\alpha (k\pi)^2 t}`: sharp features (high :math:`k`) vanish almost instantly, leaving the smooth fundamental mode. This script computes the sine coefficients of a square-wave start, watches the series smooth out, and checks it against a finite-difference solve. .. GENERATED FROM PYTHON SOURCE LINES 15-25 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.pde import HeatEquation1D, fourier_sine_coefficients, heat_series_solution def square(x): return np.where((x > 0.25) & (x < 0.75), 1.0, 0.0) .. GENERATED FROM PYTHON SOURCE LINES 26-28 The initial temperature as a sine series ---------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 28-43 .. code-block:: Python b = fourier_sine_coefficients(square, 60) for k in range(1, 8): print(f"b_{k} = {b[k - 1]:+.4f}") x = np.linspace(0.0, 1.0, 400) fig1, ax1 = plt.subplots(figsize=(7, 4)) ax1.plot(x, square(x), color="black", lw=2, label="initial temperature") for n_terms, color in ((1, "goldenrod"), (5, "darkorange"), (60, "firebrick")): ax1.plot(x, heat_series_solution(b[:n_terms], x, t=0.0), color=color, label=f"{n_terms} sine terms") ax1.set_xlabel("$x$") ax1.set_title("A square wave as a sum of sine modes") ax1.legend(fontsize=8) fig1.tight_layout() .. image-sg:: /api/gallery/pde/heat/images/sphx_glr_plot_01_fourier_heat_series_001.png :alt: A square wave as a sum of sine modes :srcset: /api/gallery/pde/heat/images/sphx_glr_plot_01_fourier_heat_series_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none b_1 = +0.9003 b_2 = +0.0000 b_3 = -0.3001 b_4 = -0.0000 b_5 = -0.1801 b_6 = +0.0000 b_7 = +0.1286 .. GENERATED FROM PYTHON SOURCE LINES 44-48 High modes die first -------------------- The :math:`k`-th mode's decay rate grows like :math:`k^2`, so after a short time only the first mode is visible. .. GENERATED FROM PYTHON SOURCE LINES 48-58 .. code-block:: Python fig2, ax2 = plt.subplots(figsize=(7, 4)) for t, color in zip((0.0, 0.002, 0.01, 0.05, 0.2), plt.get_cmap("viridis")(np.linspace(0, 0.9, 5)), strict=False): ax2.plot(x, heat_series_solution(b, x, t), color=color, label=f"t = {t}") ax2.set_xlabel("$x$") ax2.set_ylabel("$u$") ax2.set_title("Fourier's series solution smooths the square wave") ax2.legend(fontsize=8) fig2.tight_layout() .. image-sg:: /api/gallery/pde/heat/images/sphx_glr_plot_01_fourier_heat_series_002.png :alt: Fourier's series solution smooths the square wave :srcset: /api/gallery/pde/heat/images/sphx_glr_plot_01_fourier_heat_series_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 59-61 The series agrees with a finite-difference solve ------------------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 61-68 .. code-block:: Python heat = HeatEquation1D(square, n=201) sol = heat.solve_theta(0.05, dt=1e-4, theta=0.5) series = heat_series_solution(b, sol.x, 0.05) print(f"max |finite difference - Fourier series| at t = 0.05: {np.max(np.abs(sol.final - series)):.2e}") plt.show() .. rst-class:: sphx-glr-script-out .. code-block:: none max |finite difference - Fourier series| at t = 0.05: 4.24e-03 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.260 seconds) .. _sphx_glr_download_api_gallery_pde_heat_plot_01_fourier_heat_series.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../../../lite/lab/index.html?path=api/gallery/pde/heat/plot_01_fourier_heat_series.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_fourier_heat_series.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_fourier_heat_series.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_fourier_heat_series.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_