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

  • Thread starter Thread starter svishal03
  • Start date Start date
  • Tags Tags
    Error Runtime
AI Thread Summary
The discussion revolves around a runtime error encountered in a Fortran program implementing Crout's decomposition for solving linear algebraic equations. The issue arises when the variable `beta(i,j)` is not being calculated for `i=1` and `j=2`, leading to a failure in the expected output. The user identifies that the print statement for `beta(i,j)` is not executed under these conditions, indicating that the calculation does not occur.The code structure includes nested loops for iterating through matrix dimensions, with specific conditions for filling in the `alpha` and `beta` matrices. The problem is attributed to the current loop limits, which only fill the lower triangular part of the matrix. The suggestion is made to iterate through all dimensions of the matrix, ensuring that all elements are processed, particularly in cases where the matrix is larger than 2x2. The discussion also critiques the clarity of the provided code, suggesting that it appears poorly formatted and could benefit from better organization.
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
 
Technology 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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
22
Views
5K
Replies
7
Views
3K
Replies
2
Views
2K
Replies
5
Views
5K
Replies
5
Views
2K
Back
Top