summaryrefslogtreecommitdiff
path: root/Numerical_Methods_by_E_Balaguruswamy/CH7/EX7.5/Ex7_5.R
blob: 58ded1adb0708400ad8c7dfa8dbda5a3f36126c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# Example 5     Chapter 7       Page no.: 234
# Dolittle Algorithm

#import 'matrixcalc' library
install.packages("matrixcalc")
library(matrixcalc)

#Define system of equations in matrix form 
A<-matrix(c(3,2,1,2,3,2,1,2,3),nrow = 3,ncol = 3,byrow = TRUE)
A
B<-matrix(c(10,14,14),nrow = 3,ncol = 1)
B

#LU Decomposition
luA<-lu.decomposition(A)
L<-luA$L
L
U<-luA$U
U

z1<-B[1]
z2<-B[2]-L[2,1]*z1
z3<-B[3]-L[3,1]*z1-L[3,2]*z2

#By Back Substitution we get,
x3=z3/(U[3,3])
x2=(z2-U[2,3]*x3)/U[2,2]
x1=(z1-U[1,2]*x2-U[1,3]*x3)/U[1,1]
sprintf("Values of x1, x2 and x3 are %f,%f and %f",x1,x2,x3)