Advertisement
Guest User

Least Square Regression for IDF curve equation

a guest
Jul 15th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. !Least Square Regression for IDF curve function I=a/(t+b)^c where a,b and c are constants
  2.     dimension Q(100,100),R(100,100),RTranspose(100,100),RTransposeR(100,100),RTransposeRInverse(100,100),RTransposeQ(100,100),U(100,100)
  3.     dimension iIntensity(100),Times(100)
  4.     integer qrow,qcol,rrow,rcol
  5.     real intensity,t,b,tmp,a,c,sum
  6.    
  7.     !here, Q vector corresponds to rainfall intensities
  8.     ![Q]_nx1 = [R]_nx2 [X]_nx1
  9.     ![X]=[lna -c]'_2x1
  10.    
  11.     qrow=7 !7 rows corresponding to 7 I's
  12.     qcol=1 !1 column of I matrix coz it's a vector
  13.     rrow=7 !7 rows of R matrix
  14.     rcol=2 !2 cols of R matrix
  15.    
  16.     open(6,file="Result.txt")
  17.    
  18.     do b=8.0,14.0,0.5
  19.         open(5,file='I.txt')
  20.         do i=1,qrow
  21.             read (5,*) intensity
  22.             Q(i,1)=log(intensity)
  23.         end do
  24.         open(5,file='t.txt')
  25.         do i=1,rrow
  26.             read (5,*) t
  27.             R(i,1)=1
  28.             R(i,2) =log(t+b)
  29.         end do
  30.    
  31.         call Transpose(R,RTranspose,rrow,rcol)
  32.         call MatMul(RTranspose,R,RTransposeR,rcol,rrow,rrow,rcol)
  33.         call Inverse(RTransposeR,RTransposeRInverse,rcol)
  34.         call MatMul(RTranspose,Q,RTransposeQ,rcol,rrow,qrow,qcol)
  35.         call MatMul(RTransposeRInverse,RTransposeQ,U,rcol,rcol,rcol,1)
  36.  
  37.         a=exp(U(1,1))
  38.         c=-U(2,1)
  39.    
  40.    
  41.         !read the given observed intensities into the iIntensity array
  42.         open(5,file='I.txt')
  43.         do i=1,qrow
  44.             read (5,*) iIntensity(i)
  45.         end do
  46.         !read the given observed times into the Times array
  47.         open(5,file='t.txt')
  48.         do i=1,qrow
  49.             read (5,*) Times(i)
  50.         end do
  51.    
  52.         !find squared error
  53.         sum=0
  54.         do i=1,qrow
  55.             tmp=iIntensity(i)-a/(Times(i)+b)**c
  56.             tmp=tmp**2
  57.             sum=sum+tmp
  58.         end do
  59.    
  60.         write (6,*) "b = ",b
  61.         write (6,*) "a = ",a
  62.         write (6,*) "c = ",c
  63.         write (6,*) "Del I squared = ",sum
  64.         write (6,*)
  65.     end do
  66.    
  67.     close(6)
  68.    
  69.     end program
  70.    
  71.    
  72. subroutine Transpose(A,R,rows,cols) !rows,cols = of A
  73. integer rows,cols
  74. dimension A(100,100),R(100,100) !CAUTION: This declaration is crucial
  75.     do i=1,cols !i goes from 1 to cols
  76.         do j=1,rows !and j from 1 to rows because transpose should have the dimension: cols x rows
  77.             R(i,j)=A(j,i) !fills from column of A to row of R
  78.         end do
  79.     end do
  80.     return
  81. end subroutine Transpose
  82.    
  83.    
  84. subroutine MatMul(A,B,C,rrow,rcol,brow,bcol)
  85. integer rrow,rcol,brow,bcol
  86. !To see how this works, try writing the elements of the product matrix in sigma notation
  87. dimension A(100,100),B(100,100),C(100,100) !CAUTION: This declaration is crucial
  88. do k=1,rrow !k runs from 1 to rrow
  89.     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
  90.         sum=0
  91.         do i=1,rcol !this loop is the essence of the aforementioned sigma notation for the (k,l) element of the product matrix
  92.             sum=sum+A(k,i)*B(i,l)
  93.         end do
  94.         C(k,l)=sum
  95.     end do
  96. end do
  97. return
  98. end subroutine MatMul
  99.    
  100. subroutine Inverse(X,XI,size)
  101. dimension X(100,100),XI(100,100)
  102. integer rowindex,colindex,size,tmpi,tmpj,i,j
  103. real pivotelement
  104.  
  105. do i=1,size
  106.     do j=1,size
  107.         if(i.EQ.j) then
  108.             XI(i,j)=1
  109.         else
  110.             XI(i,j)=0 !this is for good measure coz by default, XI may have been changed by the caller itself though it is 0 by default
  111.         endif
  112.     end do
  113. end do
  114.  
  115.  
  116. do rowindex=1,size !loop over rows of X
  117.    
  118.     pivotelement=X(rowindex,rowindex) !this is crucial!
  119.    
  120.     !divide the row by pivot element
  121.     do colindex=1,size
  122.         X(rowindex,colindex)=X(rowindex,colindex)/pivotelement
  123.     end do
  124.     do colindex=1,size
  125.         XI(rowindex,colindex)=XI(rowindex,colindex)/pivotelement
  126.     end do
  127.    
  128.    
  129.     !perform row operations to make non-pivot elements of this column zero
  130.     do i=1,size !i gives the row number being manipulated
  131.         pivotelement=X(i,rowindex) !pivot element for the currently manipulated row. this i crucial
  132.        
  133.         if(i.NE.rowindex) then !row operations are for non-current rows only
  134.            
  135.             do j=1,size !column index for ith row of X matrix
  136.                 X(i,j)=X(i,j)-pivotelement*X(rowindex,j)
  137.             end do
  138.             do j=1,size !column index for ith row of XI matrix
  139.                 XI(i,j)=XI(i,j)-pivotelement*XI(rowindex,j)
  140.             end do
  141.            
  142.            
  143.         endif
  144.     end do
  145.    
  146.    
  147.    
  148. end do
  149.  
  150.  
  151. end subroutine
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement