Está en la página 1de 6

PROGRAM MinPotencia

IMPLICIT NONE
INTEGER, PARAMETER :: dbl = SELECTED_REAL_KIND(p = 15)
REAL(KIND=dbl),ALLOCATABLE,dimension(:,:):: a, invA,y
REAL(KIND=dbl),ALLOCATABLE,dimension(:)::b, x,z,u
REAL(KIND=dbl):: l,P,t,v,h
CHARACTER NOMBRE*30
Integer:: sal, errorflag, i, j, n, FLAG,np,m,mp,k,nn

! PRESENTACIN DEL PROGRAMA


write(*,*) ' '
write(*,100)
100 format(1x,'ESTE ES EL METODO DE AJUSTE A CURVAS CON UNA POTENCIA,
PARA UN CONJUNTO',/,&
1X,'DE DATOS PROPORCIONADOS POR EL USUARIO',/)

write(*,*) ' '


write(*,110,advance='no')
110 FORMAT(3X,'Ingrese el numero de datos n = ')
read(*,*)n

! Inicializa vectores y matrices en memoria dinmica


ALLOCATE(A(2,2))
ALLOCATE(invA(n,n))
ALLOCATE(y(n,n))
ALLOCATE(x(n))
ALLOCATE(b(2),z(26),u(26))

!Definicin de ndices
nn=n

write(*,*) ' '


write(*,*)' Ingrese los valores de xi y f(xi) en el mismo renglon
separados por blancos'
write(*,*) ' '
DO 10 i=1,n
read(*,*) x(i),y(i,1)
10 continue

write(*,*) ' '

! Seleccin del medio de salida de resultados


