.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/pde/advection/plot_03_lax_wendroff.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_03_lax_wendroff.py: The Lax-Wendroff scheme: second order, and dispersive wiggles ============================================================= Lax and Wendroff (1960) used the PDE itself to replace time derivatives in a Taylor expansion, :math:`u_{tt} = c^2 u_{xx}`, giving :math:`u_j^{n+1} = u_j^n - \tfrac{\nu}{2}(u_{j+1}^n - u_{j-1}^n) + \tfrac{\nu^2}{2}(u_{j+1}^n - 2u_j^n + u_{j-1}^n)`. It is second-order accurate and far less diffusive than upwind on smooth waves, but, as Godunov later proved every linear second-order scheme must, it oscillates near discontinuities. .. GENERATED FROM PYTHON SOURCE LINES 14-19 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.pde import AdvectionEquation1D .. GENERATED FROM PYTHON SOURCE LINES 20-22 A smooth wave after ten periods ------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 22-34 .. code-block:: Python adv = AdvectionEquation1D(lambda x: np.sin(2 * np.pi * x), n=50) fig, ax = plt.subplots() ax.plot(adv.x, adv.exact(10.0), "k--", lw=2, label="exact") for scheme in ("upwind", "lax_wendroff"): sol = adv.solve(10.0, dt=0.5 * adv.dx, scheme=scheme) print(f"smooth, {scheme:>12}: max error {np.max(np.abs(sol.final - adv.exact(10.0))):.3f}") ax.plot(sol.x, sol.final, "o-", ms=3, label=scheme) ax.set_xlabel("$x$") ax.set_title("Ten periods on 50 points: second order keeps the amplitude") ax.legend() .. image-sg:: /api/gallery/pde/advection/images/sphx_glr_plot_03_lax_wendroff_001.png :alt: Ten periods on 50 points: second order keeps the amplitude :srcset: /api/gallery/pde/advection/images/sphx_glr_plot_03_lax_wendroff_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none smooth, upwind: max error 0.860 smooth, lax_wendroff: max error 0.123 .. GENERATED FROM PYTHON SOURCE LINES 35-37 A square wave: Lax-Wendroff overshoots, upwind smears ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 37-50 .. code-block:: Python square = AdvectionEquation1D(lambda x: np.where((x > 0.3) & (x < 0.6), 1.0, 0.0), n=200) fig, ax = plt.subplots() ax.plot(square.x, square.exact(1.0), "k--", lw=2, label="exact") for scheme in ("upwind", "lax_wendroff"): sol = square.solve(1.0, dt=0.5 * square.dx, scheme=scheme) print(f"square, {scheme:>12}: min {sol.final.min():+.3f}, max {sol.final.max():.3f}") ax.plot(sol.x, sol.final, label=scheme) ax.set_xlabel("$x$") ax.set_title("Dispersive oscillations at discontinuities") ax.legend() plt.show() .. image-sg:: /api/gallery/pde/advection/images/sphx_glr_plot_03_lax_wendroff_002.png :alt: Dispersive oscillations at discontinuities :srcset: /api/gallery/pde/advection/images/sphx_glr_plot_03_lax_wendroff_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none square, upwind: min +0.000, max 0.997 square, lax_wendroff: min -0.232, max 1.232 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.222 seconds) .. _sphx_glr_download_api_gallery_pde_advection_plot_03_lax_wendroff.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_03_lax_wendroff.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_03_lax_wendroff.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_03_lax_wendroff.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_03_lax_wendroff.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_