! PROGRAM LU-Decomposition
!The number of variable or number of equations is n.
!The coefficient matrix=a(ij), i, j=1,2,3,...,n.
!The lower triangular matrix=l(ij), i, j=1,2,3,...,n.
!The upper matrix=u(ij), i, j=1,2,3,...,n.
!b(i), i=1,2,3,...,n, the constants of the of the equations.
!z(i), i=1,2,3,...,n.
!x(i), i=1,2,3,...,n.
integer::n,i,j,k,m
real:: a(5,5),l(5,5),u(5,5),b(5),x(5),z(5),ratio,s
open(3,file="input.dat")
open(4,file="output2")
read(3,*)n
do i=1,n
read(3,*)(a(i,j),j=1,n)
enddo
write(4,*) "A="
do i=1,n
write(4,*)(a(i,j),j=1,n)
enddo
write(4,*)" "
read(3,*)(b(j),j=1,n)
write(4,*)"B=",(b(j),j=1,n)
write(4,*)" "
do i=1,n
l(i,1)=a(i,1)
u(i,i)=1.0
enddo
do j=2,n
u(1,j)=a(1,j)/l(1,1)
enddo
do k=2,n
!To determine columns of l
do i=k,n
s=0
do j=1,i-1
s=s+l(i,j)*u(j,k)
enddo
l(i,k)=a(i,k)-s
enddo
!!To determine rows of u
do i=k+1,n
if(i/=k)then
s=0
do j=1,i-1
s=s+l(k,j)*u(j,i)
enddo
endif
u(k,i)=(a(k,i)-s)/l(k,k)
enddo
enddo
write(4,*)" "
write(4,*) "L="
do i=1,n
write(4,*)(l(i,j),j=1,n)
enddo
write(4,*)" "
write(4,*) "U="
do i=1,n
write(4,*)(u(i,j),j=1,n)
enddo
!To obtain z(i),i=1,2,3,...,n.
z(1)=b(1)/l(1,1)
do i=2,n
s=0
do j=1,i-1
s=s+z(j)*l(i,j)
enddo
z(i)=(b(i)-s)/l(i,i)
enddo
write(4,*)" "
write(4,*)"Z=",(z(j),j=1,n)
!To obtain x(i),i=1,2,3,...,n.
x(n)=z(n)
do k=n-1,1,-1
s=0
do i=n,k+1,-1
s=s+u(k,i)*x(i)
enddo
x(k)=z(k)-s
enddo
write(4,*)" "
write(4,*)"X=",(x(j),j=1,n)
end PROGRAM
!The number of variable or number of equations is n.
!The coefficient matrix=a(ij), i, j=1,2,3,...,n.
!The lower triangular matrix=l(ij), i, j=1,2,3,...,n.
!The upper matrix=u(ij), i, j=1,2,3,...,n.
!b(i), i=1,2,3,...,n, the constants of the of the equations.
!z(i), i=1,2,3,...,n.
!x(i), i=1,2,3,...,n.
integer::n,i,j,k,m
real:: a(5,5),l(5,5),u(5,5),b(5),x(5),z(5),ratio,s
open(3,file="input.dat")
open(4,file="output2")
read(3,*)n
do i=1,n
read(3,*)(a(i,j),j=1,n)
enddo
write(4,*) "A="
do i=1,n
write(4,*)(a(i,j),j=1,n)
enddo
write(4,*)" "
read(3,*)(b(j),j=1,n)
write(4,*)"B=",(b(j),j=1,n)
write(4,*)" "
do i=1,n
l(i,1)=a(i,1)
u(i,i)=1.0
enddo
do j=2,n
u(1,j)=a(1,j)/l(1,1)
enddo
do k=2,n
!To determine columns of l
do i=k,n
s=0
do j=1,i-1
s=s+l(i,j)*u(j,k)
enddo
l(i,k)=a(i,k)-s
enddo
!!To determine rows of u
do i=k+1,n
if(i/=k)then
s=0
do j=1,i-1
s=s+l(k,j)*u(j,i)
enddo
endif
u(k,i)=(a(k,i)-s)/l(k,k)
enddo
enddo
write(4,*)" "
write(4,*) "L="
do i=1,n
write(4,*)(l(i,j),j=1,n)
enddo
write(4,*)" "
write(4,*) "U="
do i=1,n
write(4,*)(u(i,j),j=1,n)
enddo
!To obtain z(i),i=1,2,3,...,n.
z(1)=b(1)/l(1,1)
do i=2,n
s=0
do j=1,i-1
s=s+z(j)*l(i,j)
enddo
z(i)=(b(i)-s)/l(i,i)
enddo
write(4,*)" "
write(4,*)"Z=",(z(j),j=1,n)
!To obtain x(i),i=1,2,3,...,n.
x(n)=z(n)
do k=n-1,1,-1
s=0
do i=n,k+1,-1
s=s+u(k,i)*x(i)
enddo
x(k)=z(k)-s
enddo
write(4,*)" "
write(4,*)"X=",(x(j),j=1,n)
end PROGRAM
Comments
Post a Comment