WRITE (*,41)
41 FORMAT(/,3X,'Seleccione el medio de presentacin de resultados: ',/,&
3X,'1.- Pantalla',/,&
3X,'2.- Archivo de texto',//)

WRITE(*,51,advance = 'no')
51 FORMAT(3X,'Ingrese 1 o 2 segun lo desee => ')
READ (*,*) flag

IF (flag .EQ. 2) THEN


WRITE (*,61)
61 FORMAT(/,3X,'Ingrese el nombre del archivo de salida,
escribiendolo',/,&
3X,"entre apostrofes, por ejemplo: 'RESULTADOS.RES'
")
WRITE (*,71, advance = 'no')
71 FORMAT(/3X,'Archivo de salida => ')
READ (*,*) NOMBRE
SAL = 3
OPEN (UNIT = SAL, FILE = NOMBRE, STATUS ='NEW')
ELSE
SAL = 6
END IF

! Ciclos para formar la matriz de coeficientes A


A(1,1) = n

P=0
do i=1,n
P = log(x(i)) + P
end do
A(1,2)=P
A(2,1)=P

P=0
do i=1,n
P = log(x(i))**2 + P
end do

A(2,2) = P

! Ciclo para formar el vector de constantes b

P=0
do i=1,n
P = log(y(i,1)) + P
end do

b(1) = P

P=0
do i=1,n
P = log(x(i))*log(y(i,1)) + P
end do
b(2) = P

! Presentacin de la matriz A
write(sal,*) ' '
write(sal,300)
300 format(3x,'La matriz A generada es: ')
DO i = 1,2
write (sal,8) (A(i,j), j = 1,2)
8 format (8(3X, F10.4))
END DO
WRITE(sal,*)' '
! Presentacin del vector b

write(sal,*) ' '


write(sal,301)
301 format(3x,'El vector de constantes b generado es: ')
DO i = 1,2
write (sal,8) b(i)
END DO
WRITE(sal,*)' '

n = 2
m = n
np = n
mp = n

! Llama a la subrutina Gauss para resolver el sistema


call gaussj(a,n,np,b,m,mp)

! print matrix A and vector b after the elimination


write (sal,202)
do i = 1,n
write (sal,200) (a(i,j),j=1,n), b(i)
200 format (3x,6f12.6)
end do
! print solutions
write (sal,203)
Do i=1,n
write (sal,201) i,b(i)
201 format (3x,'c(',I2,') = ',6f12.6)
end do
202 format (/,' La Matriz A y el vector b despues de la eliminacion')
203 format (/,' La solucion del sistema de ecuaciones simultaneas es:')
!end
b(1)=exp(b(1))

! Coeficientes de la ecuacin exponencial


write (sal,125) b(1),b(2)
125 format(/,3x,'La funcion de potencia de ajuste es y =
',f7.4,'x^(',f7.4,')',/)

! Determinacin de la tabla de ajuste


t=maxval(x)
v=minval(x)

h=(t-v)/25

!vector z
Do i=1,26
z(i)=v+(h*(i-1))
end do

! Ciclo para obtener la tabla de ajuste


do j=1,26
u(j)=b(1)*z(j)**b(2)
end do
!Tabla de datos proporcionados
write(sal,*) ' '
write(sal,304)
304 format(3x,'Los datos proporcionados: ')
do i=1,nn
write(sal,111) x(i),y(i,1)
end do

! Tabla de valores ajustados


write(sal,108)
108 format(/,3x,'Tabla de valores de (x,y(x))')
write(sal,*) ' '
! Imprime la tabla de P(x)
Do i=1,26
write(sal,111) z(i),u(i)
111 format(3x,F10.4,2x,F10.4)
end do

pause
end program MinPotencia

!
*************************************************************************
******

SUBROUTINE gaussj(a,n,np,b,m,mp)
INTEGER m,mp,n,np,NMAX
!REAL a(np,np),b(np,mp)
double precision a(np,np), b(np,mp), x(n)
PARAMETER (NMAX=50)
!Linear equation solution by Gauss-Jordan elimination, equation (2.1.1)
above. a(1:n,1:n)
!is an input matrix stored in an array of physical dimensions np by np.
b(1:n,1:m) is an input
!matrix containing the m right-hand side vectors, stored in an array of
physical dimensions
!np by mp. On output, a(1:n,1:n) is replaced by its matrix inverse, and
b(1:n,1:m) is
!replaced by the corresponding set of solution vectors.
!Parameter: NMAX is the largest anticipated value of n.
INTEGER i,icol,irow,j,k,l,ll,indxc(NMAX),indxr(NMAX),&
ipiv(NMAX) !The integer arrays ipiv, indxr, and indxc are
used
REAL big,dum,pivinv !for bookkeeping on the pivoting.
do j=1,n
ipiv(j)=0
end do

do i=1,n !This is the main loop over the columns to be reduced.


big=0.
do j=1,n !This is the outer loop of the search for a pivot
element.
if(ipiv(j).ne.1)then
do k=1,n
if (ipiv(k).eq.0) then
if (abs(a(j,k)).ge.big)then
big=abs(a(j,k))
irow=j
icol=k
end if
else if (ipiv(k).gt.1) then
pause 'singular matrix in gaussj'
end if
end do
end if
end do
ipiv(icol)=ipiv(icol)+1
!We now have the pivot element, so we interchange rows, if needed, to put
the pivot
!element on the diagonal. The columns are not physically interchanged,
only relabeled:
!indxc(i), the column of the ith pivot element, is the ith column that is
reduced, while
!indxr(i) is the row in which that pivot element was originally located.
If indxr(i) 6=
!indxc(i) there is an implied column interchange. With this form of
bookkeeping, the
!solution b's will end up in the correct order, and the inverse matrix
will be scrambled
!by columns.
if (irow.ne.icol) then
do l=1,n
dum=a(irow,l)
a(irow,l)=a(icol,l)
a(icol,l)=dum
end do

do l=1,m
dum=b(irow,l)
b(irow,l)=b(icol,l)
b(icol,l)=dum
end do
end if
indxr(i)=irow !We are now ready to divide the pivot row by the pivot
indxc(i)=icol !element, located at irow and icol.
if (a(icol,icol).eq.0.) pause 'singular matrix in gaussj'
pivinv=1./a(icol,icol)
a(icol,icol)=1.

do l=1,n
a(icol,l)=a(icol,l)*pivinv
end do

do l=1,m
b(icol,l)=b(icol,l)*pivinv
end do

do ll=1,n !Next, we reduce the rows...


if(ll.ne.icol)then !...except for the pivot one, of course.
dum=a(ll,icol)
a(ll,icol)=0.
do l=1,n
a(ll,l)=a(ll,l)-a(icol,l)*dum
end do

do l=1,m
b(ll,l)=b(ll,l)-b(icol,l)*dum
end do
end if
end do
end do !This is the end of the main loop over columns of the
reduction.
do l=n,1,-1 !It only remains to unscramble the solution in view
!of the column interchanges. We do this by interchanging
!pairs of columns in the reverse order
!that the permutation was built up.
if(indxr(l).ne.indxc(l))then
do k=1,n
dum=a(k,indxr(l))
a(k,indxr(l))=a(k,indxc(l))
a(k,indxc(l))=dum
end do
end if
end do
return !And we are done.
END

También podría gustarte