-
Notifications
You must be signed in to change notification settings - Fork 0
/
LUdcmp.f90
180 lines (147 loc) · 2.6 KB
/
LUdcmp.f90
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
program LUdcmp
implicit none
integer :: i,j,N,p,k
real*8, allocatable :: A(:,:),B(:),X(:)
real*8, allocatable :: L(:,:),U(:,:),Y(:),Ls(:),As(:)
real*8 :: Bs, dummy
character*50 :: nama_file_data
nama_file_data="data_matrix.dat"
! Buka file data
open(unit=10, file=nama_file_data, status="old")
! Baca data matrix
read(10,*) N
allocate(A(N,N))
allocate(B(N))
allocate(X(N))
allocate(L(N,N))
allocate(U(N,N))
allocate(Y(N))
allocate(Ls(N))
allocate(As(N))
read(10,*)
do i=1,N
read(10,*) (A(i,j), j=1,N)
end do
read(10,*)
do i=1,N
read(10,*) B(i)
end do
close(10)
! Tampilkan data
write(*,*) N
write(*,*)
do i=1,N
write(*,*) (A(i,j), j=1,N)
end do
write(*,*)
do i=1,N
write(*,*) B(i)
end do
! Dekomposisi A menjadi LU
L(:,:)=0.d0
U(:,:)=0.d0
do i=1,N
U(i,i)=1.d0
end do
do p=1,N
! Kerjakan kolom-kolom L
j=p
do i=j,N
if (j==1) then
L(i,1)=A(i,1)
else
dummy=0.0
do k=1,j-1
dummy=dummy+L(i,k)*U(k,j)
end do
L(i,j)=A(i,j)-dummy
end if
end do
! Jika ada elemen diagonal L yang berharga nol, maka tukar baris L tsb
! dengan baris dibawahnya.
if (L(p,p)==0.0) then
Ls(:)=L(p,:)
L(p,:)=L(p+1,:)
L(p+1,:)=Ls(:)
As(:)=A(p,:)
A(p,:)=A(p+1,:)
A(p+1,:)=As(:)
Bs=B(p)
B(p)=B(p+1)
B(p+1)=Bs
end if
! Kerjakan baris-baris matriks U
i=p
do j=i,N
if (i==1) then
U(1,j)=A(1,j)/L(1,1)
else
dummy=0.0
do k=1,i-1
dummy=dummy+L(i,k)*U(k,j)
end do
U(i,j)=(A(i,j)-dummy)/L(i,i)
end if
end do
end do ! p
! Tampilkan matrix L, U, dan B'
write(*,*)
write(*,*) "Matrix L ="
do i=1,N
write(*,*) (L(i,j), j=1,N)
end do
write(*,*)
write(*,*) "Matrix U ="
do i=1,N
write(*,*) (U(i,j), j=1,N)
end do
write(*,*)
write(*,*) "Matrix B (sesudah penukaran baris) ="
do i=1,N
write(*,*) B(i)
end do
write(*,*)
write(*,*) "Matrix A (sesudah penukaran baris) ="
do i=1,N
write(*,*) (A(i,j), j=1,N)
end do
write(*,*)
! Mencari vektor Y dengan substitusi maju
Y(1)=B(1)/L(1,1)
do i=2,N
dummy=0.d0
do j=1,i-1
dummy=dummy+L(i,j)*Y(j)
end do
Y(i)=(B(i)-dummy)/L(i,i)
end do
! Tampilkan vektor Y
write(*,*) "Vektor Y:"
do i=1,N
write(*,*) Y(i)
end do
write(*,*)
! Mencari vektor X dengan substitusi mundur
X(N)=Y(N)
do i=1,N-1
dummy=0.d0
do j=N-i+1,N
dummy=dummy+U(N-i,j)*X(j)
end do
X(N-i)=Y(N-i)-dummy
end do
! Tampilkan vektor X
write(*,*) "Vektor X:"
do i=1,N
write(*,*) X(i)
end do
write(*,*)
deallocate(A)
deallocate(B)
deallocate(X)
deallocate(L)
deallocate(U)
deallocate(Y)
deallocate(Ls)
deallocate (As)
end program