.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/linalg/eigen/plot_06_power_iteration_animation.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_linalg_eigen_plot_06_power_iteration_animation.py: Animated: power iteration turning a vector towards the dominant eigenvector =========================================================================== Each step of power iteration, :math:`v_{k+1} = A v_k / \|A v_k\|`, stretches the component of :math:`v_k` along each eigenvector by its eigenvalue. The component along the eigenvector of largest :math:`|\lambda|` grows fastest, so :math:`v_k` swings round onto that direction, the angle shrinking by the factor :math:`|\lambda_2 / \lambda_1|` per step. .. GENERATED FROM PYTHON SOURCE LINES 14-24 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.linalg import power_iteration from mathematicskit.linalg.visualizers import animate_power_iteration # In Jupyter or JupyterLite, show animations as an HTML/JavaScript player # (no ffmpeg needed). plt.rcParams["animation.html"] = "jshtml" .. GENERATED FROM PYTHON SOURCE LINES 25-30 A 2x2 matrix in the plane ------------------------- Eigenvalues 3 and 1.8, so the angle to the dominant eigenvector shrinks by :math:`1.8/3 = 0.6` per step. The start vector lies close to the other eigenvector, to give the iterate a long way to turn. .. GENERATED FROM PYTHON SOURCE LINES 30-40 .. code-block:: Python theta = np.deg2rad(30.0) Q = np.array([[np.cos(theta), -np.sin(theta)], [np.sin(theta), np.cos(theta)]]) A = Q @ np.diag([3.0, 1.8]) @ Q.T result = power_iteration(A, tol=1e-12, max_iter=40, v0=Q[:, 1] + 0.02 * Q[:, 0]) print(f"lambda ~ {result.eigenvalues[0]:.10f} after {result.iterations} iterations") anim = animate_power_iteration(A, result) anim .. container:: sphx-glr-animation .. raw:: html
.. rst-class:: sphx-glr-script-out .. code-block:: none lambda ~ 3.0000000000 after 35 iterations .. GENERATED FROM PYTHON SOURCE LINES 41-42 .. code-block:: Python plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 6.385 seconds) .. _sphx_glr_download_api_gallery_linalg_eigen_plot_06_power_iteration_animation.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/linalg/eigen/plot_06_power_iteration_animation.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_06_power_iteration_animation.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_06_power_iteration_animation.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_06_power_iteration_animation.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_