PROGRAM Gauss_Elimination
! n is number of variables or number of equations
! a[ij], i, j=1,2,3,...,n is coefficient matrix, b[i], i=1,2,3,...,n is constant
! vector, x[i], i=1,2,3...,n is the vector of variables
integer::n,i,j,k
real:: a(5,5),b(5),x(5),ratio,s
open(1,file="input.dat")
open(2,file="output.dat")
read(1,*)n
write(2,*)"The number of variables or number of equations, n=",n
do i=1,n
read(1,*)(a(i,j),j=1,n)
enddo
write(2,*)"The cefficient matrix, A="
do i=1,n
write(2,*)(a(i,j),j=1,n)
enddo
do j=1,n
read(1,*)b(j)
enddo
write(2,*)"The constant vector, B="
write(2,*)(b(j),j=1,n)
do k=1,n-1
do i=k+1,n
ratio=a(i,k)/a(k,k)
do j=k,n
a(i,j)=a(i,j)-ratio*a(k,j)
enddo
b(i)=b(i)-ratio*b(k)
enddo
enddo
write(2,*)
do i=1,n
write(2,*)(a(i,j),j=1,n)
enddo
x(n)=b(n)/a(n,n)
do i=n-1,1,-1
s=0.0
do j=n,i+1,-1
s=s+a(i,j)*x(j)
enddo
x(i)=(b(i)-s)/a(i,i)
enddo
write(2,*)
write(2,*)(x(j),j=1,n)
end PROGRAM
! n is number of variables or number of equations
! a[ij], i, j=1,2,3,...,n is coefficient matrix, b[i], i=1,2,3,...,n is constant
! vector, x[i], i=1,2,3...,n is the vector of variables
integer::n,i,j,k
real:: a(5,5),b(5),x(5),ratio,s
open(1,file="input.dat")
open(2,file="output.dat")
read(1,*)n
write(2,*)"The number of variables or number of equations, n=",n
do i=1,n
read(1,*)(a(i,j),j=1,n)
enddo
write(2,*)"The cefficient matrix, A="
do i=1,n
write(2,*)(a(i,j),j=1,n)
enddo
do j=1,n
read(1,*)b(j)
enddo
write(2,*)"The constant vector, B="
write(2,*)(b(j),j=1,n)
do k=1,n-1
do i=k+1,n
ratio=a(i,k)/a(k,k)
do j=k,n
a(i,j)=a(i,j)-ratio*a(k,j)
enddo
b(i)=b(i)-ratio*b(k)
enddo
enddo
write(2,*)
do i=1,n
write(2,*)(a(i,j),j=1,n)
enddo
x(n)=b(n)/a(n,n)
do i=n-1,1,-1
s=0.0
do j=n,i+1,-1
s=s+a(i,j)*x(j)
enddo
x(i)=(b(i)-s)/a(i,i)
enddo
write(2,*)
write(2,*)(x(j),j=1,n)
end PROGRAM
Comments
Post a Comment