JACOBI2
!Program name: JACOBI2
Integer :: i, j, k, n
Real,
allocatable :: A(:,:)
Real,
allocatable :: X0(:), X(:), B(:)
Real
:: tol, norm, sigma
Print
*, "Enter the dimension of the system of non-homogeneous equations"
Read *, n
allocate(A(n,n))
Allocate (X0(n))
Allocate (X(n))
Allocate (B(n))
Do i=1,n
Do
j=1,n
Print *, 'A(',i,',',j,')= '
Read *, A(i,j)
End do
end do
do i=1,n
write (*,*) (A(i,j),j=1,n)
end
do
print
*,
do
i=1,n
Print *, 'B(',i,')='
Read *, B(i)
end
do
write
(*,*) (B(j),j=1,n)
Print
*,
do
i=1,n
Print *, 'X0(',i,')='
Read *, X0(i)
end
do
write
(*,*) (X0(j),j=1,n)
Print
*,
k=0
tol=1.0E-06
10 do i=1,n
sigma=0
do j=1,n
If (j .ne. i) sigma=sigma+A(i,j)*X0(j)
end do
X(i)=(B(i)-sigma)/A(i,i)
end
do
k=k+1
print *, k, (X(i), i=1,n)
norm=abs(X(1)-X0(1))
do i=1,n
If
(abs(X(i)-X0(i)) .gt. norm) then
norm=abs(X(i)-X0(i))
if
(norm .lt. tol) go to 20
end
if
end
do
do
i=1,n
X0(i)=X(i)
end
do
go
to 10
20
stop
end
!THE FINAL RESULT IS: k=13 0.186119888 0.33123031 -0.422271289
Comentários
Postar um comentário