Paste: pi calc
Author: | rex |
Mode: | fortran |
Date: | Thu, 30 Apr 2009 19:38:51 |
Plain Text |
module simpsons_rule
contains
real (kind = 8) function f(x)
implicit none
real (kind=8),intent(in)::x
f=sqrt(16.-x**2)
if(isnan(f)) then
f=0.0
end if
return
end function f
real (kind=8) function simple(a,b)
implicit none
real (kind=8),intent(in)::a,b
simple=((b-a)/6.)*(f(a)+4*f((a+b)/2.)+f(b))
return
end function simple
real (kind=8) function composite(a,b,accuracy)
implicit none
real , intent(in)::a,b
integer, intent(in)::accuracy
integer::n,i
real (kind=8)::h,integral
integral=0.0
n=accuracy
if(mod(n,2)==0) then
n=n+1
end if
h=(b-a)/n
do i=0,n-1
integral=integral+simple(a+i*h,a+(i+1)*h)
end do
composite=integral
return
end function composite
end module simpsons_rule
program pi
use simpsons_rule
implicit none
integer::i
write(*,*)"Approximations of pi:"
do i=50000,10000000,1000000
write(*,*)"n=",i+1,composite(-4.0,4.0,i+1)/8.
end do
write(*,*)"n=",20000000,composite(-4.,4.,20000000)/8;
end program pi
Author: | rex |
Mode: | fortran |
Date: | Fri, 1 May 2009 01:02:47 |
Plain Text |
module simpsons_rule
integer::r2
contains
real (kind = 8) function f(x)
implicit none
real (kind=8),intent(in)::x
f=sqrt(r2-x**2)
if(isnan(f)) then
f=0.0
end if
return
end function f
real (kind=8) function simple(a,b)
implicit none
real (kind=8),intent(in)::a,b
simple=((b-a)/6.)*(f(a)+4*f((a+b)/2.)+f(b))
return
end function simple
real (kind=8) function composite(a,b,accuracy)
implicit none
real , intent(in)::a,b
integer, intent(in)::accuracy
integer::n,i
real (kind=8)::h,integral
integral=0.0
n=accuracy
if(mod(n,2)==0) then
n=n+1
end if
h=(b-a)/n
do i=0,n-1
integral=integral+simple(a+i*h,a+(i+1)*h)
end do
composite=integral
return
end function composite
end module simpsons_rule
program pi
use simpsons_rule
implicit none
integer::max_t=17
integer::ln_n,current_n
real::r
r2=1
r=floor(sqrt(real(r2)))
write(*,*)"Approximations of pi:"
do n=1,max_t,1
write(*,*)"n=",floor(exp(t)),composite((-1)*r,r,n)*(2./r2)
end do
end program pi
Author: | rex |
Mode: | fortran |
Date: | Fri, 1 May 2009 03:01:59 |
Plain Text |
module simpsons_rule
integer::r2=2**30
contains
real (kind = 8) function f(x)
implicit none
real (kind=8),intent(in)::x
f=sqrt(r2-x**2.)
if(isnan(f)) then
f=0.0
end if
return
end function f
real (kind=8) function simple(a,b)
implicit none
real (kind=8),intent(in)::a,b
simple=((b-a)/6.)*(f(a)+4*f((a+b)/2.)+f(b))
return
end function simple
real (kind=8) function composite(a,b,accuracy)
implicit none
real , intent(in)::a,b
integer, intent(in)::accuracy
integer::n,i
real (kind=8)::h,integral
integral=0.0
n=accuracy
if(mod(n,2)==0) then
n=n+1
end if
h=(b-a)/n
do i=0,n-1
integral=integral+simple(a+i*h,a+(i+1)*h)
end do
composite=integral
return
end function composite
end module simpsons_rule
program pi
use simpsons_rule
implicit none
integer::max_t=18
integer::t,f_e_r_t,c=3
real::r
r=floor(sqrt(real(r2)))
write(*,*)"Approximations of pi:"
do t=1,max_t,2
f_e_r_t=3**t
write(*,*)"n=",f_e_r_t,":",composite((-1)*r,r,f_e_r_t)*(2./r2)
end do
write(*,*)"n=",2**30,":",composite((-1)*r,r,2**23)*(2./r2)
end program pi
New Annotation