FORTRAN 90

advertisement
FORTRAN 90
Lecturer : Rafel Hekmat Hameed
University of Babylon
Subject : Fortran 90
College of Engineering
Year : Second B.Sc.
Mechanical Engineering Dep.
Solution a set of linear equations by
Gauss-Seidel
Gauss
method
In certain cases, such as when a system of equations is large, iterative
methods of solving equations are more advantageous. Iterative methods, such
as the Gauss-Seidel
Seidel method, give the user control of the round
round-off error.
Also, if the physics of the problem are well known, initial guesses needed in
iterative methods can be made more judiciously leading to faster
convergence. Given a general set of n equations and n unknowns, we have
If the diagonal elements are non-zero,
non zero, each equation is rewritten for
the corresponding unknown, that is, the first equation is rewritten with x1 on
the left hand side, and the second equation is rewritten with x2 on the left
hand side and so on as follows,
follows rewriting each equation
Now to find xi ’s, one assumes an initial guess for the xi ’s and then uses the
rewritten equations to calculate the new estimates. Remember, one always
ϭ
uses the most recent estimates to calculate the next estimates, xi . At the end
of
each
iteration, one calculates the
absolute
relative approximate error for each
xi as
Where new i x is the recently obtained value of xi , and old xi is the previous
value of xi . When the absolute relative approximate error for each xi is less
than the pre-specified tolerance, the iterations are stopped.
GAUSS-SEIDEL
SEIDEL CONVERGENCE THEOREM:
If A is diagonally dominant, then the Gauss-Seidel
Gauss
method converges for any
starting vector x. A sufficient, but not necessary condition.
Diagonally dominant: [A] in [A] [X] = [C] is diagonally dominant if:
For all i
For at least one i
Example
Find the solution to the following system of equations using the Gauss-Seidel
Gauss
method.
Use x1=1 , x2=0 , and x3=1 as the initial guess and conduct two iterations.
Solution
The coefficient matrix
Ϯ
is diagonally dominant as
and the inequality is strictly greater than for
for at least one row. Hence, the
solution should converge using the Gauss-Seidel method.
Rewriting the equations, we get
Iteration 1
The absolute relative approximate error at the end of the first iteration is
Repeating more iteration, the following values are obtained
ϯ
The solution of equations obtained x1=0.99919 , x2=3.0001, and x3=4.0001
A Fortran 90 program to solve a set of linear equations by
Gauss-Seidel method
Program gauss_seidle_method
implicit none
integer,parameter::n=3
real,dimension(n,n)::a
real,dimension(n)::c
real,dimension(n)::x
sion(n)::x
integer::i,j ,iter ,k
real::old,sum,ea,es=0.00001,s
real::old,sum,ea,es=0.00001,sdummy
read(*,*)((a(i,j),j=1,n),i=1,n
read(*,*)((a(i,j),j=1,n),i=1,n)
read(*,*)(c(i),i=1,n)
read(*,*)(x(i),i=1,n)
checking
checking diagonal dominant
do i=1,n
;
s=0
do j=1,n
if(i.ne.j)s=s+abs(a(i,j
if(i.ne.j)s=s+abs(a(i,j))
enddo
if(abs(a(i,i)).lt.s)then
write(*,*) "these equtions are not diagonal-dominance
diagonal
stop
;
endif ;
enddo
do i=1,n
ϰ
dummy=a(i,i)
do j=1,n
a(i,j)=a(i,j)/dummy
enddo
;
c(i)=c(i)/dummy
;
k=0
enddo
iter=0
do while (iter<10.and. k==0)
iter=iter+1
k=1
do i=1,n
old=x(i)
sum=c(i)
do j=1,n
if(i.ne.j)then
sum=sum-a(i,j)*x(j)
endif
enddo
x(i)=sum
print*,x(i)
if(x(i).ne.0 .and. k==1) ea=(abs(x(i)-old)/x(i))*100
if(ea.gt.es) k=0
enddo
print*, BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
enddo
end
ϱ
Download