Precept 5: LU

In this precept, we will investigate LU factorization. Recall the versions of LU factorization discussed in class:

Task 1

Implement LU factorization without pivoting. In particular, write a function

$$ L, U = \text{lu\_nopivot}(A) $$

that takes an $n \times n$ matrix $A$ and outputs $n \times n$ matrices $L$ and $U$. To save time, only implement LU factorization without pivoting. You can use scipy's $P, L, U = \text{scipy.linalg.lu}(A)$ for the version with partial pivoting in the following. Scipy factorizes $A = PLU$ instead of the more traditional form $ PA = LU \Rightarrow A = P^{-1} LU$

Task 2

Test LU factorization (both with and without pivoting) on $250 \times 250$ matrices $A$ with condition numbers $$ \kappa(A) = 10,10^6 $$ . Check the relative error in the Frobenius norm $$ \text{relerr} = \|LU - A \|_F / \|A\|_F $$ or $$ \text{relerr} = \| LU - PA\|_F / \|A\|_F $$ Repeat each of these tests several times.

Task 3

For certain examples, partial pivoting has no effect and the values in $U$ can become massive. Consider the following matrix $$ A = \left( \begin{array}{rrrrrr} 1 & & & & 1 \\ -1 & 1 & & & 1 \\ -1 & -1& 1& & 1 \\ -1 & -1& -1& 1& 1 \\ -1 & -1& -1& -1& 1 \\ \end{array} \right) $$ the code to form A is given to you below. Use your $\text{lu\_nopivot}$ code on this matrix for $n = 25,50,100,200$ and check the relative error as before $$ \text{relerr} =\| LU - A\|_F / \|A\|_F $$ Additionally, visualize the matrices $U$ and $L$ using plt.imshow(..., interpolation='none') In general, when working with matrices, it is often useful to visualize them using imxhow if they are too big to print on the screen (say bigger than $6 \times 6$).

Task 4

Implement a solver for lower and upper triangular systems of linear equations, and then use LU to solve linear systems. Initially, test your solver by creating a $6 \times 6$ linear system with condition number $\kappa(A) = 2$, preforming an $L U$ decomposition using LU with partial pivoting and solving the linear systems. After this code works, work up to testing your solver starting with a $250 \times 250$ linear system with condition number $10^6$.