Advertisement
parthosutradhor

SOR Method

Apr 17th, 2019
877
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. program sor
  2.     implicit none
  3.     integer, parameter::n=3, M=100
  4.     integer::i,j,k
  5.     real::a(n,n+1),b(n),x(n),x0(n), tol=0.00001,s1,s2,w,norm
  6.     open(11, file="in.txt")
  7.     ! reading matrix
  8.     do i=1,n
  9.         read(11,*) (a(i,j), j=1,n+1)
  10.     end do
  11.     ! reading initial guess
  12.     read(11,*) (x0(i), i=1,n)
  13.     ! reading w
  14.     read(11,*) w
  15.     ! separating b
  16.     do i=1,n
  17.         b(i)= a(i,n+1)
  18.     end do
  19.  
  20.     do k=1,M
  21.         do i=1,n
  22.             s1=0.0
  23.             do j=1,i-1
  24.                 s1=s1+a(i,j)*x(j)
  25.             end do
  26.             s2=0.0
  27.             do j=i+1,n
  28.                 s2=s2+a(i,j)*x0(j)
  29.             end do
  30.             x(i) = (1-w)*x0(i) + 1/a(i,i)*(w*(-s1-s2+b(i)))
  31.         end do
  32.         !write(*,*) (x(i), i=1,n)
  33.         if(norm(x,x0)<tol) then
  34.             write(*,*) (x(i), i=1,n)
  35.             STOP
  36.         end if
  37.         do i=1,n
  38.             x0(i)=x(i)
  39.         end do
  40.  
  41.     end do
  42.     write(*,*) (x(i), i=1,n)
  43.  
  44. end program sor
  45.  
  46. function norm(x,x0)
  47.     integer,parameter::n=3
  48.     integer::i
  49.     real::x(n),x0(n),s,norm
  50.     s=0.0
  51.     do i=1,n
  52.         s=s+(x(i)-x0(i))**2
  53.     end do
  54.     norm = SQRT(s)
  55. end function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement