Gaussian Elimination in Fortran

Click For Summary

Discussion Overview

The discussion revolves around implementing the Gaussian elimination method in Fortran 95, focusing on issues related to reading input for a matrix of equations and printing the solution vector. Participants are troubleshooting the code and exploring how to handle dynamic matrix sizes.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant shares their Fortran code for Gaussian elimination and describes issues with printing the solution vector for a variable number of equations.
  • Another participant suggests adding PRINT statements to debug the program and understand its execution flow, particularly before and within the elimination and back-substitution loops.
  • A participant expresses confusion regarding the need for the solution vector and clarifies that the last print statement is problematic due to its fixed size for a 3x3 matrix.
  • There is a discussion about whether the program successfully prompts for the solution vector, indicating uncertainty about where the issue arises in the code execution.

Areas of Agreement / Disagreement

Participants generally agree on the need for debugging the program but have not reached a consensus on the exact source of the problem or the effectiveness of the proposed solutions.

Contextual Notes

Participants have noted issues related to the handling of dynamic array sizes and the specific syntax required for printing arrays in Fortran, which may depend on the structure of the code and the input provided.

Who May Find This Useful

Readers interested in programming in Fortran, particularly those working on numerical methods or linear algebra applications, may find this discussion relevant.

50Cent
Messages
33
Reaction score
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
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.
 
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
 
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:
Exactly. :biggrin:
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 20 ·
Replies
20
Views
4K
  • · Replies 22 ·
Replies
22
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 4 ·
Replies
4
Views
8K
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K