Example 1. Numerical Integration

Program Example1
c#######################################################################
c#
c# This is an MPI example on parallel integration
c# Demonstrate here is the serial program.
c#
c# Dr. Kadin Tseng
c# Scientific Computing and Visualization
c# Boston University
c# 1998
c#
c#######################################################################
implicit none
integer n, p, i, j
real h, result, a, b, integral, pi
pi = acos(-1.0) !! = 3.14159...
a = 0.0 !! lower limit of integration
b = pi*1./2. !! upper limit of integration
p = 4 !! number of processes (partitions)
n = 500 !! number of increment within each process
h = (b-a)/n/p !! length of increment
result = 0.0 !! stores answer to the integral
do i=0,p-1 !! sum of integrals over all processes
result = result + integral(a,i,h,n)
enddo
print *,'The result =',result
stop
end
real function integral(a,i,h,n)
implicit none
integer n, i, j
real h, h2, aij, a
real fct, x
fct(x) = cos(x) !! kernel of the integral
integral = 0.0 !! initialize integral
h2 = h/2.
do j=0,n-1 !! sum over all "j" integrals
aij = a + (i*n +j)*h !! lower limit of "j" integral
integral = integral + fct(aij+h2)*h
enddo
return
end