.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/pde/stability/plot_02_von_neumann_analysis.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_stability_plot_02_von_neumann_analysis.py: Von Neumann stability analysis: one Fourier mode at a time ========================================================== John von Neumann's method substitutes a single Fourier mode :math:`u_j^n = G^n e^{ij\xi}` into a linear scheme. Each step multiplies the mode by the *amplification factor* :math:`G(\xi)`, so the scheme is stable exactly when :math:`|G(\xi)| \le 1` for every wavenumber. This script plots :math:`|G|` for heat and advection schemes, recovers the FTCS limit :math:`r \le 1/2` numerically, and shows why FTCS advection is unstable for *every* time step. .. GENERATED FROM PYTHON SOURCE LINES 15-21 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.pde import max_amplification from mathematicskit.pde.visualizers import plot_amplification_factors .. GENERATED FROM PYTHON SOURCE LINES 22-24 Heat-equation schemes --------------------- .. GENERATED FROM PYTHON SOURCE LINES 24-29 .. code-block:: Python fig, axes = plt.subplots(1, 2, figsize=(11, 4)) plot_amplification_factors(["ftcs_heat", "crank_nicolson_heat", "backward_euler_heat"], 0.6, ax=axes[0]) axes[0].set_title("Heat equation, diffusion number r = 0.6") .. image-sg:: /api/gallery/pde/stability/images/sphx_glr_plot_02_von_neumann_analysis_001.png :alt: Heat equation, diffusion number r = 0.6 :srcset: /api/gallery/pde/stability/images/sphx_glr_plot_02_von_neumann_analysis_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 1.0, 'Heat equation, diffusion number r = 0.6') .. GENERATED FROM PYTHON SOURCE LINES 30-32 Advection schemes ----------------- .. GENERATED FROM PYTHON SOURCE LINES 32-37 .. code-block:: Python plot_amplification_factors(["upwind", "lax_friedrichs", "lax_wendroff", "ftcs_advection"], 0.8, ax=axes[1]) axes[1].set_title("Advection, Courant number 0.8") fig.tight_layout() .. GENERATED FROM PYTHON SOURCE LINES 38-40 Finding the stability limits numerically ---------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 40-50 .. code-block:: Python rs = np.linspace(0.3, 0.7, 401) limit = rs[np.argmax([max_amplification("ftcs_heat", r) > 1 + 1e-12 for r in rs]) - 1] print(f"FTCS heat is stable up to r = {limit:.3f} (theory: 1/2)") for nu in (0.01, 0.5, 1.0): print(f"FTCS advection, nu = {nu}: max |G| = {max_amplification('ftcs_advection', nu):.6f} (> 1 for every nu > 0)") for r in (1.0, 100.0): print(f"Crank-Nicolson, r = {r}: max |G| = {max_amplification('crank_nicolson_heat', r):.3f}") plt.show() .. rst-class:: sphx-glr-script-out .. code-block:: none FTCS heat is stable up to r = 0.500 (theory: 1/2) FTCS advection, nu = 0.01: max |G| = 1.000050 (> 1 for every nu > 0) FTCS advection, nu = 0.5: max |G| = 1.118034 (> 1 for every nu > 0) FTCS advection, nu = 1.0: max |G| = 1.414214 (> 1 for every nu > 0) Crank-Nicolson, r = 1.0: max |G| = 1.000 Crank-Nicolson, r = 100.0: max |G| = 1.000 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.191 seconds) .. _sphx_glr_download_api_gallery_pde_stability_plot_02_von_neumann_analysis.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/stability/plot_02_von_neumann_analysis.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_von_neumann_analysis.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_von_neumann_analysis.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_von_neumann_analysis.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_