{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Problem 2" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 42 60]\n", " [-20 -28]] \n", "\n", " [[ 4 -8]\n", " [ 6 18]] \n", "\n", " [[ 4 -4]\n", " [-4 10]]\n" ] } ], "source": [ "A = np.array([[42, 60], [-20, -28]]);\n", "B = np.array([[4, -8], [6, 18]])\n", "C = np.array([[4, -4], [-4, 10]])\n", "print(A, \"\\n\\n\", B, \"\\n\\n\", C)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "# This powermethod function outputs a pair l, w where l is the list of approximations converging to the eigenvalue\n", "# and w is the eigenvector\n", "\n", "def powermethod(A, v, n): \n", " l = np.zeros(n)\n", " w=(1/np.linalg.norm(v))*v\n", " for k in range(n):\n", " v = A @ w\n", " newl = np.dot(v,w)\n", " l[k] = newl\n", " w=(1/np.linalg.norm(v))*v\n", " return l,w" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "v0 = np.array([1,1])\n", "alist, avect = powermethod(A,v0,20)\n", "blist, bvect = powermethod(B,v0,20)\n", "clist, cvect = powermethod(C,v0,20)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "╒═════════════════════════╤═════════════════════════╤═════════════════════════╕\n", "│ A │ B │ C │\n", "╞═════════════════════════╪═════════════════════════╪═════════════════════════╡\n", "│ 26.99999999999999644729 │ 9.99999999999999822364 │ 2.99999999999999955591 │\n", "├─────────────────────────┼─────────────────────────┼─────────────────────────┤\n", "│ 13.89801699716713834221 │ 17.94594594594594383352 │ 10.00000000000000000000 │\n", "├─────────────────────────┼─────────────────────────┼─────────────────────────┤\n", "│ 12.27385210813345217673 │ 15.92126945376868718540 │ 11.93103448275861921957 │\n", "├─────────────────────────┼─────────────────────────┼─────────────────────────┤\n", "│ 12.04463972920328629357 │ 14.57122787699872112910 │ 11.99807135969142102283 │\n", "├─────────────────────────┼─────────────────────────┼─────────────────────────┤\n", "│ 12.00741281251859504664 │ 13.79568504688623420407 │ 11.99994641661084671114 │\n", "├─────────────────────────┼─────────────────────────┼─────────────────────────┤\n", "│ 12.00123471795347640523 │ 13.31327114795233690359 │ 11.99999851156477070901 │\n", "├─────────────────────────┼─────────────────────────┼─────────────────────────┤\n", "│ 12.00020576548456929800 │ 12.99157810669037260709 │ 11.99999995865457158573 │\n", "├─────────────────────────┼─────────────────────────┼─────────────────────────┤\n", "│ 12.00003429366859286631 │ 12.76578040016991977268 │ 11.99999999885151602541 │\n", "├─────────────────────────┼─────────────────────────┼─────────────────────────┤\n", "│ 12.00000571559537121402 │ 12.60121892656163389290 │ 11.99999999996809663116 │\n", "├─────────────────────────┼─────────────────────────┼─────────────────────────┤\n", "│ 12.00000095259877852527 │ 12.47787220080673442624 │ 11.99999999999911359794 │\n", "├─────────────────────────┼─────────────────────────┼─────────────────────────┤\n", "│ 12.00000015876644532398 │ 12.38342297374618539152 │ 11.99999999999997690736 │\n", "├─────────────────────────┼─────────────────────────┼─────────────────────────┤\n", "│ 12.00000002646106800341 │ 12.30989778774626763891 │ 12.00000000000000000000 │\n", "├─────────────────────────┼─────────────────────────┼─────────────────────────┤\n", "│ 12.00000000441020375774 │ 12.25191626143521439474 │ 12.00000000000000177636 │\n", "├─────────────────────────┼─────────────────────────┼─────────────────────────┤\n", "│ 12.00000000073502981479 │ 12.20572152181351377465 │ 11.99999999999999822364 │\n", "├─────────────────────────┼─────────────────────────┼─────────────────────────┤\n", "│ 12.00000000012252243664 │ 12.16861480266319439636 │ 12.00000000000000000000 │\n", "├─────────────────────────┼─────────────────────────┼─────────────────────────┤\n", "│ 12.00000000002041034008 │ 12.13861092354648008040 │ 11.99999999999999822364 │\n", "├─────────────────────────┼─────────────────────────┼─────────────────────────┤\n", "│ 12.00000000000340349970 │ 12.11422027447587090876 │ 12.00000000000000177636 │\n", "├─────────────────────────┼─────────────────────────┼─────────────────────────┤\n", "│ 12.00000000000057731597 │ 12.09430624571495904718 │ 11.99999999999999644729 │\n", "├─────────────────────────┼─────────────────────────┼─────────────────────────┤\n", "│ 12.00000000000010125234 │ 12.07798926573218167846 │ 12.00000000000000000000 │\n", "├─────────────────────────┼─────────────────────────┼─────────────────────────┤\n", "│ 12.00000000000002486900 │ 12.06458053992080792227 │ 12.00000000000000000000 │\n", "╘═════════════════════════╧═════════════════════════╧═════════════════════════╛\n" ] } ], "source": [ "from tabulate import tabulate\n", "data = zip(alist, blist, clist)\n", "print(tabulate(data, headers=[\"A\", \"B\", \"C\"], tablefmt='fancy_grid',floatfmt=\".20f\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Interpretation\n", "It's straightforward to check that the eigenvalues for $A$ are $12$ and $2$, the eigenvalues for $B$ are $12$ and $10$, and the eigenvalues for $C$ are $12$ and $2$. The power method to find the principle eigenvalue for $A$ and $B$ will converge to $12$ with a rate of $\\frac{\\lambda_2}{\\lambda_1} = \\frac{2}{12}$ and $\\frac{\\lambda_2}{\\lambda_1} = \\frac{10}{12}$ respectively. Because $C$ is symmetric, convergence to $12$ will be at the rate $\\left(\\frac{\\lambda_2}{\\lambda_1}\\right)^2 = \\left(\\frac{2}{12}\\right)^2$ (much faster!)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.19" } }, "nbformat": 4, "nbformat_minor": 2 }