.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/pde_methods/stability/plot_02_von_neumann.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_methods_stability_plot_02_von_neumann.py: Von Neumann stability analysis ============================== John von Neumann's method, used at Los Alamos in the 1940s and published in 1950, inserts a single Fourier mode :math:`V_j^n = g^n e^{ij\xi}` into a constant-coefficient scheme. The mode grows when its amplification factor has :math:`|g(\xi)| > 1`, so stability means :math:`|g| \le 1` for every :math:`\xi`. For the theta-scheme in log-spot, :math:`g = (1 + (1-\theta)z)/(1 - \theta z)` with :math:`z \approx -\sigma^2\Delta t/\Delta x^2 (1 - \cos\xi)`: :func:`~financialkit.pde_methods.von_neumann_amplification` shows the explicit scheme failing above a diffusion number of one, Crank-Nicolson never growing but barely damping the highest modes, and the implicit scheme damping them all. .. GENERATED FROM PYTHON SOURCE LINES 19-40 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from financialkit.pde_methods import von_neumann_amplification from financialkit.pde_methods.visualizers import plot_amplification sigma, r, dx = 0.2, 0.05, 0.02 fig, axes = plt.subplots(1, 2, figsize=(11, 4)) lams = [0.5, 0.9, 1.2] res = [von_neumann_amplification(sigma, r, lam * dx**2 / sigma**2, dx, theta=0.0) for lam in lams] plot_amplification(res, labels=[f"explicit, $\\lambda$ = {lam}" for lam in lams], ax=axes[0]) axes[0].set_title("explicit scheme") big = 20 * dx**2 / sigma**2 # a diffusion number of 20 res = [von_neumann_amplification(sigma, r, big, dx, theta=t) for t in (0.5, 0.75, 1.0)] plot_amplification(res, labels=["Crank-Nicolson", "theta = 0.75", "implicit"], ax=axes[1]) axes[1].set_title(r"implicit schemes, $\lambda = 20$") fig.tight_layout() plt.show() .. image-sg:: /api/gallery/pde_methods/stability/images/sphx_glr_plot_02_von_neumann_001.png :alt: explicit scheme, implicit schemes, $\lambda = 20$ :srcset: /api/gallery/pde_methods/stability/images/sphx_glr_plot_02_von_neumann_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 41-43 The largest amplification against the diffusion number ------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 43-53 .. code-block:: Python lam = np.linspace(0.05, 3.0, 60) fig, ax = plt.subplots(figsize=(6, 4)) for theta, name in ((0.0, "explicit"), (0.5, "Crank-Nicolson"), (1.0, "implicit")): ax.plot(lam, [von_neumann_amplification(sigma, r, l * dx**2 / sigma**2, dx, theta=theta).max_modulus for l in lam], label=name) ax.axhline(1.0, color="k", lw=0.8, ls="--") ax.set_xlabel(r"diffusion number $\lambda = \sigma^2 \Delta t / \Delta x^2$") ax.set_ylabel(r"$\max_\xi |g(\xi)|$") ax.legend() plt.show() .. image-sg:: /api/gallery/pde_methods/stability/images/sphx_glr_plot_02_von_neumann_002.png :alt: plot 02 von neumann :srcset: /api/gallery/pde_methods/stability/images/sphx_glr_plot_02_von_neumann_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.117 seconds) .. _sphx_glr_download_api_gallery_pde_methods_stability_plot_02_von_neumann.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_methods/stability/plot_02_von_neumann.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_von_neumann.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_von_neumann.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_von_neumann.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_