need help with a python physics hw Reference:
Posted: Fri May 06, 2022 6:32 am
need help with a python physics hw
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
( |u₁| q1a2 |u₂| (If this looks complicated it's worth multiplying out the matrices on the right to verify for yourself that you get the correct expressions for the aj.) Notice now that the first matrix on the right-hand side of this equation, the matrix with columns qi, is orthogonal, because the vectors qi are orthonormal, and the second matrix is upper triangular. In other words, we have found the QR decomposition A = QR. The matrices Q and R are 0-(4+4) Q = 90 91 92 R = uol qo a1 q0 a2 |u₁q1-a2 |u₂| 0 0 0 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 A = 1 4 8 4 4237 8369 4792/ Check the results by multiplying Q and R together to recover the original matrix A again. c) Using your function, write a complete program to calculate the eigenvalues and eigen- vectors 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 10-6. Test your program on the example matrix above. You should find that the eigenvalues are 1, 21, -3, and -8.
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