Note: If you wish to replicate the R code below, then you will need to copy and paste the following commands in R first (to make sure you have all the packages and their dependencies):

install.packages("import")
# install the import package

import::from(pracma, lu, mldivide)
# import lu and mldivide from the pracma package


This document was created with rmarkdown 1.2 using the following:



“Use R to compute the LU factorization and find the solution for the linear system shown below” (Chapra 242-243):


\[\left[ \begin{array}{ccc} 3 & -0.1 & -0.2 \\ 0.1 & 7 & -0.3 \\ 0.3 & -0.2 & 10 \end{array} \right] \begin{Bmatrix} x1 \\ x2 \\ x3 \end{Bmatrix} = \begin{Bmatrix} 7.85 \\ -19.3 \\ 71.4 \end{Bmatrix}\]



import::from(pracma, lu, mldivide)
# import lu and mldivide from the pracma package


source("https://raw.githubusercontent.com/ggrothendieck/gsubfn/master/R/list.R")

A <- matrix(c(3, -0.1, -0.2, 0.1, 7, -0.3, 0.3, -0.2, 10), nrow = 3, ncol = 3, 
    byrow = TRUE)

A
##      [,1] [,2] [,3]
## [1,]  3.0 -0.1 -0.2
## [2,]  0.1  7.0 -0.3
## [3,]  0.3 -0.2 10.0
b <- matrix(c(7.85, -19.3, 71.4), nrow = 3, ncol = 1, byrow = TRUE)

b
##        [,1]
## [1,]   7.85
## [2,] -19.30
## [3,]  71.40
list[L, U] <- lu(A)

L
##            [,1]        [,2] [,3]
## [1,] 1.00000000  0.00000000    0
## [2,] 0.03333333  1.00000000    0
## [3,] 0.10000000 -0.02712994    1
U
##      [,1]      [,2]       [,3]
## [1,]    3 -0.100000 -0.2000000
## [2,]    0  7.003333 -0.2933333
## [3,]    0  0.000000 10.0120419
L %*% U
##      [,1] [,2] [,3]
## [1,]  3.0 -0.1 -0.2
## [2,]  0.1  7.0 -0.3
## [3,]  0.3 -0.2 10.0
d <- solve(L, b)
d  # solve using base R solve
##           [,1]
## [1,]   7.85000
## [2,] -19.56167
## [3,]  70.08429
d <- mldivide(L, b)
d  # solve using pracma mldivide (left division or \)
##           [,1]
## [1,]   7.85000
## [2,] -19.56167
## [3,]  70.08429
x <- solve(U, d)
x  # solve using base R solve
##      [,1]
## [1,]  3.0
## [2,] -2.5
## [3,]  7.0
x <- mldivide(U, d)
x  # solve using pracma mldivide
##      [,1]
## [1,]  3.0
## [2,] -2.5
## [3,]  7.0



Works Cited

João Eduardo Kögler Junior. Machine Intelligence ?, March 21, 2008. LaTeX - Multiline equations, systems and matrices

Steven C. Chapra, Applied Numerical Methods with MATLAB for Engineers and Scientists, 2nd Edition, Boston, Massachusetts: McGraw-Hill, 2008, pages 242-243.

Wikimedia Foundation, Inc. Wikipedia, 21 July 2016, LU decomposition



77u/LS0tCnRpdGxlOiAiTFUgRmFjdG9yaXphdGlvbiB3aXRoIFIgKGJhc2VkIG9uIEdOVSBPY3RhdmUvTUFUTEFCIEV4YW1wbGUpIgphdXRob3I6ICJJcnVja2EgRW1icnksIEUuSS5ULiIKZGF0ZTogImByIFN5cy5EYXRlKClgIgpvdXRwdXQ6Cmh0bWxfZG9jdW1lbnQ6Cm1hdGhqYXg6IGRlZmF1bHQKLS0tCgo8YnIgLz4KPGJyIC8+CgpOb3RlOiBJZiB5b3Ugd2lzaCB0byByZXBsaWNhdGUgdGhlIFIgY29kZSBiZWxvdywgdGhlbiB5b3Ugd2lsbCBuZWVkIHRvIGNvcHkgYW5kIHBhc3RlIHRoZSBmb2xsb3dpbmcgY29tbWFuZHMgaW4gUiBmaXJzdCAodG8gbWFrZSBzdXJlIHlvdSBoYXZlIGFsbCB0aGUgcGFja2FnZXMgYW5kIHRoZWlyIGRlcGVuZGVuY2llcyk6CgpgYGB7ciBldmFsID0gRkFMU0V9Cmluc3RhbGwucGFja2FnZXMoImltcG9ydCIpCiMgaW5zdGFsbCB0aGUgaW1wb3J0IHBhY2thZ2UKCmltcG9ydDo6ZnJvbShwcmFjbWEsIGx1LCBtbGRpdmlkZSkKIyBpbXBvcnQgbHUgYW5kIG1sZGl2aWRlIGZyb20gdGhlIHByYWNtYSBwYWNrYWdlCmBgYAoKPGJyIC8+CgpUaGlzIGRvY3VtZW50IHdhcyBjcmVhd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