Due: Thursday 3/15
This problem concerns forward and backward solvers.
Recall that given an $n\times n$ lower triangular matrix $\mathbf{A}$ with non-zero diagonal and an $n$ dimensional vector $\mathbf{b}$, that we can solve the system
\[ \mathbf{A}\mathbf{x} = \mathbf{b} \]using forward substitution. This takes the exceptionally simple form in MATLAB:
n = length(b);
x = zeros(n,1);
for i=1:n
x(i) = (b(i) - A(i,1:(i-1))*x(1:(i-1)))/A(i,i);
end
When $\mathbf{A}$ is instead upper triangular this method is called backward substitution. Modify the above MATLAB code to instead use backward substitution for a given upper triangular matrix $\mathbf{A}$ and vector $\mathbf{b}$. Use this algorithm to solve the problem
\[ \mathbf{A}\mathbf{x} = \mathbf{b} \]for the following $\mathbf{A}$ and $\mathbf{b}$:
Show that the number of floating point operations for either backward or forward substitution is exactly
\[ \text{flops}(n) = n^2, \]where $n$ is the size of the matrix.
We've seen in class that the most basic $LU$ factorization algorithm may fail for some matrices because of inconvenient zeros that arise as the algorithm proceeds. We showed how this could be avoided by `pivoting' or swapping rows to allow the algorithm to continue.
Write a code in MATLAB that generates an $LU$ factorization with pivoting for a square matrix $\mathbf{A}$. Your pivoting strategy should be to swap the offending row with one that has the largest number (in absolute value) for that column. Your code must produce three quantities: the lower triangular $\mathbf{L}$ matrix with ones on the diagonal, the upper triangular $\mathbf{U}$ matrix, and a vector $p$ which is a permutation of the vector $\mathbf{p} = (1, 2, 3,\ldots n)^\top$ that keeps track of the pivots. For example if $n=3$, then $\mathbf{p} = (1 , 3, 2)$ would mean that the algorithm involved a pivot that swapped rows $2$ and $3$. For your convenience, I have included the MATLAB code lu_fac.m for $LU$ factorization without pivoting.
Apply your new pivoting algorithm to the following matrices:
A permutation matrix $\mathbf{P}$ is a matrix which, when applied to a vector, permutes it's entries. It is defined as a matrix of ones and zeros where each row and column can only have exactly one non-zero entry. It can be viewed as an identity matrix whose rows have been permuted by the specific permutation operation it encodes. For instance the matrix
\[ \mathbf{P} = \begin{bmatrix} 1 & 0 & 0\\ 0 & 0 & 1\\ 0 & 1 & 0 \end{bmatrix} \]will swap the second and third entries of any vector it acts on.
Show that for any permutation matrix $\mathbf{P}$ the inverse is operation is given by the transpose matrix. Namely,
\[ \mathbf{P}^{-1} = \mathbf{P}^{\top}. \]Write a MATLAB code which takes a permuted vector the values $1,2,\ldots n$ and computes the permutation matrix associated with it. For instance we have
\[ \begin{bmatrix}2 \\ 1 \\ 3\end{bmatrix} \Rightarrow \begin{bmatrix}0& 1 & 0\\ 1 & 0 & 0\\ 0 & 0 & 1\end{bmatrix}. \] Use this code to generate the permutation matrices for the following permuted vectors,For a given symmetric positive definite matrix $\mathbf{A}$, the Cholesky factorization $\mathbf{A} = \mathbf{U}^\top\mathbf{U}$ can be computed in MATLAB using following code:
U = triu(A); # Throw away lower triangular piece
n = length(A);
for k=1:n
U(k,k:n) = U(k,k:n)./sqrt(U(k,k)); # Divide top row by sqrt(a_ii)
for j= (k+1):n # Transform submatrix
for i = (k+1):j
U(i,j) = U(i,j) - U(k,i)*U(k,j);
end
end
end
Show that the number of floating point operations in this algorithm for an $n\times n$ matrix is given by
\[ \text{flops}(n) = \frac{1}{3}n^3 + \mathcal{O}(n^2). \]Find the condition number $\kappa_\infty(\mathbf{A})$ of the matrix
\[ \mathbf{A} =\begin{bmatrix} 1 & -2\\ -2 & \alpha \end{bmatrix} \]for $\alpha \neq 4$. What happens when $\alpha = 4$?
Let $\mathbf{D}$ be the $n\times n$ diagonal matrix with $d_1, d_2, \ldots d_n$ on the diagonal. Show that the condition number is given by
\[ \kappa_\infty(\mathbf{D}) = \frac{ \max_i |d_i|}{\min_i |d_i|} \]Let $\mathbf{P}$ be a permutation matrix. What are the condition numbers $\kappa_1(\mathbf{P})$ and $\kappa_\infty(\mathbf{P})$?