Advertisement
Guest User

Recursive Least Square Technique for IDF curve equation

a guest
Jul 27th, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. !recursive Least Square technique for IDF curve equation I=a/(t+b)^c
  2.  
  3. integer k,j
  4.     real t,intensity
  5.     real X(100,100) !the parameter vector
  6.     integer n !number of data
  7.     real A(100,100) !design/coefficient matrix
  8.     real I(100,100) !output vector
  9.     real b
  10.     real ATranspose(100,100),ATransposeA(100,100),ATransposeAInverse(100,100),ATransposeI(100,100)
  11.     real ARow(100,100),ARowTranspose(100,100),ARowTransposeARow(100,100),P_n_plus1(100,100),P_n_plus1_ARowTranspose(100,100),totalCorrection(100,100)
  12.     real ARowX(100,100)
  13.     real tmp(100,100)
  14.     b=12
  15.     n=0
  16.    
  17.     do while(.TRUE.)
  18.         write(*,*) "t"
  19.         read(*,*) t
  20.         write(*,*) "I"
  21.         read(*,*) intensity  
  22.         n=n+1
  23.  
  24.         I(n,1)=log(intensity)
  25.         A(n,1)=1
  26.         A(n,2)=log(t+b)
  27.        
  28.        
  29.        
  30.         if(n.EQ.2) then
  31.             !solve the system only once, when the number of data provided=number of unknowns(here, 2)
  32.             call matrixTranspose(A,ATranspose,n,2)
  33.             call matrixMul(ATranspose,A,ATransposeA,2,n,n,2)
  34.             call Inverse(ATransposeA,ATransposeAInverse,2)
  35.             call matrixMul(ATranspose,I,ATransposeI,2,n,n,1)
  36.             call matrixMul(ATransposeAInverse,ATransposeI,X,2,2,2,1)    
  37.         elseif(n.GT.2) then
  38.             !use recursive Least Square formula
  39.             !add elements of previous ATransposeA with (ARow'ARow) where '=transpose operation, ARow=row matrix
  40.             ![1 ln(t_n+b)], t_n being the current(new) time data value
  41.             ARow(1,1)=1
  42.             ARow(1,2)=log(t+b)
  43.             call matrixTranspose(ARow,ARowTranspose,1,2)
  44.             call matrixMul(ARowTranspose,ARow,ARowTransposeARow,2,1,1,2)
  45.             do k=1,2
  46.                 do j=1,2
  47.                     tmp(k,j)=ATransposeA(k,j) + ARowTransposeARow(k,j)
  48.                 end do
  49.             end do
  50.             call Inverse(tmp,P_n_plus1,2)
  51.             !update ATransposeA
  52.             do k=1,2
  53.                 do j=1,2
  54.                     ATransposeA(k,j)=tmp(k,j)
  55.                 end do
  56.             end do
  57.             call matrixMul(P_n_plus1,ARowTranspose,P_n_plus1_ARowTranspose,2,2,2,1) !P_n_plus1_ARowTranspose is the same dimension as X
  58.             call matrixMul(ARow,X,ARowX,1,2,2,1) !ARowX(1,1) is the output, which is a scalar
  59.             do k=1,2
  60.                 totalCorrection(k,1)=P_n_plus1_ARowTranspose(k,1)*(log(intensity)-ARowX(1,1))
  61.             end do
  62.             !update X vector
  63.             do k=1,2
  64.                 X(k,1)=X(k,1)+totalCorrection(k,1)    
  65.             end do
  66.         endif
  67.        
  68.         write(*,*) "a = ",exp(X(1,1))
  69.         write(*,*) "c = ",-X(2,1)
  70.         write(*,*)
  71.        
  72.        
  73.     end do
  74.    
  75.     end program
  76.    
  77.    
  78. subroutine matrixTranspose(A,R,rows,cols) !rows,cols = of A
  79. integer rows,cols
  80. dimension A(100,100),R(100,100) !CAUTION: This declaration is crucial
  81.     do i=1,cols !i goes from 1 to cols
  82.         do j=1,rows !and j from 1 to rows because transpose should have the dimension: cols x rows
  83.             R(i,j)=A(j,i) !fills from column of A to row of R
  84.         end do
  85.     end do
  86.     return
  87. end subroutine matrixTranspose
  88.    
  89.    
  90. subroutine matrixMul(A,B,C,arow,acol,brow,bcol)
  91. integer arow,acol,brow,bcol
  92. !To see how this works, try writing the elements of the product matrix in sigma notation
  93. dimension A(100,100),B(100,100),C(100,100) !CAUTION: This declaration is crucial
  94. do k=1,arow !k runs from 1 to arow
  95.     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
  96.         sum=0
  97.         do i=1,acol !this loop is the essence of the aforementioned sigma notation for the (k,l) element of the product matrix
  98.             sum=sum+A(k,i)*B(i,l)
  99.         end do
  100.         C(k,l)=sum
  101.     end do
  102. end do
  103. return
  104. end subroutine matrixMul
  105.    
  106. subroutine Inverse(X,XI,size)
  107. dimension X(100,100),XI(100,100),originalX(100,100)
  108. integer rowindex,colindex,size,tmpi,tmpj,i,j
  109. real pivotelement
  110.  
  111. originalX=X
  112.  
  113. do i=1,size
  114.     do j=1,size
  115.         if(i.EQ.j) then
  116.             XI(i,j)=1
  117.         else
  118.             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
  119.         endif
  120.     end do
  121. end do
  122.  
  123.  
  124. do rowindex=1,size !loop over rows of X
  125.    
  126.     pivotelement=X(rowindex,rowindex) !this is crucial!
  127.    
  128.     !divide the row by pivot element
  129.     do colindex=1,size
  130.         X(rowindex,colindex)=X(rowindex,colindex)/pivotelement
  131.     end do
  132.     do colindex=1,size
  133.         XI(rowindex,colindex)=XI(rowindex,colindex)/pivotelement
  134.     end do
  135.    
  136.    
  137.     !perform row operations to make non-pivot elements of this column zero
  138.     do i=1,size !i gives the row number being manipulated
  139.         pivotelement=X(i,rowindex) !pivot element for the currently manipulated row. this i crucial
  140.        
  141.         if(i.NE.rowindex) then !row operations are for non-current rows only
  142.            
  143.             do j=1,size !column index for ith row of X matrix
  144.                 X(i,j)=X(i,j)-pivotelement*X(rowindex,j)
  145.             end do
  146.             do j=1,size !column index for ith row of XI matrix
  147.                 XI(i,j)=XI(i,j)-pivotelement*XI(rowindex,j)
  148.             end do
  149.            
  150.            
  151.         endif
  152.     end do
  153.    
  154.  
  155.    
  156. end do
  157.  
  158. !restore X to original X since upon inversion completion, X has been reduced to an Identity matrix. good practice
  159. X=originalX
  160.  
  161. end subroutine
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement