Solving a Runtime Error: Investigating Beta(i,j)

  • Context: Fortran 
  • Thread starter Thread starter svishal03
  • Start date Start date
  • Tags Tags
    Error Runtime
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
1 reply · 3K views
svishal03
Messages
124
Reaction score
1
I'm n facing a runtime error and do not know the reason.Also, I found where is the run time error in the code below but am at no clues, why it is occurring.

Actually when j=2 and i=1, the beta(i,j) is not getting calculated- that is, the print statement for beta(i,j) is not executed when j=2 and i=1. Thouhg the program is done till j=2 and also prints i=1 but does not calculate beta(2,1) as it is not printed. See the code in bold below

Anyone knows why?

Please help if possible!

MODULE DIRECT_METHODS_FOR_SOLUTION_OF_LINEAR_ALGEBRAIC_EQUATIONS
CONTAINS


FUNCTION CroutDecomp(AA,total_rows,total_columns,no_of_equations) !This routine decomposes the 'A' matrix of Ax=b into an upper and lower triangular matrix

!It is to be noted that this function fills in only the combined matrix of 'alpha's' and 'beta's' ; where alpha is the upper triangular matrix and beta is the lower triangular matrix.


!Crout's decomposition carries out 2 steps:
!1) First, for i=1,2,---,j use , beta(i,j)= a(i,j)-(summation over k=1 to i-1)[alpha(i,k)*beta(k,j)
!2)Second, for i=j+1,--N use, alpha(i,k)= 1/beta(j,j)*((a(i,j)-summation over k=1 to j-1 [ alpha(i,k)*beta(k,j)]


INTEGER::total_rows,total_columns ,no_of_equations
REAL aa(total_rows,total_columns)
INTEGER:: i,j,k
REAL ::sum1
REAL alpha(total_rows,total_columns),beta(total_rows,total_columns),decomposed(total_rows,total_columns)



LOOP_COLUMN:DO j=1,total_columns

print*,'inside j' ,j
LOOP_ROWS:DO i= 1,j
print*,'inside i', i
alpha(i,i)=1
sum1=0

INotEqual1:IF(i.NE.1)THEN
print*,'inside prod alpha beta',i,j
LOOP_PROD_ALPHA_BETA:DO k=1,i-1
sum1=sum1+(alpha(i,k)*beta(k,j))
END DO LOOP_PROD_ALPHA_BETA
END IF INotEqual1

beta(i,j)=AA(i,j)-sum1
print*,'printing betaqqqqqqqqqqq',beta(i,j)

decomposed(i,j)=beta(i,j)
print*,'printing decomposed',decomposed(i,j)
END DO LOOP_ROWS



jLessThanNoOfEqns:IF(j.LT.no_of_equations)THEN
LOOP_ROWS_1: DO i=j+1,no_of_equations
sum1=0

jNotEqual1:IF(j.NE.1)THEN
LOOP_PROD_ALPHA_BETA2:DO k=1,j-1
sum1=sum1+(alpha(i,k)*beta(k,j))
END DO LOOP_PROD_ALPHA_BETA2
END IF jNotEqual1

alpha(i,j)=(1/beta(j,j))*(AA(i,j)-sum1)
decomposed(i,j)=alpha(i,j)
END DO LOOP_ROWS_1

END IF jLessThanNoOfEqns

END DO LOOP_COLUMN
print *,'in CroutDecomp'



!ROW11_LOOP:DO i=1,total_rows
!COLUMN11_LOOP:DO j=1,total_columns
!PRINT*,decomposed(i,j)
!END DO COLUMN11_LOOP
!END DO ROW11_LOOP


CroutDecomp = 20

END FUNCTION CroutDecomp




END MODULE DIRECT_METHODS_FOR_SOLUTION_OF_LINEAR_ALGEBRAIC_EQUATIONS


PROGRAM LINEAR_EQUATIONS_SOLVER
USE DIRECT_METHODS_FOR_SOLUTION_OF_LINEAR_ALGEBRAIC_EQUATIONS
!IMPLICIT NONE
REAL,DIMENSION(100,100)::AA,x
REAL,DIMENSION(100)::b
INTEGER::total_rows,total_columns,no_of_equations,i,j
PRINT*, 'ENTER THE TOTAL NUMBER OF ROWS AND COLUMNS'
READ*, total_rows,total_columns
PRINT*, 'ENTER THE TOTAL NUMBER OF EQUATIONS'
READ*, no_of_equations
PRINT*, 'ENTER THE A MATRIX OF Ax = b'
ROW_LOOP:DO i=1,total_rows
COLUMN_LOOP:DO j=1,total_columns
READ*,AA(i,j)
END DO COLUMN_LOOP
END DO ROW_LOOP
PRINT*, 'ENTER THE b MATRIX OF Ax = b'
ROW_LOOP:DO i=1,total_rows
READ*,b(i)
END DO ROW_LOOP

x= CroutDecomp(AA,total_rows,total_columns,no_of_equations)

END PROGRAM LINEAR_EQUATIONS_SOLVER
 
Physics news on Phys.org
This is happening because you are only filling in the lower diagonal of your matrix.

Code:
LOOP_COLUMN:DO j=1,total_columns 

print*,'inside j' ,j 
LOOP_ROWS:DO i= 1,j 
print*,'inside i', i 
alpha(i,i)=1 
sum1=0 

INotEqual1:IF(i.NE.1)THEN 
print*,'inside prod alpha beta',i,j 
LOOP_PROD_ALPHA_BETA:DO k=1,i-1 
sum1=sum1+(alpha(i,k)*beta(k,j)) 
END DO LOOP_PROD_ALPHA_BETA 
END IF INotEqual1 

beta(i,j)=AA(i,j)-sum1 
print*,'printing betaqqqqqqqqqqq',beta(i,j) 
decomposed(i,j)=beta(i,j) 
print*,'printing decomposed',decomposed(i,j) 
END DO LOOP_ROWS

This computes the following:

1,1
2,1;2,2
3,1;3,2;3,3
...

You need to iterate all the way through your dimensions, for example if your matrix is 10x10, you need to run you loop as follows:

Code:
do j = 1, 10
   do i = 1, 10
      a(i,j) = ...
   end do
end do

Also, I hope what you provided was pseudo-code because it's very ugly.