Gaussian Elimination in Fortran

In summary: I also asked if you could give us a sample of what the program's output looks like so far. Can you do that?In summary, the conversation is about a person trying to recreate the naive gauss elimination method in Fortran 95 but encountering problems. They have shared their program and are looking for help in finding the error. They are advised to add extra PRINT statements to track the progress of the program and are also asked for a sample of the current output.
  • #1
50Cent
33
0
Hi,

I am trying to recreate the naive gauss elimination method in fotran 95 but am having a few problems with it. The idea is to read in a nxn matrix of equations, so you can type in any number when u start the program and then the program will ask you to enter the relavant amount of coefficients.

This is my program so far:
PROGRAM Gauss1
! Solution of a system of N linear equations
! A.X = C
IMPLICIT NONE
PARAMETER (IN=20)
REAL:: A(IN,IN), X(IN), C(IN), F, SUM
INTEGER:: K, I, J, N, IN

! Read or input A and C here

PRINT *,'Input the number of equations (N):'
READ (5,*) N

PRINT *,'Input the matrix coefficients [A(I,J)]:'
READ (5,*) ((A(I,J),J=1,N),I=1,N)

PRINT *,'Input the solution vector[C(I)]:'
READ (5,*) (C(IN),N=1,N)

! Decomposition (Elimination)
DO K = 1, N-1
DO I = K+1, N
F = A(I,K) / A(K,K)
DO J = K+1, N
A(I,J) = A(I,J) - F * A(K,J)
ENDDO
C(I) = C(I) - F * C(K)
ENDDO
ENDDO

! Back-substitution
X(IN) = C(IN) / A(IN,IN)
DO I = N-1, 1, -1
SUM = 0.0
DO J = I+1, N
SUM = SUM + A(I,J) * X(J)
ENDDO
X(I) = (C(I) - SUM) / A(I,I)
ENDDO

! Print out results X here
WRITE (6,*) 'x(1)= ',X(1), ' x(2)= ',X(2), ' x(3)= ',X(3)

STOP

END PROGRAM Gauss1

I know the last print command is incorrect because its only specific for 3 unknowns. I didnt know how to write it to print N unknowns.

At the moment the program compiles and runs, asks for the the coefficients but then stops there. Can anyone see the problem?

Any help is much appreciated
Thanks
 
Technology news on Phys.org
  • #2
Does it ask for the solution vector?

If it does, then I would add some extra PRINT statements so as to see exactly how far the program is going. For example, first add one immediately before the back-substitution section. If you don't see that output, then add a PRINT statement inside the loops of the decomposition section, that displays the values of I, J and K. Otherwise add a similar PRINT statement inside the loops of the back-substitution section. Etc.

The general idea is to make the program itself tell you what it is actually doing, instead of trying to puzzle it out simply by reading the code.
 
  • #3
Hi, Thanks for the reply. Yes i do need to find the solution vector X(IN).

I think the problem is actually the last print statement that i have. The one i have used is for a set 3x3 matrix. Do you know how to code it so that it will print "N" solutions?

i tried this:
WRITE(6,*) (X(IN),IN=1,N)

but get the error:
Compiling file: Gauss1.f95
H:\ENG3068\Gauss1\Gauss1.F95(43) : error 227 - DO control variables must be INTEGER or REAL scalar variables only
H:\ENG3068\Gauss1\Gauss1.F95(43) : error 52 - Compilation abandoned
Compilation failed.

In the meanwhile i will add the extra print statements to check how far the program is going

Thanks
 
  • #4
50Cent said:
Hi, Thanks for the reply. Yes i do need to find the solution vector X(IN).

Hardly surprising :wink: That's not what jtbell asked about. He asked IF the program asks you to enter solution vector - you wrote

asks for the the coefficients but then stops there.

So we don't know if problems started BEFORE elimination loop, or if the program was able to get INTO elimination loop.
 
Last edited:
  • #5
Exactly. :biggrin:
 

Related to Gaussian Elimination in Fortran

1. What is Gaussian Elimination in Fortran?

Gaussian Elimination is a popular numerical method used in Fortran programming to solve systems of linear equations. It involves transforming a system of equations into an equivalent upper triangular matrix, which can then be easily solved using back substitution.

2. How does Gaussian Elimination work in Fortran?

In Fortran, Gaussian Elimination involves transforming a system of equations into an augmented matrix and then using a series of row operations to reduce the matrix to an upper triangular form. This is done by using the FOR loop and IF statements to manipulate the matrix elements.

3. What are the advantages of using Gaussian Elimination in Fortran?

Gaussian Elimination in Fortran is a powerful and efficient method for solving systems of linear equations. It is particularly useful for large systems with many variables, as it reduces the number of calculations required compared to other methods. Additionally, Fortran's built-in array and loop functions make it easy to implement Gaussian Elimination in code.

4. Are there any limitations to using Gaussian Elimination in Fortran?

One limitation of Gaussian Elimination in Fortran is that it can be computationally expensive for very large systems of equations. Additionally, the method may encounter numerical instability or round-off errors when dealing with ill-conditioned matrices. In these cases, alternative methods such as LU decomposition may be more suitable.

5. Is Gaussian Elimination in Fortran still relevant in modern scientific computing?

Yes, Gaussian Elimination is still a widely used method in scientific computing, including in Fortran programming. While alternative methods such as LU decomposition and QR decomposition have been developed, Gaussian Elimination remains a popular and efficient method for solving systems of linear equations in Fortran, particularly for large systems with many variables.

Similar threads

  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
20
Views
3K
  • Programming and Computer Science
Replies
4
Views
963
  • Programming and Computer Science
Replies
22
Views
4K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
Replies
1
Views
2K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
Back
Top