I am trying to find the volume under a shape defined by h(x,y)
I have the following function which uses the Simpson Rule to integrate a one-dimensional function
!----------------------------------------------------------------------------
double precision function INTEGRAL2(FUNC,a,b,N)
!----------------------------------------------------------------------------
! *** numerical integration (Simpson-rule) with equidistant spacing ***
!----------------------------------------------------------------------------
implicit none
double precision,external :: FUNC ! the function to be integrated
double precision,intent(in) :: a,b ! boundary values
integer,intent(in) :: N ! number of sub-intervals
double precision :: dx,x1,x2,xm,f1,f2,fm,int ! local variables
integer :: i
dx = (b-a)/DBLE(N) ! x subinterval
x1 = a ! left
f1 = FUNC(a)
int = 0.d0
do i=1,N
x2 = a+DBLE(i)*dx ! right
xm = 0.5d0*(x1+x2) ! midpoint
f2 = FUNC(x2)
fm = FUNC(xm)
int = int + (f1+4.d0*fm+f2)/6.d0*(x2-x1) ! Simpson rule
x1 = x2
f1 = f2 ! save for next subinterval
enddo
INTEGRAL2 = int
end
How do I use this to find the double integral of h(x,y)? Note h(x,y) is not reducible to h(x)*h(y), and I want to use this function nested, rather than modifying it/writing a function to do double integration.
I'm fundamentally stuck on the concept of writing the code, but I suspect using a module will be crucial.
Think how Simpson's rule can be derived in 1-d. You divide the domain into pairs of intervals, fit a quadratic curve to each set of 3 points and integrate that. Well, you can do the same in 2-d (or higher) - fit a bi-quadratic by Lagrange interpolation and integrate that; the weights turn out to be the same in each direction - [1,4,1]dx/3 for a 2.dx interval - as for Simpson's rule.
Incidentally, there are better forms of quadrature (e.g. gaussian quadrature) that you might like to look up.