.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/pde/advection/plot_02_lax_friedrichs.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_advection_plot_02_lax_friedrichs.py: The Lax-Friedrichs scheme: stabilizing centered differences =========================================================== The centered FTCS scheme :math:`u_j^{n+1} = u_j^n - \tfrac{\nu}{2}(u_{j+1}^n - u_{j-1}^n)` is unconditionally unstable for advection. Peter Lax (1954), following Friedrichs, replaced :math:`u_j^n` by the neighbour average :math:`\tfrac12(u_{j+1}^n + u_{j-1}^n)`. That one change makes the scheme stable for :math:`\nu \le 1`, and its conservation form carries over to nonlinear conservation laws and shocks -- at the price of heavy numerical diffusion. .. GENERATED FROM PYTHON SOURCE LINES 15-24 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.pde import AdvectionEquation1D, max_amplification G_NAME = {"ftcs": "ftcs_advection"} # amplification-factor name of each advection scheme adv = AdvectionEquation1D(lambda x: np.exp(-200 * (x - 0.5) ** 2), n=200) .. GENERATED FROM PYTHON SOURCE LINES 25-27 FTCS blows up; averaging tames it --------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 27-39 .. code-block:: Python fig, ax = plt.subplots() ax.plot(adv.x, adv.exact(0.5), "k--", lw=2, label="exact") for scheme in ("ftcs", "lax_friedrichs"): sol = adv.solve(0.5, dt=0.8 * adv.dx, scheme=scheme) print(f"{scheme:>15}: max |u| = {np.max(np.abs(sol.final)):.3g}, max |G| = {max_amplification(G_NAME.get(scheme, scheme), 0.8):.4f}") ax.plot(sol.x, np.clip(sol.final, -1.0, 2.0), label=scheme) ax.set_ylim(-1.0, 2.0) ax.set_xlabel("$x$") ax.set_title("Same centered difference, with and without averaging (nu = 0.8)") ax.legend() .. image-sg:: /api/gallery/pde/advection/images/sphx_glr_plot_02_lax_friedrichs_001.png :alt: Same centered difference, with and without averaging (nu = 0.8) :srcset: /api/gallery/pde/advection/images/sphx_glr_plot_02_lax_friedrichs_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none ftcs: max |u| = 1.88, max |G| = 1.2806 lax_friedrichs: max |u| = 0.831, max |G| = 1.0000 .. GENERATED FROM PYTHON SOURCE LINES 40-43 Lax-Friedrichs' numerical diffusion grows as nu shrinks ------------------------------------------------------- Its modified equation has diffusion coefficient dx^2 (1 - nu^2) / (2 dt). .. GENERATED FROM PYTHON SOURCE LINES 43-54 .. code-block:: Python fig, ax = plt.subplots() ax.plot(adv.x, adv.exact(1.0), "k--", lw=2, label="exact") for nu in (0.3, 0.6, 0.95): sol = adv.solve(1.0, dt=nu * adv.dx, scheme="lax_friedrichs") ax.plot(sol.x, sol.final, label=f"nu = {nu}") ax.set_xlabel("$x$") ax.set_title("Lax-Friedrichs after one period") ax.legend() plt.show() .. image-sg:: /api/gallery/pde/advection/images/sphx_glr_plot_02_lax_friedrichs_002.png :alt: Lax-Friedrichs after one period :srcset: /api/gallery/pde/advection/images/sphx_glr_plot_02_lax_friedrichs_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.182 seconds) .. _sphx_glr_download_api_gallery_pde_advection_plot_02_lax_friedrichs.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/advection/plot_02_lax_friedrichs.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_lax_friedrichs.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_lax_friedrichs.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_lax_friedrichs.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_