Skip to content

Commit ac67751

Browse files
committed
Exercises and solutions
1 parent a4436f3 commit ac67751

5 files changed

Lines changed: 969 additions & 106 deletions

File tree

Applications_EV.ipynb

Lines changed: 58 additions & 58 deletions
Large diffs are not rendered by default.

Approximating_Eigenvalues.ipynb

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"\n",
2424
"The eigenvalues of $A$ are solutions to the quadratic equation $\\lambda^2 - (a+d)\\lambda + ad-bc = 0$, which can be written explicitly in terms of $a$, $b$, $c$, and $d$ using the quadratic formula. The challenges with larger matrices are that the polynomial is more difficult to construct, and the roots cannot be easily found with a formula.\n",
2525
"\n",
26-
"The algorithms we describe in the section are iterative methods. They generate a sequence of vectors $\\{X^{(1)}, X^{(2)}, X^{(3)}, ... \\}$ that approach a true eigenvector of the matrix under consideration. An approximation of the corresponding eigenvalue can then be computed by multiplying the approximate eigenvector by $A$. "
26+
"The algorithms we describe in this section are iterative methods. They generate a sequence of vectors $\\{X^{(1)}, X^{(2)}, X^{(3)}, ... \\}$ that approach a true eigenvector of the matrix under consideration. An approximation of the corresponding eigenvalue can then be computed by multiplying the approximate eigenvector by $A$. "
2727
]
2828
},
2929
{
@@ -79,7 +79,7 @@
7979
"\\end{equation}\n",
8080
"$$\n",
8181
"\n",
82-
"As a matter of practicality, it is common to scale the vectors in the sequence to unit length as the Power Method is applied. If the vectors in the sequence are not scaled, their magnitudes will grow of $\\lambda_1>1$ or decay if $\\lambda_1<1$. Since all components of the vectors get divided by the same factor when the vector is scaled, this step doesn't change the ultimate behavior of the sequence. The scaled sequence of vectors still approaches the direction of the eigenvector. \n",
82+
"As a matter of practicality, it is common to scale the vectors in the sequence to unit length as the Power Method is applied. If the vectors in the sequence are not scaled, their magnitudes will grow if $\\lambda_1>1$ or decay if $\\lambda_1<1$. Since all components of the vectors get divided by the same factor when the vector is scaled, this step doesn't change the ultimate behavior of the sequence. The scaled sequence of vectors still approaches the direction of the eigenvector. \n",
8383
"\n",
8484
"We choose an arbitrary $X^{(0)}$ and calculate $X^{(20)}$ using the following rule.\n",
8585
"\n",
@@ -92,7 +92,7 @@
9292
},
9393
{
9494
"cell_type": "code",
95-
"execution_count": 2,
95+
"execution_count": 1,
9696
"metadata": {},
9797
"outputs": [
9898
{
@@ -176,7 +176,7 @@
176176
"cell_type": "markdown",
177177
"metadata": {},
178178
"source": [
179-
"Indeed the difference $AX-24X$ small. Note that in this case, we can even do the calculation with integer multiplication. Notice that $X$ has 0 in the first entry and the other entries are equal. If we set these entries to 1, the result is easy to calculate even without the aid of the computer. (*Remember that we can change the magnitude of an eigenvector and it is still an eigenvector.*) \n",
179+
"Indeed the difference $AX-24X$ is small. Note that in this case, we can even do the calculation with integer multiplication. Notice that $X$ has 0 in the first entry and the other entries are equal. If we set these entries to 1, the result is easy to calculate even without the aid of the computer. (*Remember that we can change the magnitude of an eigenvector and it is still an eigenvector.*) \n",
180180
"\n",
181181
"$$\n",
182182
"\\begin{equation}\n",
@@ -191,12 +191,12 @@
191191
"cell_type": "markdown",
192192
"metadata": {},
193193
"source": [
194-
"In practice, we do not know how many iterations we need to perform in order to get a good approximation of the eigenvector eigenvalue. Instead we should specify a condition upon which we will be satisfied with the approximation and terminate the iteration. For example, since $||AX^{(m)}||\\approx \\lambda_1$ and $AX^{(m)}\\approx \\lambda_1X^{(m)}$ we might require that $AX^{(m)} - ||AX^{(m)}||X^{(m)} < \\epsilon$ for some small number $\\epsilon$ known as a tolerance. This condition ensures that $X^{(m)}$ functions roughly like an eigenvector. It is also best to include in the code a limit on the number of iterations that will be carried out. This ensures that the computation will eventually end, even if a satisfactory result has not yet been achieved."
194+
"In practice, we do not know how many iterations we need to perform in order to get a good approximation of the eigenvector. Instead we should specify a condition upon which we will be satisfied with the approximation and terminate the iteration. For example, since $||AX^{(m)}||\\approx \\lambda_1$ and $AX^{(m)}\\approx \\lambda_1X^{(m)}$ we might require that $AX^{(m)} - ||AX^{(m)}||X^{(m)} < \\epsilon$ for some small number $\\epsilon$ known as a tolerance. This condition ensures that $X^{(m)}$ functions roughly like an eigenvector. It is also best to include in the code a limit on the number of iterations that will be carried out. This ensures that the computation will eventually end, even if a satisfactory result has not yet been achieved."
195195
]
196196
},
197197
{
198198
"cell_type": "code",
199-
"execution_count": 5,
199+
"execution_count": 2,
200200
"metadata": {},
201201
"outputs": [
202202
{
@@ -251,7 +251,7 @@
251251
"cell_type": "markdown",
252252
"metadata": {},
253253
"source": [
254-
"A more common condition to require is that $||X^{(m)} - X^{(m-1})|| < \\epsilon$ for a given tolerance $\\epsilon$. This condition mere requires that the vectors in the sequence get close to one another, not that they are actually approximate an eigenvector. "
254+
"A more common condition to require is that $||X^{(m)} - X^{(m-1})|| < \\epsilon$ for a given tolerance $\\epsilon$. This condition merely requires that the vectors in the sequence get close to one another, not that they are actually approximate an eigenvector. "
255255
]
256256
},
257257
{
@@ -325,7 +325,7 @@
325325
"\\end{equation}\n",
326326
"$$\n",
327327
"\n",
328-
"If $\\lambda$ is an eigenvalue of $A$, then $\\lambda^{-1}$ is an eigenvalue of $A$. Furthermore the eigenvector of $A$ is also an eigenvector of $A^{-1}$. The important point here is that if $\\lambda_n$ is the smallest eigenvalue of $A$, then $\\lambda_n^{-1}$ is the *largest* eigenvector of $A^{-1}$. If we want to approximate the smallest eigenvalue of $A$, we can just apply the Power Method to $A^{-1}$.\n",
328+
"If $\\lambda$ is an eigenvalue of $A$, then $\\lambda^{-1}$ is an eigenvalue of $A^{-1}$. Furthermore the eigenvector of $A$ is also an eigenvector of $A^{-1}$. The important point here is that if $\\lambda_n$ is the smallest eigenvalue of $A$, then $\\lambda_n^{-1}$ is the *largest* eigenvector of $A^{-1}$. If we want to approximate the smallest eigenvalue of $A$, we can just apply the Power Method to $A^{-1}$.\n",
329329
"\n",
330330
"We demonstrate the calculation for the following $3\\times 3$ matrix.\n",
331331
"\n",
@@ -346,7 +346,7 @@
346346
},
347347
{
348348
"cell_type": "code",
349-
"execution_count": 7,
349+
"execution_count": 4,
350350
"metadata": {},
351351
"outputs": [
352352
{
@@ -358,10 +358,10 @@
358358
" [ 6.14168469e-05]\n",
359359
" [-8.94437425e-01]] \n",
360360
"\n",
361-
"Magnitude of the eigenvalue is A inverse is approximately:\n",
361+
"Magnitude of the eigenvalue of A inverse is approximately:\n",
362362
"0.3333371476391265 \n",
363363
"\n",
364-
"Magnitude of the eigenvalue is A is approximately:\n",
364+
"Magnitude of the eigenvalue of A is approximately:\n",
365365
"2.999931351114087 \n",
366366
"\n"
367367
]
@@ -390,9 +390,9 @@
390390
" \n",
391391
"print(\"Eigenvector is approximately:\")\n",
392392
"print(X,'\\n')\n",
393-
"print(\"Magnitude of the eigenvalue is A inverse is approximately:\")\n",
393+
"print(\"Magnitude of the eigenvalue of A inverse is approximately:\")\n",
394394
"print(lag.Magnitude(A_inv@X),'\\n')\n",
395-
"print(\"Magnitude of the eigenvalue is A is approximately:\")\n",
395+
"print(\"Magnitude of the eigenvalue of A is approximately:\")\n",
396396
"print(lag.Magnitude(A@X),'\\n')"
397397
]
398398
},
@@ -415,7 +415,7 @@
415415
},
416416
{
417417
"cell_type": "code",
418-
"execution_count": 8,
418+
"execution_count": 6,
419419
"metadata": {},
420420
"outputs": [
421421
{
@@ -430,7 +430,7 @@
430430
"Magnitude of the eigenvalue of A inverse is approximately:\n",
431431
"0.3333371476391265 \n",
432432
"\n",
433-
"Magnitude of the eigenvalue is A is approximately:\n",
433+
"Magnitude of the eigenvalue of A is approximately:\n",
434434
"2.999931351114087 \n",
435435
"\n"
436436
]
@@ -460,15 +460,15 @@
460460
"print(X,'\\n')\n",
461461
"print(\"Magnitude of the eigenvalue of A inverse is approximately:\")\n",
462462
"print(lag.Magnitude(sla.lu_solve(LU_factorization,X)),'\\n')\n",
463-
"print(\"Magnitude of the eigenvalue is A is approximately:\")\n",
463+
"print(\"Magnitude of the eigenvalue of A is approximately:\")\n",
464464
"print(lag.Magnitude(A@X),'\\n')"
465465
]
466466
},
467467
{
468468
"cell_type": "markdown",
469469
"metadata": {},
470470
"source": [
471-
"#### Shifts\n",
471+
"### Shifted inverse power method\n",
472472
"\n",
473473
"Using a small modification to the Inverse Power Method, we can also approximate eigenvalues that are not the smallest. For this variation of the method, we need to observe that if we \"shift\" the diagonal entries of a matrix by a scalar $\\mu$, all of the eigenvalues of the matrix are also shifted by $\\mu$. Let $A$ be an $n\\times n$ matrix with eigenvalue $\\lambda$ and corresponding eigenvector $V$, so that $AV=\\lambda V$. Then $(A-\\mu I)V = AV - \\mu V = \\lambda V - \\mu V = (\\lambda-\\mu)V$, which means that $V$ is also an eigenvector of the matrix $(A-\\mu I)$ corresponding to the eigenvalue $\\lambda -\\mu$. \n",
474474
"\n",
@@ -498,7 +498,7 @@
498498
},
499499
{
500500
"cell_type": "code",
501-
"execution_count": 9,
501+
"execution_count": 7,
502502
"metadata": {},
503503
"outputs": [
504504
{
@@ -510,7 +510,7 @@
510510
" [0.88465174]\n",
511511
" [0.14744196]] \n",
512512
"\n",
513-
"Eigenvalue is A is approximately:\n",
513+
"Eigenvalue of A is approximately:\n",
514514
"6.0\n"
515515
]
516516
}
@@ -541,7 +541,7 @@
541541
"\n",
542542
"print(\"Eigenvector is approximately:\")\n",
543543
"print(X,'\\n')\n",
544-
"print(\"Eigenvalue is A is approximately:\")\n",
544+
"print(\"Eigenvalue of A is approximately:\")\n",
545545
"print(lag.Magnitude(A@X))"
546546
]
547547
},
@@ -551,33 +551,31 @@
551551
"source": [
552552
"### Exercises\n",
553553
"\n",
554-
"Let $A$ be the matrix from the Inverse Power Method example.\n",
554+
"**Exercise 1:** Let $A$ be the matrix from the Inverse Power Method example.\n",
555555
"\n",
556556
"$$\n",
557557
"\\begin{equation}\n",
558558
"A = \\left[ \\begin{array}{rrr} 9 & -1 & -3 \\\\ 0 & 6 & 0 \\\\ -6 & 3 & 6 \\end{array}\\right]\n",
559559
"\\end{equation}\n",
560560
"$$\n",
561561
"\n",
562-
"- Use the Power Method to approximate the largest eigenvalue $\\lambda_1$. Verify that the exact value of $\\lambda_1$ is 12."
562+
"($a$) Use the Power Method to approximate the largest eigenvalue $\\lambda_1$. Verify that the exact value of $\\lambda_1$ is 12."
563563
]
564564
},
565565
{
566566
"cell_type": "code",
567-
"execution_count": 10,
567+
"execution_count": 8,
568568
"metadata": {},
569569
"outputs": [],
570570
"source": [
571-
"A = np.array([[9,-1,-3],[0,6,0],[-6,3,6]])\n",
572-
"\n",
573571
"## Code solution here."
574572
]
575573
},
576574
{
577575
"cell_type": "markdown",
578576
"metadata": {},
579577
"source": [
580-
"- Apply the Inverse Power Method with a shift of $\\mu = 10$. Explain why the results differ from those in the example."
578+
"($b$) Apply the Inverse Power Method with a shift of $\\mu = 10$. Explain why the results differ from those in the example."
581579
]
582580
},
583581
{
@@ -593,7 +591,7 @@
593591
"cell_type": "markdown",
594592
"metadata": {},
595593
"source": [
596-
"- Apply the Inverse Power Method with a shift 0f $\\mu = 7.5$ and the initial vector given below. Explain why the sequence of vectors approach the eigenvector corresponding to $\\lambda_1$\n",
594+
"($c$) Apply the Inverse Power Method with a shift of $\\mu = 7.5$ and the initial vector given below. Explain why the sequence of vectors approach the eigenvector corresponding to $\\lambda_1$\n",
597595
"\n",
598596
"$$\n",
599597
"\\begin{equation}\n",
@@ -615,15 +613,15 @@
615613
"cell_type": "markdown",
616614
"metadata": {},
617615
"source": [
618-
"Let $B$ be the following matrix.\n",
616+
"**Exercise 2:*** Let $B$ be the following matrix.\n",
619617
"\n",
620618
"$$\n",
621619
"\\begin{equation}\n",
622620
"B = \\left[ \\begin{array}{rrrr} -2 & -18 & 6 \\\\ -11 & 3 & 11 \\\\ -27 & 15 & 31 \\end{array}\\right]\n",
623621
"\\end{equation}\n",
624622
"$$\n",
625623
"\n",
626-
"- Apply the Power Method and Inverse Power Method with shifts to approximate all eigenvalues of the matrix $B$. (*Note that one of the eigenvalues of this matrix is negative.*)"
624+
"($a$) Apply the Power Method and Inverse Power Method with shifts to approximate all eigenvalues of the matrix $B$. (*Note that one of the eigenvalues of this matrix is negative.*)"
627625
]
628626
},
629627
{
@@ -639,7 +637,7 @@
639637
"cell_type": "markdown",
640638
"metadata": {},
641639
"source": [
642-
"- Check your results using the $\\texttt{eig}$ function in SciPy."
640+
"($b$) Check your results using the $\\texttt{eig}$ function in SciPy."
643641
]
644642
},
645643
{

Eigenvalues.ipynb

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"source": [
1414
"In this chapter we shift focus away from solving linear systems, and look closer at the effect of matrix multiplication. We restrict our attention now to square matrices, which define linear transformations from $\\mathbb{R}^n$ to $\\mathbb{R}^n$. In this context we will study special values called eigenvalues, and corresponding vectors called eigenvectors, that can be used to analyze the effect of a corresponding matrix.\n",
1515
"\n",
16-
"Given a square $n\\times n$ matrix $A$, a scalar $\\lambda$ is called an **eigenvalue** of $A$ if there exists some nonzero vector $V$ in $\\mathbb{R}^n$ such that $AV=\\lambda V$. The vector $V$ is the **eigenvector** associated with $\\lambda$. The equation states that when an eigenvector of $A$ is multiplied by $A$, the result is simply a multiple of the eigenvector. In general, there may be multiple eigenvalues associated with a given matrix, and we will label them as $\\lambda_1$, $\\lambda_2$, etc., to keep an orderly notation. We will lable eigenvectors in a similar way in order to track which eigenvectors are associated with which eigenvalues. \n",
16+
"Given a square $n\\times n$ matrix $A$, a scalar $\\lambda$ is called an **eigenvalue** of $A$ if there exists some nonzero vector $V$ in $\\mathbb{R}^n$ such that $AV=\\lambda V$. The vector $V$ is the **eigenvector** associated with $\\lambda$. The equation states that when an eigenvector of $A$ is multiplied by $A$, the result is simply a multiple of the eigenvector. In general, there may be multiple eigenvalues associated with a given matrix, and we will label them as $\\lambda_1$, $\\lambda_2$, etc., to keep an orderly notation. We will label eigenvectors in a similar way in order to track which eigenvectors are associated with which eigenvalues. \n",
1717
"\n",
1818
"We will visualize examples in $\\mathbb{R}^2$. "
1919
]
@@ -288,13 +288,59 @@
288288
"$$"
289289
]
290290
},
291+
{
292+
"cell_type": "markdown",
293+
"metadata": {},
294+
"source": [
295+
"**Exercise 2:** Find the eigenvalues and the corresponding eigenvectors of the following matrix $R$ that represents the reflection transformation about the line $ x_1 = x_2 $.\n",
296+
"\n",
297+
"\n",
298+
"$$\n",
299+
"\\begin{equation}\n",
300+
"R = \\left[ \\begin{array}{cc} 0 & 1 \\\\ 1 & 0 \\end{array}\\right]\n",
301+
"\\end{equation}\n",
302+
"$$"
303+
]
304+
},
305+
{
306+
"cell_type": "code",
307+
"execution_count": null,
308+
"metadata": {},
309+
"outputs": [],
310+
"source": [
311+
"## Code solution here"
312+
]
313+
},
314+
{
315+
"cell_type": "markdown",
316+
"metadata": {},
317+
"source": [
318+
"**Exercise 3:** Find a matrix that represents a vertical and horizontal stretch by a factor of $2$. Then, find the eigenvalues and the eigenvectors associated with those eigenvalues. (You may have to take a look at the Planar Transformations section [Planar Transformations](Planar_Transformations.ipynb))."
319+
]
320+
},
321+
{
322+
"cell_type": "code",
323+
"execution_count": 1,
324+
"metadata": {},
325+
"outputs": [],
326+
"source": [
327+
"## Code solution here"
328+
]
329+
},
330+
{
331+
"cell_type": "markdown",
332+
"metadata": {},
333+
"source": [
334+
"**Exercise 4:** Find a matrix that represents reflection about the $x_1-$axis and find its eigenvalues and eigenvectors."
335+
]
336+
},
291337
{
292338
"cell_type": "code",
293-
"execution_count": 5,
339+
"execution_count": 3,
294340
"metadata": {},
295341
"outputs": [],
296342
"source": [
297-
"## Code solution here."
343+
"## Code solution here"
298344
]
299345
}
300346
],

0 commit comments

Comments
 (0)