100% found this document useful (2 votes)
2K views8 pages

Programs in Fortran

This Fortran program implements the Gauss-Jordan method to solve systems of linear equations. It begins by reading in the number of variables and populating the matrix. It then performs Gaussian elimination by iterating through the matrix rows, subtracting multiples of lower rows from higher rows to introduce zeros. Finally, it back-substitutes the resulting upper triangular matrix to solve for the variable values.

Uploaded by

diegoferro90
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
2K views8 pages

Programs in Fortran

This Fortran program implements the Gauss-Jordan method to solve systems of linear equations. It begins by reading in the number of variables and populating the matrix. It then performs Gaussian elimination by iterating through the matrix rows, subtracting multiples of lower rows from higher rows to introduce zeros. Finally, it back-substitutes the resulting upper triangular matrix to solve for the variable values.

Uploaded by

diegoferro90
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

GAUSS PROGRAM IN FORTRAN

PROGRAM GAUSS
IMPLICIT NONE
INTEGER:: I,J,K,N
REAL:: Z, aux2, aux
REAL,ALLOCATABLE :: A(:,:),x(:),P(:)
WRITE(*,*)"gauss jordan "
WRITE(*,*)"Numero de variables: "
READ(*,*)N
ALLOCATE (A(N+1,N),x(N),P(N+1))
DO I=1,N,1
DO J=1,N+1,1
WRITE(*,*)'A(', I,',', J,'):'
READ(*,*)A(J,I)
END DO
END DO
DO j=1,N-1,1
aux=A(j,j)
DO k=1,N+1,1
P(k)=A(k,j)/aux
END DO
DO i=j+1,N,1
aux2=A(j,i)
DO k=1,N+1,1
A(k,i)=A(k,i)-P(k)*aux2
END DO
END DO
END DO
DO I=1,N,1
x(I)=0
END DO
x(N)=A(N+1,N)/A(N,N)
DO I=N-1,1,-1
Z=0;
DO K=1,N,1
Z=Z+x(K)*A(k,I)
END DO
x(I)=(A(N+1,I)-Z)/A(I,I)
END DO
WRITE(*,*)'xS'
DO I=1,N,1
WRITE(*,*)"x",i,"=",x(i)
END DO
END PROGRAM GAUSS
GAUSS-JORDAN PROGRAM IN FORTRAN
PROGRAM GAUSS-JORDAN
IMPLICIT NONE
INTEGER:: I,J,K,N
REAL:: Z, aux2, aux
REAL,ALLOCATABLE :: A(:,:),x(:),P(:)
WRITE(*,*)"gauss jordan "
WRITE(*,*)"Numero de variables: "
READ(*,*)N
ALLOCATE (A(N+1,N),x(N),P(N+1))
DO I=1,N,1
DO J=1,N+1,1
WRITE(*,*)'A(', I,',', J,'):'
READ(*,*)A(J,I)
END DO
END DO
DO j=1,N-1,1
aux=A(j,j)
DO k=1,N+1,1
P(k)=A(k,j)/aux
END DO
DO i=j+1,N,1
aux2=A(j,i)
DO k=1,N+1,1
A(k,i)=A(k,i)-P(k)*aux2
END DO
END DO
END DO
DO I=1,N,1
x(I)=0
END DO
x(N)=A(N+1,N)/A(N,N)
DO I=N-1,1,-1
Z=0;
DO K=1,N,1
Z=Z+x(K)*A(k,I)
END DO
x(I)=(A(N+1,I)-Z)/A(I,I)
END DO
WRITE(*,*)'xS'
DO I=1,N,1
WRITE(*,*)"x",i,"=",x(i)
END DO
END PROGRAM GAUSS-JORDAN
PIVOTING GAUSS PROGRAM IN FORTRAN
Program pivoting gauss
implicit none

integer:: n,i,j,k
real:: tol=0.001,er
real,allocatable :: a(:,:),b(:),x(:),s(:)

write(*,*)"METODO DE GAUSS"
write(*,*)"INTRODUCE EL NUMERO INCOGNITAS EN LAS ECUACIONES: "
read(*,*)n
ALLOCATE (a(n,n),b(n),x(n),s(n))
er=0

DO i=1,n,1
DO j=1,n,1
write(*,*)'a(', i ,',', j, '):'
read(*,*)a(j,i)
END DO
write(*,*)'b(',i,'):'
read(*,*)b(i)
END DO

DO i=1,n,1
s(i)=abs(a(i,i))
DO j=2,n,1
if(abs(a(j,i))>s(i)) then
s(i)=abs(a(j,i))
end if
END DO
END DO

call eliminate(a,s,n,b,tol,er)
write(*,*)""
if (er/=-1) then
call sustitute(a,n,b,x)
end if

DO i=1,n,1
write(*,*)x(i)
END DO
end program
subroutine eliminate(a,s,n,b,tol,er)
integer:: n,i,j,k,factor
real:: tol,er
real:: a(n,n),b(n),x(n),s(n)
DO k=1,n-1,1
call pivot(a,b,s,n,k)
if(abs(a(k,k)/s(k))<tol) then
er=-1
return
end if
do i=k+1,n,1
factor=a(k,i)/a(k,k)
do j=k+1,n,1
a(j,i)=a(j,i)-factor*a(j,k)
end do
b(i)=b(i)-factor*b(k)
end do
END DO
if(abs(a(k,k)/s(k))<tol) then
er=-1
end if
return
end subroutine

