Preface
This is a tutorial made solely for the purpose of education and it was designed for students taking Applied Math 0340. It is primarily for students who have some experience using Mathematica. If you have never used Mathematica before and would like to learn more of the basics for this computer algebra system, it is strongly recommended looking at the APMA 0330 tutorial. As a friendly reminder, don'r forget to clear variables in use and/or the kernel.
Finally, the commands in this tutorial are all written in bold black font, while Mathematica output is in regular fonts. This means that you can copy and paste all comamnds into Mathematica, change the parameters and run them. You, as the user, are free to use the scripts to your needs for learning how to use the Mathematica program, and have the right to distribute this tutorial and refer to this tutorial as long as this tutorial is accredited appropriately.
Return to computing page for the second course APMA0340
Return to Mathematica tutorial for the first course APMA0330
Return to Mathematica tutorial for the second course APMA0340
Return to the main page for the course APMA0340
Return to the main page for the course APMA0330
Return to Part II of the course APMA0340
LU-factorizationΒΆ
Recall that in Gaussian Elimination, row operations are used to change the coefficient matrix to an upper triangular matrix. The solution is then found by back substitution, starting from the last equation in the reduced system. In Gauss-Jordan Reduction, row operations are used to diagonalize the coefficient matrix, and the answers are read directly.
The goal of this section is to identify Gaussian elimination with LU factorization. The original m-by-n matrix A becomes the product of two or more special matrices that are actually triangular matrices. Namely, the factorization comes from elimination: \( {\bf A} = {\bf L}\,{\bf U} , \) where L is lower triangular m-by-m matrix and U is upper m-by-n triangular matrix. Such representation is called and LU-decomposition of LU-factorization. Computers usually solve square systems of linear equations using the LU decomposition, and it is also a key step when inverting a matrix, or computing the determinant of a matrix. The LU decomposition was introduced by a Polish astronomer, mathematician, and geodesist Tadeusz Banachiewicz (1882--1954) in 1938.
For a single linear system \( {\bf A}\,{\bf x} = {\bf b} \) of n equations in n unknowns, the methods LU-decomposition and Gauss-Jordan elimination differ in bookkeeping but otherwise involve the same number of flops. However, LU-factorization has the following advantages:
- Gaussian elimination and Gauss--Jordan elimination both use the augmented matrix \( \left[ {\bf A} \, | \, {\bf b} \right] ,\) so b must be known. In contrast, LU-decomposition uses only matrix A, so once that factorization is complete, it can be applied to any vector b.
- For large linear systems in which computer memory is at a premium, one can dispense with the storage of the 1's and zeroes that appear on or below the main diagonal of U, since those entries are known. The space that this opens up can then be used to store the entries of L.
Not every square matrix has an LU-factorization. However, if it is possible to reduce a square matrix A to row echelon form by Gaussian elimination without performing any row interchanges, then A will have an LU-decomposition, though it may not be unique.
LU-decomposition:
Step 1: rewrite the system of algebraic equations \( {\bf A} {\bf x} = {\bf b} \) as
Step 4: Substitute y into the equation \( {\bf U} {\bf x} = {\bf y} \) and solve for x.
Procedure for constructing LU-decomposition:
Step 1: Reduce \( n \times n \) matrix A
to a row echelon form U by Gaussian elimination without row interchanges, keeping track of the multipliers
used to introduce the leading coefficients (usually 1's) and multipliers used to introduce the zeroes below
the leading coefficients.
Step 2: In each position along the main diagonal L, place the reciprocal of the multiplier
that introduced the leading 1 in that position of U.
Step 3:
In each position below the main diagonal of L, place the negative of the multiplier used to introduce the zero
in that position of U.
Step 4: For the decomposition \( {\bf A} = {\bf L} {\bf U} . \)
Recall the elementary operations on the rows of a matrix that are
equivalent to premultiplying by an elementary matrix E:
(1) multiplying row i by a nonzero scalar α , denoted by \( {\bf E}_i (\alpha ) ;\)
(2) adding β times row j to row i, denoted by \( {\bf E}_{ij} (\beta ) \) (here β is any
scalar), and
(3) interchanging rows i and j, denoted by \( {\bf E}_{ij} \) (here \( i \ne j \) ),
called elementary row operations of types 1,2 and 3, respectively.
Correspondingly, a square matrix E is called an elementary matrix if it can be obtained from an identity matrix by performing a single elementary operation.
Theorem: Every elementary matrix is invertible, and the inverse is also an elementary matrix. ■
Example. Illustrations for m = 4:
Example. We find LU-factorization of the following 3-by-3 matrix:
To achieve this, we reduce A to a row echelon form U using Gaussian elimination and then calculate L by inverting the product of elementary matrices.
Reduction | Row operation | Elementary matrix | Inverse matrix |
\( \begin{bmatrix} 2&6&2 \\ -3&-8&0 \\ 4&9&2 \end{bmatrix} \) | |||
Step 1: | \( \frac{1}{2} \times \,\mbox{row 1} \) | \( {\bf E}_1 = \begin{bmatrix} 1/2&0&0 \\ 0&1&0 \\ 0&0&1 \end{bmatrix} \) | \( {\bf E}_1^{-1} = \begin{bmatrix} 2&0&0 \\ 0&1&0 \\ 0&0&1 \end{bmatrix} \) |
\( \begin{bmatrix} 1&3&1 \\ -3&-8&0 \\ 4&9&2 \end{bmatrix} \) | |||
Step 2 | \( 3 \times \,\mbox{row 1} + \mbox{row 2}\) | \( {\bf E}_2 = \begin{bmatrix} 1&0&0 \\ 3&1&0 \\ 0&0&1 \end{bmatrix} \) | \( {\bf E}_2^{-1} = \begin{bmatrix} 1&0&0 \\ -3&1&0 \\ 0&0&1 \end{bmatrix} \) |
\( \begin{bmatrix} 1&3&1 \\ 0&1&3 \\ 4&9&2 \end{bmatrix} \) | |||
Step 3 | \( -4 \times \,\mbox{row 1} + \mbox{row 3}\) | \( {\bf E}_3 = \begin{bmatrix} 1&0&0 \\ 0&1&0 \\ -4&0&1 \end{bmatrix} \) | \( {\bf E}_3^{-1} = \begin{bmatrix} 1&0&0 \\ 0&1&0 \\ 4&0&1 \end{bmatrix} \) |
\( \begin{bmatrix} 1&3&1 \\ 0&1&3 \\ 0&-3&-2 \end{bmatrix} \) | |||
Step 4 | \( 3 \times \,\mbox{row 2} + \mbox{row 3}\) | \( {\bf E}_4 = \begin{bmatrix} 1&0&0 \\ 0&1&0 \\ 0&3&1 \end{bmatrix} \) | \( {\bf E}_4^{-1} = \begin{bmatrix} 1&0&0 \\ 0&1&0 \\ 0&-3&1 \end{bmatrix} \) |
\( \begin{bmatrix} 1&3&1 \\ 0&1&3 \\ 0&0&7 \end{bmatrix} \) | |||
Step 5 | \( \frac{1}{7} \times \,\mbox{row 3} \) | \( {\bf E}_5 = \begin{bmatrix} 1&0&0 \\ 0&1&0 \\ 0&0&1/7 \end{bmatrix} \) | \( {\bf E}_5^{-1} = \begin{bmatrix} 1&0&0 \\ 0&1&0 \\ 0&0&7 \end{bmatrix} \) |
\( \begin{bmatrix} 1&3&1 \\ 0&1&3 \\ 0&0&1 \end{bmatrix} = {\bf U}\) |
L = Array[0, {n, n}];
U = Array[0, {n, n}];
AA = Array[0, {n, n}];
For[i = 1, i <= n, i++, L[[i, i]] = 1;
For[j = 1, j <= n, j++, AA[[i, j]] = A[[i, j]];
If[j != i, L[[i, j]] = 0]]];
For[k = 1, k <= n - 1, k++,
For[i = k + 1, i <= n, i++,
multiplier = N[AA[[i, k]]/AA[[k, k]]];
L[[i, k]] = multiplier;
For[j = 1, j <= n, j++,
AA[[i, j]] = AA[[i, j]] - multiplier*AA[[k, j]]]]
For[i = 1, i <= n, i++,
For[j = 1, j <= n, j++, U[[i, j]] = AA[[i, j]];
If[j < i, U[[i, j]] = 0]]];];
Print["L", "=", L // MatrixForm];
Print["U", "=", U // MatrixForm]]
LUdecompose[3, A]
U= \( \left( \begin{array}{cccc} 2& 6& 2 \\ 0& 1.& 3. \\ 0& 0& 7. \end{array} \right) \)
y=Array[y,n];
y[[1]]=b[[1]]/L[[1,1]];
For[i=2,i<=n,i++,summ=0;
For[j=1,j<=i-1,j++,summ=summ+L[[i,j]]*y[[j]]];
y[[i]]=(b[[i]]-summ)/L[[i,i]]];
Print["y","=",y//MatrixForm]]
Back substitution begins with solving the n-th equation as it has only one unknown:
x=Array[x,n];
x[[n]]=y[[n]]/U[[n,n]];
For[i=n-1,i>=1,i--, summ=0;
For[j=i+1,j<=n,j++,summ=summ+U[[i,j]]*X[[j]]];
X[[i]]=(y[[i]]-summ)/U[[i,i]]];
Print["X","=",X//MatrixForm]]
Example. Consider the \( 3\times 4 \) matrix