- #1
Telemachus
- 835
- 30
Hi, I was trying to write a code in fortran that takes an array 'fx' of length 'm', and integrates it between a and b using the successive trapezoidal rule. Off course, the length of the array, m, has to be a power of 2, and the underlying grid uniform. So, I'm not sure on how to call the vector indices, in order that, if for example, n=2, in rsum it should be calling f(m/2). If n=4, it should call f(m/4) and f(3*m/4), when n=8, it should call for f(m/8), f(3*m/8), f(5*m/8), f(7*m/8), etc. In general ##n=2^l## with l some exponent, and it determines how many partitions will be done for the interval (a,b). More over, the program is running too slow, in my opinion should go way much much faster. It takes several seconds if for example I choose m=n=50.
So, this is the subroutine I have written. The result is written in the variable 's', and iterates 'n' times on the successive trapezoidal rule:
So, this is the subroutine I have written. The result is written in the variable 's', and iterates 'n' times on the successive trapezoidal rule:
Fortran:
SUBROUTINE trapzoidal(fx,m,a,b,s,n)
integer m
real*8 fx
dimension fx(m)
REAL*8 a,b,s,h
integer n,j,k
REAL*8 T(100)
j=1
h=(b-a)
T(1)=h*(fx(1)+fx(m))/2.d0
do i=1,n
j=2*j
rsum=0.d0
h=h/2.d0
do k=1,j,2
rsum=rsum+fx(k*m/j)
enddo
T(i+1)=T(i)/2.d0+h*rsum
enddo
s=T(n+1) return
END
Last edited: