Our task is to create an LU decomposition of a 4 by 4 matrix. At the end, we check to ensure that the resulting decomposition multiplies to return our original matrix.
Factorized<-function(a){
otherMatrix<-matrix(c(1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1),nrow=4)
print(a)
for (i in 2:4){
factorOne<- (-a[i,1])
factorTwo<-a[1,1]
for(j in 1:4) {a[i,j]<- (((factorOne/factorTwo)*a[1,j])+a[i,j])}
otherMatrix[i,1]<-(-factorOne/factorTwo)
print(a)
}
for (i in 3:4){
factorOne<- (-a[i,2])
factorTwo<-a[2,2]
for(j in 1:4) {a[i,j]<- (((factorOne/factorTwo)*a[2,j])+a[i,j])}
otherMatrix[i,2]<-(-factorOne/factorTwo)
print(a)
}
factorOne<- (-a[i,3])
factorTwo<-a[3,3]
for(j in 1:4) {a[i,j]<- (((factorOne/factorTwo)*a[3,j])+a[i,j])}
otherMatrix[i,3]<-(-factorOne/factorTwo)
print("Our upper triangular is:")
print(a)
print("Our lower triangular is:")
print(otherMatrix)
print("When we multiply our L * U, we return to our original matrix.")
print(otherMatrix%*%a)
}
MatrixA<-matrix(c(.5,7,3,4,1,2,17,4,5,10,11,12,13,14,25,36),nrow=4)
Factorized(MatrixA)
## [,1] [,2] [,3] [,4]
## [1,] 0.5 1 5 13
## [2,] 7.0 2 10 14
## [3,] 3.0 17 11 25
## [4,] 4.0 4 12 36
## [,1] [,2] [,3] [,4]
## [1,] 0.5 1 5 13
## [2,] 0.0 -12 -60 -168
## [3,] 3.0 17 11 25
## [4,] 4.0 4 12 36
## [,1] [,2] [,3] [,4]
## [1,] 0.5 1 5 13
## [2,] 0.0 -12 -60 -168
## [3,] 0.0 11 -19 -53
## [4,] 4.0 4 12 36
## [,1] [,2] [,3] [,4]
## [1,] 0.5 1 5 13
## [2,] 0.0 -12 -60 -168
## [3,] 0.0 11 -19 -53
## [4,] 0.0 -4 -28 -68
## [,1] [,2] [,3] [,4]
## [1,] 0.5 1 5 13
## [2,] 0.0 -12 -60 -168
## [3,] 0.0 0 -74 -207
## [4,] 0.0 -4 -28 -68
## [,1] [,2] [,3] [,4]
## [1,] 0.5 1 5 13
## [2,] 0.0 -12 -60 -168
## [3,] 0.0 0 -74 -207
## [4,] 0.0 0 -8 -12
## [1] "Our upper triangular is:"
## [,1] [,2] [,3] [,4]
## [1,] 0.5 1 5 13.00000
## [2,] 0.0 -12 -60 -168.00000
## [3,] 0.0 0 -74 -207.00000
## [4,] 0.0 0 0 10.37838
## [1] "Our lower triangular is:"
## [,1] [,2] [,3] [,4]
## [1,] 1 0.0000000 0.0000000 0
## [2,] 14 1.0000000 0.0000000 0
## [3,] 6 -0.9166667 1.0000000 0
## [4,] 8 0.3333333 0.1081081 1
## [1] "When we multiply our L * U, we return to our original matrix."
## [,1] [,2] [,3] [,4]
## [1,] 0.5 1 5 13
## [2,] 7.0 2 10 14
## [3,] 3.0 17 11 25
## [4,] 4.0 4 12 36