.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/pde/heat/plot_04_heat_animation.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_04_heat_animation.py: Animated: the heat equation smoothing a rough initial profile ============================================================= Under :math:`u_t = \alpha u_{xx}` with :math:`u(0) = u(1) = 0`, the Fourier mode :math:`\sin(n\pi x)` decays like :math:`e^{-\alpha n^2 \pi^2 t}`: the higher the frequency, the faster it dies. Watching a jagged profile evolve shows exactly that: the wiggles vanish almost at once, then the remaining hump sinks slowly as the single slowest mode. The dashed curve is Fourier's series solution. .. GENERATED FROM PYTHON SOURCE LINES 14-33 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.pde import HeatEquation1D, fourier_sine_coefficients, heat_series_solution from mathematicskit.pde.visualizers import animate_solution # In Jupyter or JupyterLite, show animations as an HTML/JavaScript player # (no ffmpeg needed). plt.rcParams["animation.html"] = "jshtml" def u0(x): return np.sin(np.pi * x) + 0.4 * np.sin(5 * np.pi * x) + 0.25 * np.sin(13 * np.pi * x) heat = HeatEquation1D(u0, n=101) sol = heat.solve_theta(0.15, dt=5e-4, theta=0.5) # Crank-Nicolson coefficients = fourier_sine_coefficients(u0, n_terms=20) .. GENERATED FROM PYTHON SOURCE LINES 34-39 Fast modes die first -------------------- The :math:`\sin(13\pi x)` wiggles fade within :math:`t \approx 0.001` and :math:`\sin(5\pi x)` by :math:`t \approx 0.01`, so the frames are spaced geometrically in time to catch both. .. GENERATED FROM PYTHON SOURCE LINES 39-44 .. code-block:: Python frames = np.concatenate([[0], np.geomspace(1, len(sol.t) - 1, 70)]) anim = animate_solution(sol, exact=lambda x, t: heat_series_solution(coefficients, x, t), frames=frames, interval=90) anim .. container:: sphx-glr-animation .. raw:: html
.. GENERATED FROM PYTHON SOURCE LINES 45-46 .. code-block:: Python plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 3.628 seconds) .. _sphx_glr_download_api_gallery_pde_heat_plot_04_heat_animation.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_04_heat_animation.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_04_heat_animation.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_04_heat_animation.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_04_heat_animation.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_