subroutine pivot(a,b,s,n,k)
integer:: n,ii,jj,k,d,p
real :: a(n,n),b(n),s(n)
real :: dummy,big
p=k
big=abs(a(k,k)/s(k))
do ii=k+1,n,1
dummy=abs(a(k,ii)/s(ii))
if(dummy>big) then
big=dummy
p=ii
end if
end do
if(p/=k) then
do jj=k,n,1
dummy=a(jj,p)
a(jj,p)=a(jj,k)
a(jj,k)=dummy
end do
dummy=b(p)
b(p)=b(k)
b(k)=dummy
dummy=s(p)
s(p)=s(k)
s(k)=dummy
end if
return
end subroutine pivot

subroutine sustitute(a,n,b,x)
integer:: n,i,j
real :: a(n,n),b(n),x(n)
real :: suma
x(n)=b(n)/a(n,n)
do i=n-1,i,-1
suma=0
do j=i+1,n
suma=suma+a(j,i)*x(j)
end do
x(i)=(b(i)-suma)/a(i,i)
end do
return
end subroutine substitute
end Program pivoting gauss
PROGRAM GAUSS
!========================================
!RESOLUCION DE MATRICES A TRAVES DE ¦¦
!METODO DE GAUSS Y PIVOTEO ¦¦
!========================================
IMPLICIT NONE
INTEGER:: I,J,K,N
REAL:: FACTOR
REAL,ALLOCATABLE :: MAT(:,:),RESULTADOS(:)

WRITE(*,*)"PROGRAMA DE RESOLUCION DE ECUACIONES"


WRITE(*,*)" A PARTIR DEL METODO DE GAUSS "
WRITE(*,*)""
WRITE(*,*)""
WRITE(*,*)"INTRODUCE EL NUMERO INCOGNITAS EN LAS ECUACIONES: "
READ(*,*)N
ALLOCATE (MAT(N,N+1),RESULTADOS(N))
DO I=1,N,1
DO J=1,N+1,1
WRITE(*,*)"INTRODUCE EL ELEMENTO (",I,",",J,"): "
READ(*,*)MAT(I,J)
END DO
END DO
DO K=1,N-1,1
DO I=K,N-1,1
IF (K==I) THEN
CALL PIVOTE(MAT,N,K)
ENDIF
FACTOR=MAT(I+1,K)/MAT(K,K)
DO J=1,N+1,1
MAT(I+1,J)=MAT(I+1,J)-(FACTOR*MAT(K,J))
ENDDO
ENDDO
ENDDO
WRITE(*,*)"LA MATRIZ RESULTANTE DESPUES DE LAS OPERACIONES ES LA SIGUIENTE:"
WRITE(*,*)""
DO I=1,N,1
DO J=1,N+1,1
WRITE(*,*)"(",I,",",J,")",MAT(I,J)
ENDDO
ENDDO
CALL SOLUCION(MAT,N,RESULTADOS)
WRITE(*,*)"LOS RESULTADOS PARA LOS VALORES DE X SON LOS SIGUIENTES: "
DO I=1,N,1
WRITE(*,*)" X(",I,"):",RESULTADOS(I)
ENDDO

ENDPROGRAM

SUBROUTINE PIVOTE(MAT,N,I)
!===================================================
!SUBRUTINA QUE NOS AYUDA A DETERMINAR EL ELEMENTO ¦¦
!MAYOR A SER CONSIDERADO EL PIVOTE DE LOS CALCULOS¦¦
!Y LO REEMPLAZA EN EL ORDEN DE LOS MISMOS ¦¦
!===================================================
IMPLICIT NONE
INTEGER::I,N,K,INDICE,BAND
REAL::TEMP
REAL,DIMENSION(N,N+1)::MAT
BAND=0
INDICE=I
DO K=I+1,N,1
IF (ABS(MAT(INDICE,I))<ABS(MAT(K,I))) THEN
INDICE=K
BAND=1
ENDIF
ENDDO
IF (BAND/=0) THEN
DO K=I,N+1,1
TEMP=MAT(INDICE,K)
MAT(INDICE,K)=MAT(I,K)
MAT(I,K)=TEMP
ENDDO
ENDIF
RETURN
ENDSUBROUTINE

SUBROUTINE SOLUCION(MAT,N,RESULTADOS)
IMPLICIT NONE
INTEGER:: I,J,N
REAL:: SUMA
REAL,DIMENSION(N)::RESULTADOS
REAL,DIMENSION(N,N+1)::MAT

SUMA=0
RESULTADOS(N)=MAT(N,N+1)/MAT(N,N)
DO I=N-1,1,-1
DO J=I+1,N,1
SUMA=SUMA+(MAT(I,J)*RESULTADOS(J))
ENDDO
RESULTADOS(I)=(MAT(I,N+1)-SUMA)/MAT(I,I)
ENDDO
RETURN
ENDSUBROUTINE

You might also like