You are on page 1of 3

c c

arrays set up to cope with up to 10 equations dimension a(10,10),b(10),x(10),swap(10) Reads data from file. Input number to select appropriate file. open(99,file='results.dat',form='formatted') 13 write(*,*)'Give number of data file (1 or 2 or 3) ' read(*,*)i if(i.eq.1)then open(8,file='GAUSS1.DAT',form='formatted') read(8,*)n do 1 j=1,n read(8,*)(a(j,k),k=1,n) continue do 2 j=1,n read(8,*)b(j) continue close (8) else if(i.eq.2)then open(8,file='GAUSS2.DAT',form='formatted') read(8,*)n do 11 j=1,n read(8,*)(a(j,k),k=1,n) continue do 12 j=1,n read(8,*)b(j) continue close (8) else if(i.eq.3)then open(8,file='GAUSS3.DAT',form='formatted') read(8,*)n do 66 j=1,n read(8,*)(a(j,k),k=1,n) continue do 67 j=1,n read(8,*)b(j) continue close (8) else write(*,*)'You must reply either 1 or 2 or 3' go to 13 end if do iteration to change matrix to triangular form write(99,*)'Initial matrix and b' first print out the original matrix call matout(a,b,n)

1 2

11 12

66 67

c c

c c c c c c c c c c c c c c 6 c 4 3 c

do 3 k=1,n-1 modify rows k+1 to n using a k,k

as a pivot

To modify this programme to do partial pivoting you should sweep through the entries in this column from row k to n to find entry with the largest magnetude. The copy the entries from this row into the array swap. Copy the entries from row k to this row. Copy the enries from swap into row k. Do a similar row exchange for the vector b After you have done all that continue from here do 4 i=k+1,n calculte multiplication factor for row i fact=a(i,k)/a(k,k) set entry in column k to be 0 a(i,k)=0.0 the add required multiples of row i do 6 j=k+1,n a(i,j)=a(i,j)-fact*a(k,j) continue do the same manipulation for b b(i)=b(i)-fact*b(k) continue write(99,*)'After step ',k,' matrix and b are' call matout(a,b,n) continue calculate solution do 7 i=n,1,-1 x(i)=b(i)/a(i,i) do 8 j=i+1,n x(i)=x(i)-a(i,j)*x(j)/a(i,i) continue continue write(99,*)'Solutions is x(1), ... , x(n) = ',(x(i),i=1,n) stop end subroutine matout(a,b,n) dimension a(10,10),b(10) write(99,*) do 1 i=1,n write(99,1000)(a(i,j),j=1,n),b(i) continue write(99,*) This format statement assumes that you have a 3x3 matrix as in the two examples. format(1x,f8.4,2x,f8.4,2x,f8.4,4x,f8.4)

8 7

1 c c 1000

return end