.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/pde/elliptic/plot_02_richardson_relaxation.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_elliptic_plot_02_richardson_relaxation.py: Richardson's relaxation: iterating toward Laplace's solution ============================================================ Lewis Fry Richardson (1911) replaced Laplace's equation by finite differences and solved the resulting equations *by hand*, sweeping over the grid and replacing each value by the average of its neighbors until nothing changed. That idea became the Jacobi iteration; updating values in place gives Gauss-Seidel, and over-relaxing each update gives SOR (Young, 1950). This script heats one side of a square plate and compares how fast the three relaxation schemes from :mod:`mathematicskit.linalg`, and conjugate gradient, converge. .. GENERATED FROM PYTHON SOURCE LINES 16-27 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.linalg.visualizers import plot_residual_history from mathematicskit.pde import solve_laplace_2d from mathematicskit.pde.visualizers import plot_field_2d n = 17 boundary = np.zeros((n, n)) boundary[:, -1] = np.sin(np.pi * np.linspace(0.0, 1.0, n)) # warm top edge .. GENERATED FROM PYTHON SOURCE LINES 28-30 The steady temperature ---------------------- .. GENERATED FROM PYTHON SOURCE LINES 30-37 .. code-block:: Python direct = solve_laplace_2d(boundary, n=(n, n)) fig1, ax1 = plt.subplots(figsize=(5.5, 4.5)) plot_field_2d(direct, ax=ax1, levels=20, cmap="inferno") ax1.set_title("Laplace's equation: a plate warmed along its top edge") fig1.tight_layout() .. image-sg:: /api/gallery/pde/elliptic/images/sphx_glr_plot_02_richardson_relaxation_001.png :alt: Laplace's equation: a plate warmed along its top edge :srcset: /api/gallery/pde/elliptic/images/sphx_glr_plot_02_richardson_relaxation_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 38-40 Relaxation sweeps ----------------- .. GENERATED FROM PYTHON SOURCE LINES 40-52 .. code-block:: Python fig2, ax2 = plt.subplots(figsize=(7, 4.5)) for method in ("jacobi", "gauss_seidel", "sor", "cg"): sol = solve_laplace_2d(boundary, n=(n, n), method=method, tol=1e-8) result = sol.solver_result print(f"{method:>12}: {result.iterations:5d} iterations, max |u - direct| = {np.max(np.abs(sol.u - direct.u)):.1e}") plot_residual_history(result, ax=ax2, label=method) ax2.set_xscale("log") ax2.set_title("Jacobi (Richardson) < Gauss-Seidel < SOR < CG") fig2.tight_layout() plt.show() .. image-sg:: /api/gallery/pde/elliptic/images/sphx_glr_plot_02_richardson_relaxation_002.png :alt: Jacobi (Richardson) < Gauss-Seidel < SOR < CG :srcset: /api/gallery/pde/elliptic/images/sphx_glr_plot_02_richardson_relaxation_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none jacobi: 812 iterations, max |u - direct| = 4.6e-08 gauss_seidel: 411 iterations, max |u - direct| = 4.4e-08 sor: 58 iterations, max |u - direct| = 8.7e-09 cg: 16 iterations, max |u - direct| = 2.9e-11 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.762 seconds) .. _sphx_glr_download_api_gallery_pde_elliptic_plot_02_richardson_relaxation.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/elliptic/plot_02_richardson_relaxation.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_richardson_relaxation.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_richardson_relaxation.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_richardson_relaxation.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_