Advertisement
Guest User

Least Square Method for UH from DRH and ERH

a guest
Jul 7th, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. dimension Q(100,100),R(100,100),RTranspose(100,100),RTransposeR(100,100),RTransposeRInverse(100,100),RTransposeQ(100,100),U(100,100)
  2.     integer qrow,qcol,rrow,rcol
  3.     qrow=16 !16 non-zero DRH ordinates
  4.     qcol=1
  5.     rrow=16 !16 rows of Rainfall matrix
  6.     rcol=14 !14 rows of Rainfall matrix (qrow-(no. of ERH ordinates-1)) = 16-(3-1) = 16-2 = 14
  7.     open(5,file='Q.txt')
  8.     do i=1,qrow
  9.         read (5,*)(Q(i,j),j=1,qcol)
  10.     end do
  11.     open(5,file='R.txt')
  12.     do i=1,rrow
  13.         read (5,*)(R(i,j),j=1,rcol)
  14.     end do
  15.    
  16.     call Transpose(R,RTranspose,rrow,rcol)
  17.     call MatMul(RTranspose,R,RTransposeR,rcol,rrow,rrow,rcol)
  18.     call Inverse(RTransposeR,RTransposeRInverse,rcol)
  19.     call MatMul(RTranspose,Q,RTransposeQ,rcol,rrow,qrow,qcol)
  20.     call MatMul(RTransposeRInverse,RTransposeQ,U,rcol,rcol,rcol,1)
  21.  
  22.     open(5,file='U.txt')
  23.     do i=1,rcol
  24.         write (5,*) U(i,1)
  25.     end do
  26.    
  27.     end program
  28.    
  29.    
  30. subroutine Transpose(A,R,rows,cols) !rows,cols = of A
  31. integer rows,cols
  32. dimension A(100,100),R(100,100) !CAUTION: This declaration is crucial
  33.     do i=1,cols !i goes from 1 to cols
  34.         do j=1,rows !and j from 1 to rows because transpose should have the dimension: cols x rows
  35.             R(i,j)=A(j,i) !fills from column of A to row of R
  36.         end do
  37.     end do
  38.     return
  39. end subroutine Transpose
  40.    
  41.    
  42. subroutine MatMul(A,B,C,arow,acol,brow,bcol)
  43. integer arow,acol,brow,bcol
  44. !To see how this works, try writing the elements of the product matrix in sigma notation
  45. dimension A(100,100),B(100,100),C(100,100) !CAUTION: This declaration is crucial
  46. do k=1,arow !k runs from 1 to arow
  47.     do l=1,bcol !and l from 1 to bcol because that's the output matrix dimension and we _have_ to populate each element of it
  48.         sum=0
  49.         do i=1,acol !this loop is the essence of the aforementioned sigma notation for the (k,l) element of the product matrix
  50.             sum=sum+A(k,i)*B(i,l)
  51.         end do
  52.         C(k,l)=sum
  53.     end do
  54. end do
  55. return
  56. end subroutine MatMul
  57.    
  58. subroutine Inverse(X,XI,size)
  59. dimension X(100,100),XI(100,100)
  60. integer rowindex,colindex,size,tmpi,tmpj,i,j
  61. real pivotelement
  62.  
  63. do i=1,size
  64.     do j=1,size
  65.         if(i.EQ.j) then
  66.             XI(i,j)=1
  67.         endif
  68.     end do
  69. end do
  70.  
  71.  
  72. do rowindex=1,size !loop over rows of X
  73.    
  74.     pivotelement=X(rowindex,rowindex) !this is crucial!
  75.    
  76.     !divide the row by pivot element
  77.     do colindex=1,size
  78.         X(rowindex,colindex)=X(rowindex,colindex)/pivotelement
  79.     end do
  80.     do colindex=1,size
  81.         XI(rowindex,colindex)=XI(rowindex,colindex)/pivotelement
  82.     end do
  83.    
  84.    
  85.     !perform row operations to make non-pivot elements of this column zero
  86.     do i=1,size !i gives the row number being manipulated
  87.         pivotelement=X(i,rowindex) !pivot element for the currently manipulated row. this i crucial
  88.        
  89.         if(i.NE.rowindex) then !row operations are for non-current rows only
  90.            
  91.             do j=1,size !column index for ith row of X matrix
  92.                 X(i,j)=X(i,j)-pivotelement*X(rowindex,j)
  93.             end do
  94.             do j=1,size !column index for ith row of XI matrix
  95.                 XI(i,j)=XI(i,j)-pivotelement*XI(rowindex,j)
  96.             end do
  97.            
  98.            
  99.         endif
  100.     end do
  101.    
  102.    
  103.    
  104. end do
  105.  
  106.  
  107. end subroutine
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement