Reference:
Exercise 6.8 In this exercise you'll write a program to calculate the eigenvalues and eigenvectors of a real symmetric matrix using the QR algorithm. The first challenge is to write a program that finds the QR decomposition of a matrix. Then we'll use that decomposition to find the eigenvalues. Refer to the question in your textbook for the complete details to this exercise. b) Write a Python function that takes as its argument a real square matrix A and returns the two matrices Q and R that form its QR decomposition. As a test case, try out your function on the matrix listed in this problem description from your textbook. Check the results by multiplying Q and R together to recover the original matrix A again. You can use the numpy function dot() to perform matrix multiplication. :#Type your code here /20pts. c) Using your function, write a complete program to calculate the eigenvalues and eigenvectors of a real symmetric matrix using the QR algorithm. Continue the calculation until the magnitude of every off-diagonal element of the matrix is smaller than 106. Test your program on the example matrix above. You should find that the eigenvalues are 1, 21, -3, and-8. Out the eigenvectors and eigenvalues. :#Type your code here /20pts. d) Verify that your eigenvalues and eigenvectors satisfy the eigenvector equation: Av = λv # Type code here /10pts. e) Verify your eigenvectors and eigenvalues using the numpy.linalg function eigh(). # Type code here /10pts.
Exercise 6.8: The QR algorithm In this exercise you'll write a program to calculate the eigenvalues and eigenvectors of a real symmetric matrix using the QR algorithm. The first challenge is to write a program that finds the QR decomposition of a matrix. Then we'll use that decomposition to find the eigenvalues. As described above, the QR decomposition expresses a real square matrix A in the form A = QR, where Q is an orthogonal matrix and R is an upper-triangular matrix. Given an Nx N matrix A we can compute the QR decomposition as follows. Let us think of the matrix as a set of N column vectors ao... an-1 thus: A = ao a1 a2 I where we have numbered the vectors in Python fashion, starting from zero, which will be convenient when writing the program. We now define two new sets of vectors up... un-1 and 90... qN-1 as follows: uo uo = ao 90 uo u₁ u₁ = a₁ (qo a₁) 90, U₂ u₂ = a₂ - - (qo-a2)qo (q1-a2) 91, U₂ 5 91 92 =
and so forth. The general formulas for calculating u; and q; are i-1 uj U₁ = aj - Σ(qj aj) qj, qi u; j=0 a) Show, by induction or otherwise, that the vectors q; are orthonormal, i.e., that they satisfy 91-9f= { } if i=j, if i tj. Now, rearranging the definitions of the vectors, we have ao = |uo| qo, a₁ |u₁|q1+ (qo a1) qo, a2 = |u₂| q2 + (90-a2) 90+ (91-a2)91, and so on. Or we can group the vectors q; together as the columns of a matrix and write all of these equations as a single matrix equation uol qo a₁ qo-a2 | | | 90 91 92 A = · - ( + ) - ( + ao aj a2