.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/linalg/iterative/plot_01_cg_and_gmres_convergence.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. .. rst-class:: sphx-glr-example-title .. _sphx_glr_api_gallery_linalg_iterative_plot_01_cg_and_gmres_convergence.py: Conjugate gradient and GMRES convergence ============================================= Both are Krylov-subspace iterative solvers that never form a dense factorization of ``A``. Conjugate gradient requires ``A`` symmetric positive-definite; GMRES works for any nonsingular ``A``. This script watches both converge and plots the residual-norm history. .. GENERATED FROM PYTHON SOURCE LINES 12-18 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.linalg import GMRES, ConjugateGradient, random_spd_matrix from mathematicskit.linalg.visualizers.plots import plot_residual_history .. GENERATED FROM PYTHON SOURCE LINES 19-21 Conjugate gradient on an SPD system ----------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 21-27 .. code-block:: Python A_spd = random_spd_matrix(20, seed=4) b = np.ones(20) result_cg = ConjugateGradient(tol=1e-10).solve(A_spd, b) print(f"CG converged in {result_cg.iterations} iterations (n=20); final residual {result_cg.residual_history[-1]:.2e}") .. rst-class:: sphx-glr-script-out .. code-block:: none CG converged in 24 iterations (n=20); final residual 1.40e-11 .. GENERATED FROM PYTHON SOURCE LINES 28-30 GMRES on a general nonsymmetric system --------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 30-43 .. code-block:: Python rng = np.random.default_rng(5) A_gen = rng.uniform(-1, 1, size=(20, 20)) + 20.0 * np.eye(20) result_gmres = GMRES(tol=1e-10).solve(A_gen, b) print(f"GMRES converged in {result_gmres.iterations} iterations (n=20); final residual {result_gmres.residual_history[-1]:.2e}") fig, ax = plt.subplots(figsize=(6, 4)) plot_residual_history(result_cg, ax=ax) plot_residual_history(result_gmres, ax=ax) ax.legend() fig.tight_layout() plt.show() .. image-sg:: /api/gallery/linalg/iterative/images/sphx_glr_plot_01_cg_and_gmres_convergence_001.png :alt: Iterative solver convergence :srcset: /api/gallery/linalg/iterative/images/sphx_glr_plot_01_cg_and_gmres_convergence_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none GMRES converged in 11 iterations (n=20); final residual 1.61e-11 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.042 seconds) .. _sphx_glr_download_api_gallery_linalg_iterative_plot_01_cg_and_gmres_convergence.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_cg_and_gmres_convergence.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_cg_and_gmres_convergence.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_cg_and_gmres_convergence.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_