Lecture #9 Math 221 

LU Decomposition with Row Switches 

 

We would now like to look at what happens when you need to do a row switch in the middle of find the LU decomposition of a matrix.  We will also look at how to solve a system of equations when we need to do a row switch.   

 

Consider the following system 

 

 x + 3y -2z +4w + 5v = 0 

2x  + 5y -3z + 8w +11v = 0 

2x + 4y -2z + 9w + 13 v = 1 

-x + 2y +3z + 2w +  v =  2 

2x + 4y -3z + w + 3v = 0 

 

The coefficient matrix is the one of interest at the present time.  We will do Gauss elimination to the coefficient matrix. 

 

Typesetting:-mrow(Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mi(  

 

Typesetting:-mrow(Typesetting:-mi(Typesetting:-mrow(Typesetting:-mi(
 

---------->   

Typesetting:-mrow(Typesetting:-mi(Typesetting:-mrow(Typesetting:-mi(
 

Typesetting:-mrow(Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mi(Typesetting:-mrow(Typesetting:-mi(Typesetting:-mrow(Typesetting:-mi(
---------->
 

Typesetting:-mrow(Typesetting:-mi(Typesetting:-mrow(Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mi(
We will need to do a row swap to continue.  
Do not try to be fancy just go to the next row that will work which is the fourth row. 

Typesetting:-mrow(Typesetting:-msub(Typesetting:-mi( <-->Typesetting:-mrow(Typesetting:-msub(Typesetting:-mi(  Typesetting:-mrow(Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mi(
We now continue on with the Gauss elimination process.
 

 

Typesetting:-mrow(Typesetting:-mi(  

--------->Typesetting:-mrow(Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mi(Typesetting:-mrow(Typesetting:-mi(
 

--------->Typesetting:-mrow(Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mi(
This is now the U in the LU decomposition.  
Do not do any mulrow operatons.  This is not allowed in the building of the L matrix.     We now need to build the L.  The row switch will now come into play.  We will start will L equal to the identity and fill in the entries as we did when we were doing the LU decomposition without row switches. 

 

L = Typesetting:-mrow(Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mi(Typesetting:-mrow(Typesetting:-mo(
, Typesetting:-mrow(Typesetting:-mi( ,  Typesetting:-mrow(Typesetting:-mi(, Typesetting:-mrow(Typesetting:-mi(, Typesetting:-mrow(Typesetting:-mi(, Typesetting:-mrow(Typesetting:-mi( which gives the matrix
 

 

L = Typesetting:-mrow(Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mi(
We now have a row switch of row 3 and row 4.  We handle this in the LU decomposition by only switching the elements in Typesetting:-mrow(Typesetting:-mi(
.  We do not switch the rows. in the Matrix L.  Notice 

 

L = Typesetting:-mrow(Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mi(
We now continue with filling in the multipliers from the Gauss elimination.
 

 

Typesetting:-mrow(Typesetting:-mi(  which leads to the L matrix that we are looking for. 

 

L = Typesetting:-mrow(Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mi(
let P be the identity matrix with row 3 and row 4 switched
 

 

P= Typesetting:-mrow(Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mi(
If we multiply P times A we will get L times U i.e.  Typesetting:-mrow(Typesetting:-mi(   but Typesetting:-mrow(Typesetting:-mi( just interchanges row 3 and row 4 of matrix a so really what we have is that
 

Typesetting:-mrow(Typesetting:-mi(= Typesetting:-mrow(Typesetting:-mi(
We now return to how to solve A
x = b using the LU decomposition 

If we multiply on the left both sides of the equation then we would have PAx =P b or  

LUx = P b.    We are now ready to discuss the way to solve the system of equations using the LU decomposition with forward and backward substitution.   Set Ux = y and solve  

Ly = Pb by forward substitution  

 

y = Typesetting:-mrow(Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mi(  we can now solve the following equation using backward substitution 

 

Ux = y 

 

x = Typesetting:-mrow(Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mi(
The advantage of the LU decomposition again is that many times you have to solve
 

Ax = b where the coefficient matrix A does not change but the right hand side b changes. The LU decomposition means that you have to do the Gauss elimination process only once and you can find the LU and the P if necessary.  Then it just forward and backwards substitution which leads to a savings in the number of operations count that are necessary to carry out the operation necessary to  

solve the system of equations.    

>
 


 

Maple Commands for the Above Example 

>
 

> with(linalg):
 

> With(LinearAlgebra):
 

The following is the sequence of Maple commands to find the LU = PA decomposition discussed above in text.   The matrix will be entered by columns.   

> A:=<<1,2,2,-1,2>|<3,5,4,2,4>|<-2,-3,-2,3,-3>|<4,8,9,2,1>|<5,11,13,1,3>>;
 

`:=`(A, Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mtable(Typesetting:-mtr(Typesetting:-mtd(Typesetting:-mn( (2.1)
 

> A1:=addrow(addrow(addrow(addrow(A,1,2,-2),1,3,-2),1,4,1),1,5,-2);
 

Typesetting:-mrow(Typesetting:-mverbatim( (2.2)
 

> A2:=addrow(addrow(addrow(A1,2,3,-2),2,4,5),2,5,-2);
 

Typesetting:-mrow(Typesetting:-mverbatim( (2.3)
 

> A3:=swaprow(A2,3,4);
 

Typesetting:-mrow(Typesetting:-mverbatim( (2.4)
 

> A4:=addrow(A3,3,5,1/6);
 

Typesetting:-mrow(Typesetting:-mverbatim( (2.5)
 

> U:=addrow(A4,4,5,6);
 

Typesetting:-mrow(Typesetting:-mverbatim( (2.6)
 

> with(LinearAlgebra):
 

You can not use the IdentityMatrix command here so I will just build the matrix      

> L:=<<1,0,0,0,0>|<0,1,0,0,0>|<0,0,1,0,0>|<0,0,0,1,0>|<0,0,0,0,1>>;
 

`:=`(L, Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mtable(Typesetting:-mtr(Typesetting:-mtd(Typesetting:-mn( (2.7)
 

> L[2,1]:=2;L[3,1]:=2;L[4,1]:=-1;L[5,1]:=2;L[3,2]:=2;L[4,2]:=-5;L[5,2]:=2;
 

 

 

 

 

 

 

2
2
-1
2
2
-5
2 (2.8)
 

We need now to swap the elements in the the L1[3,1[,L1[3,2] and the L1[4,1],L1[4,2] positions.  To make sure I am doing it correctly I will display L up to this point.  Next I will then change the elements in the above entries.  There are nice programing way to do this but we will just change the entries.   

> L;
 

Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mtable(Typesetting:-mtr(Typesetting:-mtd(Typesetting:-mn( (2.9)
 

> L[3,1]:=-1; L[3,2]:=-5;  L[4,1]:=2; L[4,2]:=2;
 

 

 

 

-1
-5
2
2 (2.10)
 

> L;
 

Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mtable(Typesetting:-mtr(Typesetting:-mtd(Typesetting:-mn( (2.11)
 

> L[5,3]:=-1/6; L[5,4]:=-6;
 

 

-`/`(1, 6)
-6 (2.12)
 

We will look at the L matrix now.   

> L;
 

Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mtable(Typesetting:-mtr(Typesetting:-mtd(Typesetting:-mn( (2.13)
 

> I5:=<<1,0,0,0,0>|<0,1,0,0,0>|<0,0,1,0,0>|<0,0,0,1,0>|<0,0,0,0,1>>;
 

`:=`(I5, Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mtable(Typesetting:-mtr(Typesetting:-mtd(Typesetting:-mn( (2.14)
 

> P:=swaprow(I5,3,4);
 

Typesetting:-mrow(Typesetting:-mverbatim( (2.15)
 

> evalm(U);
 

Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mtable(Typesetting:-mtr(Typesetting:-mtd(Typesetting:-mn( (2.16)
 

> evalm(L);
 

Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mtable(Typesetting:-mtr(Typesetting:-mtd(Typesetting:-mn( (2.17)
 

> L.U;
 

Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mtable(Typesetting:-mtr(Typesetting:-mtd(Typesetting:-mn( (2.18)
 

> P.A;
 

Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mtable(Typesetting:-mtr(Typesetting:-mtd(Typesetting:-mn( (2.19)
 

We want to solve Ax = b.  To  solve by using LU decomposition we need to multiply both sides by  P PAx=Pb .   So we now have LU x = Pb.   As in the case of LU decompositions with no row swaps we will not do a forward and backward substitution to solve the new equation PAx = P b.   The solution of both equations are the same.  As in the previous lecture we will first set Ux = y. and solve Ly = Pb.   

> b:=<<0,0,1,2,0>>;
 

`:=`(b, Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mtable(Typesetting:-mtr(Typesetting:-mtd(Typesetting:-mn( (2.20)
 

> with(LinearAlgebra):
 

> Pb:=P.b;
 

`:=`(Pb, Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mtable(Typesetting:-mtr(Typesetting:-mtd(Typesetting:-mn( (2.21)
 

> LPb:=<L|Pb>;
 

`:=`(LPb, Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mtable(Typesetting:-mtr(Typesetting:-mtd(Typesetting:-mn( (2.22)
 

> y:=ForwardSubstitute(LPb);
 

`:=`(y, Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mtable(Typesetting:-mtr(Typesetting:-mtd(Typesetting:-mn( (2.23)
 

> x:=BackwardSubstitute(<U|y>);
 

`:=`(x, Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mtable(Typesetting:-mtr(Typesetting:-mtd(Typesetting:-mfrac(Typesetting:-mn( (2.24)
 

> A.x;
 

Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mtable(Typesetting:-mtr(Typesetting:-mtd(Typesetting:-mn( (2.25)
 

There is a short cut to doing the LU decomposition in Maple.  You can use it for a check in this set of exercise but you must work the problems out step by step to receive credit.   

> P,L,U:=LUDecomposition(A);
 

`:=`(P, L, U, Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mtable(Typesetting:-mtr(Typesetting:-mtd(Typesetting:-mn( (2.26)
 

> P;
 

Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mtable(Typesetting:-mtr(Typesetting:-mtd(Typesetting:-mn( (2.27)
 

> L;
 

Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mtable(Typesetting:-mtr(Typesetting:-mtd(Typesetting:-mn( (2.28)
 

> U;
 

Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mtable(Typesetting:-mtr(Typesetting:-mtd(Typesetting:-mn( (2.29)
 

The LU decomposition with row switches is again nothing more than Gauss elimination with the proper way of writing the L matrix and remembering that we are replacing the equation Ax = b by 

PAx = Pb so that we can solve LUx = b.  Again, the advantage of using the LU decomposition is in solving systems where A never changes but b can have many values.  We will save a lot of time and storage on a computer using the LU decomposition.  Once the LU decomposition has been found then we will need only change the b vector and do a forward and backward substitution.   I have no idea of why I needed 

Exercises  

Exercise 1.8.10 (Do this one first)  Exercise 1.8.8  Do this one as the matrix model in listed on page 171.  The last problem will have a square L and a 4x5 U  Even though U is not square it is consider upper triangular if the elements below the diagonal are zero.  This last problem will have at least one arbitrary parameter.   Problems are due on September 29, 2008 Also, find the LU decomposition of the following matrix.  

Typesetting:-mrow(Typesetting:-mi(For all the problems listed you should carry out the gausselimation step by using the addrow command and swaprow commands until you turn the coeffiecint matrices into an upper triangular matrix.  Then you should create the L matrix by using the multipliers.  Also you will need to give the P matrix when you switch rows.   

You may use the LUdecomp commands on page 42 or it may be 43 in you Maple Lab Manual to check your answers.  This is only a check not the way I want you to work the problem.  You will be allowed to use the LU decomp command on the take home part of test 1.   

>