Está en la página 1de 3

PROGRAMAS EN FORTRAN 90

Primer programa de truncamiento de la serie de Mauclarin

!error por truncamiento


!e^x = 1 + x + x^2/2! + x^3/3! + ....
program trunca
real::x,ex,fac
integer::n !n=numero de terminos
x=1
print*,"ingrese numero de terminos"
read*,n
fac=1
if (n==1) then
ex=x
end if
if (n==2) then
ex=1+x
end if
ex=2
if (n>2) then
do i=3,n
fac=fac*(i-1)
ex=ex+(x**(n-1))/(fac)
end do
end if
print*,"la suma de la serie e^x: ",ex
end program trunca

Segundo programa, modificando el programa anterior, presentar los valores que corresponde de ex para
cada uno de los trminos y mostrar el error respecto al valor de la funcin exp(x)
!error por truncamiento
!e^x = 1 + x + x^2/2! + x^3/3! + ....
program trunca
real::x,ex,fac,valor_real
real::ex1,ex2,error,error1
integer::i,n !n=numero de terminos
x=1
print*,"ingrese numero de terminos"
read*,n
fac=1
n1=1
valor_real=exp(x) !funcin de biblioteca del fortran e^x
ex1=x
error=abs(ex1-valor_real)
n2=2
ex2=ex1+x
error1=abs(ex2-valor_real)
ex=2 !variable inicializada paa numeros de terminos mayores que 2 de la serie
!salida
print 5,valor_real
5 format(2x,"Valor real de e^x para x=1:"1x,f8.4)
print*
if (n==1) then
print 10,n1,ex1,error
elseif (n>=2) then
print 10,n1,ex1,error
print 10,n2,ex2,error1
do i=3,n
fac=fac*(i-1)
ex=ex+(x**(n-1))/(fac)
error=abs(ex-valor_real)
print 10,i,ex,error
end do
end if
10 format(2x,i4,2x,f8.4,2x,f12.8)
end program trunca

ingrese numero de terminos


10
Valor real de e^x para x=1: 2.7183
1 1.0000 1.71828200
2 2.0000 .71828170
3 2.5000 .21828170
4 2.6667 .05161508
5 2.7083 .00994833
6 2.7167 .00161492
7 2.7181 .00022592
8 2.7183 .00002761
9 2.7183 .00000285
10 2.7183 .00000011
Press any key to continue

Programa para redondeo, del programa anterior solo aumentamos las cifras decimales
program redondeo
real::x,ex,fac,valor_real
real::ex1,ex2,error,error1
integer::i,n !n=numero de terminos
x=1
print*,"ingrese numero de terminos"
read*,n
fac=1
n1=1
valor_real=exp(x) !funcin de biblioteca del fortran e^x
ex1=x
error=abs(ex1-valor_real)
n2=2
ex2=ex1+x
error1=abs(ex2-valor_real)
ex=2 !variable inicializada paa numeros de terminos mayores que 2 de la serie
!salida
print 5,valor_real
5 format(2x,"Valor real de e^x para x=1:"1x,f12.6)
print*
if (n==1) then
print 10,n1,ex1,error
elseif (n>=2) then
print 10,n1,ex1,error
print 10,n2,ex2,error1
do i=3,n
fac=fac*(i-1)
ex=ex+(x**(n-1))/(fac)
error=abs(ex-valor_real)
print 10,i,ex,error
end do
end if
10 format(2x,i4,2x,f12.6,2x,f12.8)
end program redondeo

ingrese numero de terminos


10
Valor real de e^x para x=1: 2.718282
1 1.000000 1.71828200
2 2.000000 .71828170
3 2.500000 .21828170
4 2.666667 .05161508
5 2.708333 .00994833
6 2.716667 .00161492
7 2.718056 .00022592
8 2.718254 .00002761
9 2.718279 .00000285
10 2.718282 .00000011
Press any key to continue

También podría gustarte