Assignment 5 onlineLecture 14 (Pivoted LU decomposition) onlineLecture 13 (Linear systems of equations) onlineLecture 12 (Gaussian quadrature) onlineA4 onlineTypos in L1-11 fixed, Quiz 3 in the exercise bank, solutions published, study guide availableLecture 10/11: Newton-Cotes quadratureLecture 9: Hermite interpolation availableAssignment 3 availableLecture 8 onlineLecture 7 onlineSchedule now available in "Admin"Problem class exercises now onlineLecture 6 onlineQuiz 1 solutions onlineLectures 2-3 exercises have solutions!Lecture 1 exercises have solutions!
Lecture 14: Chapter 5: Linear systems of equations
Warning
These lecture notes are mainly a record of what we discussed in class. They should not be used as a replacement for attending lectures or reading books. Please let me know if you find any errors or have any questions about the material.
TipHypothesis
You can use the Hypothesis commenting system available on the right side panel (you can login with your U/Google account). This allows you annotate the notes (private) and leave comments or reply to other people’s comments (public).
Note
I encourage you to download the lecture notes and play around with the Julia code.
If \(L\) is unit lower triangular and \(U\) is upper triangular, then we solve \(LUx = b\) by solving \(Ly = b\) (forwards substitution) and then \(Ux = y\) (backwards substitution): this requires \(O(n^2)\) operations.
14.2 Computing LU decomposition
Algorithm: set \(A^{(1)} := A\) and, for \(k = 1, \dots, n\), let \(u_k\) be the \(k\)-th row of \(A^{(k)}\), \(\ell_k\) the \(k\)-th column of \(A^{(k)}\) divided by \((u_k)_k\), and \[
\begin{align}
A^{(k+1)} := A^{(k)} - \ell_k u_k.
\end{align}
\] Then \(A = LU\) where \(L\) has columns \(\ell_k\) and \(U\) has rows \(u_k\).
Code
functionLU_decomp( A ) n =size(A, 1) L =diagm(ones(n)) U =zeros(n, n)for j ∈1:n-1 U[j, :] = A[j, :] L[:, j] = A[:, j] / U[j, j] A = A - L[:, j] * U[j, :]'end U[n, n] = A[n, n]returntril(L), triu(U)endA = [ 2043 ; -45-7-10 ; 1152-9/2; -202-13](L, U) =LU_decomp(A)display( L )display( U )
A = [234 ; 4510 ; 482]b = [1; 0 ; -1](L, U) =LU_decomp(A) y =forwardSub(L, b)x =backwardSub(U, y)
3-element Vector{Float64}:
-20.0
9.0
3.5
We can use Julia’s A \ b which is described as follows:
“Matrix division using a polyalgorithm. For input matrices A and B, the result X is such that AX = B when A is square. The solver that is used depends upon the structure of A. If A is upper or lower triangular (or diagonal), no factorization of A is required and the system is solved with either forward or backward substitution. For non-triangular square matrices, an LU factorization is used.”
The first row of \(U\) is \(u_1 = (2, 0, 4, 3)\) and so the first column of \(L\) is \(\ell_1 = (1, -1, 1/2, -2)^\intercal\) (the first column of \(A\) divided by \((u_1)_1\)). Therefore, we have
Now we have a problem! In the algorithm above, we would set \(u_2 = (0, 0, 6, -10)\) and then let \(\ell_2\) be the second column of \(A^{(2)}\) divided by \((u_2)_2\) but this is zero!
Let’s see what happens when we apply this algorithm to \[
\begin{align}
A = \begin{pmatrix}
2 & 0 & 4 & 3 \\
-2 & 0 & 2 & -13 \\
1 & 15 & 2 & -4.5 \\
-4 & 5 & -7 & -10
\end{pmatrix}
\nonumber
\end{align}
\]
Code
A = [2043 ; -202-13 ; 1152-4.5 ; -45-7-10];L, U =LU_decomp(A)L
4×4 Matrix{Float64}:
1.0 0.0 0.0 0.0
-1.0 NaN 0.0 0.0
0.5 Inf NaN 0.0
-2.0 Inf NaN 1.0
After the first step, the \((2,2)\) entry is zero, so LU_decomp divides by zero.
Before, we chose \(u_k\) to be the \(k\)-th row of \(A^{(k)}\). Now we instead choose the \(p(k)\) row so that we maximise the absolute value of \([A^{(k)}]_{p(k),k}\). Let’s consider an example:
Example 14.1 Use LU decomposition to solve \(Ax = b\) where \(b = (0, 1, -1)^\intercal\) and
First, we choose \(p(1)\) so that \(|A_{p(1),1}|\) is maximised: that is, \(p(1) = 3\). Then, we let \(u_1\) be the \(p(1)\) row of \(A\): that is, \(u_1 = (2, 0, 3)\) and \(\ell_1\) be the first column of \(A\) divided by \((u_1)_1=2\): that is, \(\ell_1 = ( 1/2, 0, 1 )^\intercal\). At this stage we have
We choose \(p(2)\) to maximise \(|A^{(2)}_{p(2),2}|\) and so we set \(p(2) = 2\) and \(u_2 = (0, 2, 1)\). Then we set \(\ell_2\) to be the second column of \(A^{(2)}\) divided by \((u_2)_2 = 2\) (that is, \(\ell_2 = (0, 1, 0)^\intercal\)) and we have
is lower triangular (where \(P\) is the permutation matrix with a \(1\) in the \((i,p(i))\) entry). Since \(A = LU\), we have \(PA = (PL)U\) and so \(Ax = b\) is equivalent to
\[
\begin{align}\nonumber
(PL) U x = Pb
\end{align}
\]
which we may solve using forwards and backwards substitution as before.
Exercise 14.4 Use the PLU decomposition as above to solve
Answer: We get better conditioning by choosing the largest pivot. Later, we introduce the condition number of a matrix, written \(\kappa(A)\) (Definition 14.2), which will make the following discussion rigorous.
It turns out that this matrix is well conditioned for all small \(\epsilon\). Here, we can see the condition number is bounded for all small \(\epsilon\):
Code
M(ϵ) = [-ϵ 1; 1-1]plot( ϵ->cond(M(ϵ)), -1/2, 1/2, title="Condition number of M", xlabel="ϵ")
If we did not use pivoting, the LU decomposition of \(M(\epsilon)\) is
14.5 Condition numbers of linear systems of equations
In order to fully understand the previous section, we need to make sense of the condition number \(\kappa(A)\). Recall (Definition 3.1) that when we were considering functions \(f : \mathbb R \to \mathbb R\), the condition number was (approximately) the constant of proportionality between the relative error in the input to the relative error of the output. That is, if \(x \approx \tilde{x}\) then, we have
We now take this idea but the “input” is the vector \(b\) and the “output” is \(x\) solving the linear system \(A x = b\). Again, the condition number measures how sensitive the solution \(x\) is to errors in the data \(b\). Roughly speaking, if the condition number is \(10^\rho\), we expect to lose \(\rho\) digits of accuracy when we go from \(b\) to \(x\). This made perfect sense on \(\mathbb R\) (with the absolute value) but now we have many choices for the norm. We shall fix the Euclidean norm for the remainder of the course:
Notice that, we have \(|Ax| \leq \|A\| |x|\) for all \(x\). We are now ready to define the condition number of the matrix \(A\) (or the corresponding linear system).
Suppose we have the exact data \(b\) and exact solution \(x\) (solving \(Ax=b\)) and we have the approximate data \(\tilde{b}\) (for example, the floating point representation of \(b\)) and corresponding solution \(\tilde{x}\) solving \(A\tilde{x} = \tilde{b}\). First, notice that \(|b| = |Ax| \leq \|A\| |x|\) and that \(A(x - \tilde{x}) = b - \tilde{b}\) and so
Definition 14.2 (Condition number of a matrix) The condition number of an invertible matrix \(A\) is the right hand side of (14.1): \[
\begin{align}\nonumber
\kappa(A) := \|A^{-1}\| \|A\|.
\end{align}
\]
Exercise 14.5
Use the definitions directly to show that the following matrices are ill-conditioned for small \(\epsilon > 0\), \[
\begin{align}\nonumber
\begin{pmatrix}
1 & 0 \\ -\epsilon^{-1} & 1
\end{pmatrix}, \qquad
\begin{pmatrix}
-\epsilon & 1 \\ 0 & \epsilon^{-1}-1
\end{pmatrix},
\end{align}
\]
Show that \(\|A\| = \max_{|x|=1} |Ax|\),
Show that \(\|AB\| \leq \|A\|\|B\|\) and hence \(\|A^k\| \leq \|A\|^k\),
What is \(\|D\|\) for diagonal \(D\)?
A real matrix \(A\) is symmetric (i.e. \(A^T = A\)) if and only if there exists orthogonal \(P\) (i.e. \(P^{-1} = P^T\)) and diagonal \(\Lambda\) such that \(A = P\Lambda P^T\). Show that the columns of \(P\) are eigenvectors of \(A\),
Show that, for symmetric matrices \(A\), the operator norm \(\|A\|\) is equal to \(\max_i |\lambda_i|\) where \((\lambda_i)\) are the eigenvalues of \(A\).
14.6 Next time….
Linear least squares: overdetermined systems \(Ax = b\) with \(A \in \mathbb R^{m\times n}\), \(m > n\),
The normal equations and fitting data,
QR decomposition.
TipTO-DO
Exercises throughout the notes,
(Sign-up to office hours if you like)
14.7 Extra exercises
Exercise 14.6 Compute (by hand) the PLU decomposition of \[
\begin{align}\nonumber
C = \begin{pmatrix} 0 & 6 & -10 \\ 15 & 0 & -6 \\ 5 & 1 & -4 \end{pmatrix}
\end{align}
\] using the largest pivot at each step, and check your answer with lu(C). Use your answer to compute \(\det C\).
Exercise 14.7 Let \(A\) be invertible.
Show that \(\kappa(A) \geq 1\) and that \(\kappa(\alpha A) = \kappa(A)\) for all \(\alpha \not= 0\),
Show that, if \(A\) is symmetric with eigenvalues \((\lambda_i)\), then \(\kappa(A) = \max_i |\lambda_i| / \min_i |\lambda_i|\),
Compute \(\kappa(A)\) for \(A = \begin{pmatrix} 2 & 1 \\ 1 & 2 \end{pmatrix}\) and check your answer with cond(A).