MSE3114 Computational Methods for Physicists and Materials Engineers - Assignment 4: LU decomposition and QR decomposition
Q1. (LU decomposition) Write a python code for solving a system of linear equations by LU decomposition. Written in matrix form, a system of linear equations is expressed as Ax=b. The pivoted LU decomposition on A gives A=PLU. Then, the equations become PLUx=b. We can firstly solve Lz=PTb for z by the forward substitution, and finally solve Ux=z for x by the backward substitution. CourseNana.COM
- Define a function
plu_decomposition(A)
which takes in A, does pivoted LU decomposition byscipy.linalg.lu()
, and returns the permutation matrix P, the lower triangular matrix L and the upper triangular matrix U. - Define a function
forward_subs(L, PTb)
which takes in L and PTb, does forward substitution, and returns the result z after forward substitution. - Define a function
backward_subs(U, z)
which takes in U and z, does backward substitution, and returns the result x after backward substitution. - Define a function
solve_by_lu_decomp(A, b)
which takes in A and b, does LU decomposition by callingplu_decomposition(A)
defined in Q1.1, print out the result of LU decomposition (i.e., P, L and U), does forward substitution by callingforward_subs()
defined in Q1.2 on L and PTb and returns z, does backward_substitution by callingbackward_subs()
defined in Q1.3 on U and z and returns the solution x. - Apply the function
solve_by_lu_decomp(A, b)
defined in Q1.4 to solve the following equations:⎛⎜⎝32−31−4−17131⎞⎟⎠⎛⎜⎝x1x2x3⎞⎟⎠=⎛⎜⎝19−12⎞⎟⎠. - Solve the same equations in Q1.5 by
scipy.linalg.solve()
directly.
(60 marks) CourseNana.COM
Q2. (QR decomposition) Write a python code for solving a system of linear equations by QR decomposition. Written in matrix form, a system of linear equations is expressed as Ax=b. The QR decomposition on A gives A=QR. Then, the equations become QRx=b. We can solve Rx=QTb for x by the backward substitution. CourseNana.COM
- Define a function
qr_decomposition(A)
which takes in A, does QR decomposition byscipy.linalg.qr()
, and returns the orthogonal matrix Q and the upper triangular matrix R. - Define a function
solve_by_qr_decomp(A, b)
which takes in A and b, does QR decomposition by callingqr_decomposition(A)
defined in Q2.1, print out the result of QR decomposition (i.e., Q and R), does backward_substitution by callingbackward_subs()
defined in Q1.3 on R and QTb and returns the solution x. - Apply the function
solve_by_qr_decomp(A, b)
defined in Q2.2 to solve the following equations:⎛⎜⎝132−31−44−1711⎞⎟⎠⎛⎜⎝x1x2x3⎞⎟⎠=⎛⎜⎝10.9−1⎞⎟⎠. - Solve the same equations in Q2.3 by
scipy.linalg.solve()
directly.
(40 marks) CourseNana.COM