- #1
fluidistic
Gold Member
- 3,923
- 261
I must calculate [tex]\int_0^1 e^{-x} dx[/tex] using the composite Simpson's rule, i.e. the common Simpson's rule but applied on many intervals between 0 and 1. This is not all : I must divide the interval [0,1] in 100 subintervals and then in 200, to compare the value obtained of the integral. And then I must calculate the coefficient of precision, that is Q=absolute value of [tex]\frac{s-I}{r-I}[/tex], where I is the (correct) value of the integral we are searching, s its approximation by using the Simpson's rule on 100 subintervals and r its approximation by using the S' rule on 200 subintervals. And the answer, Q, is 16. While I find almost 2. I've thought a lot about my error, and I know it's in the subroutine when computing the integral, but can't figure where exactly. The bad thing is that my result of the integral is quite similar to the real one... Should be a little mistake then.
I forgot to say, my program may be a little hard to understand. When you execute it, first it will ask the bounds of the integral, just put 0,1. Then chose 100 for n and 200 for m and we're done.
Program Simpson
implicit none
Real(8) :: x,s,x_i,x_f,r,q
Integer :: k,n,m
Write(*,*)'Enter x_i y x_f'
Read(*,*)x_i,x_f
Write(*,*)'Enter n'
Read(*,*)n
Write(*,*)'Enter m'
Read(*,*)m
Call Simp(x_i,x_f,s,n)
Write(*,*)'When n=100, s is worth',s
Call Simp2(x_i,x_f,r,m)
Write(*,*)'When n=200, s is worth',r
Q=(s-(1-exp(-1.)))/(r-(1-exp(-1.)))
Write(*,*)'The quotient of precision is',Q
Contains
Subroutine Simp(x_i,x_f,s,n)
implicit none
Real(8), intent(in) :: x_i,x_f
Real(8), Intent(out) :: s
Real(8) :: dx
Integer :: n
dx=(x_f-x_i)/n
s=(dx/3.)/(f(x_i)+f(x_f))
Do k=2,n-2,2
x=x_i+k*dx
s=s+2*f(x)
end do
do k=1,n-1,2
x=x_i+k*dx
s=s+4*f(x)
end do
s=(dx/3.)*s
end subroutine
Subroutine Simp2(x_i,x_f,r,m)
implicit none
Real(8), Intent(in) :: x_i,x_f
Real(8), Intent(out) :: r
Real(8) :: dx
Integer :: m
dx=(x_f-x_i)/m
r=(dx/3.)/(f(x_i)+f(x_f))
Do k=2,m-2,2
x=x_i+k*dx
r=r+2*f(x)
end do
do k=1,m-1,2
x=x_i+k*dx
r=r+4*f(x)
end do
r=(dx/3.)*r
end subroutine
Real(8) Function f(x)
Real(8), Intent(in) :: x
f=exp(-x)
end function
end program
I forgot to say, my program may be a little hard to understand. When you execute it, first it will ask the bounds of the integral, just put 0,1. Then chose 100 for n and 200 for m and we're done.
Program Simpson
implicit none
Real(8) :: x,s,x_i,x_f,r,q
Integer :: k,n,m
Write(*,*)'Enter x_i y x_f'
Read(*,*)x_i,x_f
Write(*,*)'Enter n'
Read(*,*)n
Write(*,*)'Enter m'
Read(*,*)m
Call Simp(x_i,x_f,s,n)
Write(*,*)'When n=100, s is worth',s
Call Simp2(x_i,x_f,r,m)
Write(*,*)'When n=200, s is worth',r
Q=(s-(1-exp(-1.)))/(r-(1-exp(-1.)))
Write(*,*)'The quotient of precision is',Q
Contains
Subroutine Simp(x_i,x_f,s,n)
implicit none
Real(8), intent(in) :: x_i,x_f
Real(8), Intent(out) :: s
Real(8) :: dx
Integer :: n
dx=(x_f-x_i)/n
s=(dx/3.)/(f(x_i)+f(x_f))
Do k=2,n-2,2
x=x_i+k*dx
s=s+2*f(x)
end do
do k=1,n-1,2
x=x_i+k*dx
s=s+4*f(x)
end do
s=(dx/3.)*s
end subroutine
Subroutine Simp2(x_i,x_f,r,m)
implicit none
Real(8), Intent(in) :: x_i,x_f
Real(8), Intent(out) :: r
Real(8) :: dx
Integer :: m
dx=(x_f-x_i)/m
r=(dx/3.)/(f(x_i)+f(x_f))
Do k=2,m-2,2
x=x_i+k*dx
r=r+2*f(x)
end do
do k=1,m-1,2
x=x_i+k*dx
r=r+4*f(x)
end do
r=(dx/3.)*r
end subroutine
Real(8) Function f(x)
Real(8), Intent(in) :: x
f=exp(-x)
end function
end program