- #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:
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
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