Você está na página 1de 5

Universidade de Braslia

Departamento de Engenharia Mecnica


Daniel de Oliveira da Silva
Matr.: 09/0109970

O programa a seguir capaz de fornecer o resultado para um sistema do tipo AX=B,


sendo que A(nxn) e B(nx1) so gerados aleatoriamente. O programa tem como input a
dimenso n do sistema e fornece os resultados em um arquivo texto com nome aoutput.txt
localizado na mesma pasta do executvel. Segue em anexo um conjunto de resultados obtidos
no arquivo aoutput.txt aps a execuo do programa com n=10.
Cdigo do programa:
!Daniel de Oliveira da Silva
program system_solver
implicit none
integer :: n
integer :: l
integer :: c
write(*,*) "Entre com a dimenso n das matrizes A(nxn) e B(nx1)"
read (*,*) l
c=l
n=l*c
OPEN(UNIT=1, FILE="aoutput.txt")
write(1,*) "O programa fornece o resultado de um sistema AX=B, sendo A(nxn) e B(nx1) gerados
aleatriamente."
write(1,*) "Para n=",l,"tem-se:"
write(1,*) " "
call randnum(n,l,c)
end program
!////////////////////////////////////////////////////////////////////////
!Esta subrotina resolve um sistema AX=B gerado aleatriamente
subroutine randnum(n,l,c)

integer :: n
integer ::l
integer ::c
real :: y(l)
real :: b(l)
real :: x(n)
real :: t(l)
real :: p(l)
real :: w(l,c)
real :: s(l,c)
real :: h(l,c)
real :: m(l,l)
OPEN(UNIT=1, FILE="aoutput.txt")
call random_seed
call random_number(x)
call matriz(x,n,l,c,w)
call random_number(y)
call matriz(y,l,l,1,b)
write(1,*)"Matriz A:"
write(1,*)" "
call imprimir(l,c,w)
write(1,*)" "
write(1,*)"Vetor B:"
write(1,*)" "
call imprimir(l,1,b)
n=l
s=w
call inverse(w,h,n)
write(1,*)" "
write(1,*)"Matriz A inversa:"
write(1,*)" "
call imprimir2(l,c,h)
m= MATMUL(s,h)
write(1,*)" "
write(1,*)"Matriz identidade I=(A^(-1)A):"
write(1,*)" "
call imprimir(l,c,m)
t= MATMUL(h,b)
write(1,*)" "
write(1,*)"Vetor X(Resultado):"

write(1,*)" "
call imprimir(l,1,t)
write(1,*)" "
p= MATMUL(s,t)
write(1,*)" "
write(1,*)"Produto AX=B:"
write(1,*)" "
call imprimir(l,1,p)
write(1,*)" "
return
end subroutine
!////////////////////////////////////////////////////////////////////////
!////////////////////////////////////////////////////////////////////////
!Subrotina para transformar um vetor x(n) em uma matriz A(l,c)
subroutine matriz(x,n,l,c,w)
integer :: n
integer :: l
integer :: c
real :: x(n)
real :: w(l,c)
w= reshape(x(1:n),[l,c])
end subroutine
!/////////////////////////////////////////////////////////////////////////
!/////////////////////////////////////////////////////////////////////////
subroutine imprimir(l,c,w)
integer :: l
integer :: c
real :: w(l,c)
OPEN(UNIT=1, FILE="aoutput.txt")
do i=1,l
write (1,10) (w(i,j),j=1,c)
write (1,*) " "
end do
10 format(100f6.3)
return
end subroutine
!//////////////////////////////////////////////////////////////////////////
!/////////////////////////////////////////////////////////////////////////

subroutine imprimir2(l,c,w)
integer :: l
integer :: c
real :: w(l,c)
OPEN(UNIT=1, FILE="aoutput.txt")
do i=1,l
write (1,11) (w(i,j),j=1,c)
write (1,*) " "
end do
11 format(100f7.3)
return
end subroutine
!//////////////////////////////////////////////////////////////////////////
subroutine inverse(w,h,n)
!============================================================
! Inverse matrix
! Method: Based on Doolittle LU factorization for Ax=b
! Alex G. December 2009
!----------------------------------------------------------! input ...
! w(n,n) - array of coefficients for matrix A
! n - dimension
! output ...
! h(n,n) - inverse matrix of A
! comments ...
! the original matrix a(n,n) will be destroyed
! during the calculation
!===========================================================
implicit none
integer n
real w(n,n), h(n,n)
double precision L(n,n), U(n,n), b(n), d(n), x(n)
double precision coeff
integer i, j, k
! step 0: initialization for matrices L and U and b
! Fortran 90/95 aloows such operations on matrices
L=0.0
U=0.0
b=0.0
! step 1: forward elimination
do k=1, n-1
do i=k+1,n

coeff=w(i,k)/w(k,k)
L(i,k) = coeff
do j=k+1,n
w(i,j) = w(i,j)-coeff*w(k,j)
end do
end do
end do
! Step 2: prepare L and U matrices
! L matrix is a matrix of the elimination coefficient
! + the diagonal elements are 1.0
do i=1,n
L(i,i) = 1.0
end do
! U matrix is the upper triangular part of A
do j=1,n
do i=1,j
U(i,j) = w(i,j)
end do
end do
! Step 3: compute columns of the inverse matrix C
do k=1,n
b(k)=1.0
d(1) = b(1)
! Step 3a: Solve Ld=b using the forward substitution
do i=2,n
d(i)=b(i)
do j=1,i-1
d(i) = d(i) - L(i,j)*d(j)
end do
end do
! Step 3b: Solve Ux=d using the back substitution
x(n)=d(n)/U(n,n)
do i = n-1,1,-1
x(i) = d(i)
do j=n,i+1,-1
x(i)=x(i)-U(i,j)*x(j)
end do
x(i) = x(i)/u(i,i)
end do
! Step 3c: fill the solutions x(n) into column k of h
do i=1,n
h(i,k) = x(i)
end do
b(k)=0.0
end do
end subroutine inverse
!//////////////////////////////////////////////////////////////////////////

Você também pode gostar