6.7 Double and Triple Integrals
159
Direct Derivation The formula (6.29) can also be derived directly in the twodimensional case by applying the idea of the midpoint method. We divide the
rectangle [a, b] × [c, d] into n x × n y equal-sized parts called cells. The idea of
the midpoint method is to approximate f by a constant over each cell, and evaluate
the constant at the midpoint. Cell (i, j ) occupies the area
[a + ih x , a + (i + 1)h x ] × [c + j h y , c + (j + 1)h y ],
and the midpoint is (x i , y j ) with
x i = a + ih x +
1
2
h x , y j = c + j h y +
1
2
h y .
The integral over the cell is therefore h x h y f (x i , y j ), and the total double integral is
the sum over all cells, which is nothing but formula (6.29).
Programming a Double Sum The formula (6.29) involves a double sum, which is
normally implemented as a double for loop. A Python function implementing (6.29)
may look like
def midpoint_double1(f, a, b, c, d, nx, ny):
hx = (b - a)/nx
hy = (d - c)/ny
I = 0
for i in range(nx):
for j in range(ny):
xi = a + hx/2 + i*hx
yj = c + hy/2 + j*hy
I = I + hx*hy*f(xi, yj)
return I
If this function is stored in a module file midpoint_double.py, we can compute
some integral, e.g.,
3
2
2
0 (2x + y)dxdy = 9 in an interactive shell and demonstrate
that the function computes the right number:
In [1]: from midpoint_double import midpoint_double1
In [2]: def f(x, y):
...:
return 2*x + y
...:
In [3]: midpoint_double1(f, 0, 2, 2, 3, 5, 5)
Out[3]: 9.000000000000005
Reusing Code for One-Dimensional Integrals It is very natural to write a twodimensional midpoint method as we did in function midpoint_double1 when we
have the formula (6.29). However, we could alternatively ask, much as we did in
the mathematics, can we reuse a well-tested implementation for one-dimensional
integrals to compute double integrals? That is, can we use function midpoint
def midpoint(f, a, b, n):
h = (b-a)/n
f_sum = 0
159
Direct Derivation The formula (6.29) can also be derived directly in the twodimensional case by applying the idea of the midpoint method. We divide the
rectangle [a, b] × [c, d] into n x × n y equal-sized parts called cells. The idea of
the midpoint method is to approximate f by a constant over each cell, and evaluate
the constant at the midpoint. Cell (i, j ) occupies the area
[a + ih x , a + (i + 1)h x ] × [c + j h y , c + (j + 1)h y ],
and the midpoint is (x i , y j ) with
x i = a + ih x +
1
2
h x , y j = c + j h y +
1
2
h y .
The integral over the cell is therefore h x h y f (x i , y j ), and the total double integral is
the sum over all cells, which is nothing but formula (6.29).
Programming a Double Sum The formula (6.29) involves a double sum, which is
normally implemented as a double for loop. A Python function implementing (6.29)
may look like
def midpoint_double1(f, a, b, c, d, nx, ny):
hx = (b - a)/nx
hy = (d - c)/ny
I = 0
for i in range(nx):
for j in range(ny):
xi = a + hx/2 + i*hx
yj = c + hy/2 + j*hy
I = I + hx*hy*f(xi, yj)
return I
If this function is stored in a module file midpoint_double.py, we can compute
some integral, e.g.,
3
2
2
0 (2x + y)dxdy = 9 in an interactive shell and demonstrate
that the function computes the right number:
In [1]: from midpoint_double import midpoint_double1
In [2]: def f(x, y):
...:
return 2*x + y
...:
In [3]: midpoint_double1(f, 0, 2, 2, 3, 5, 5)
Out[3]: 9.000000000000005
Reusing Code for One-Dimensional Integrals It is very natural to write a twodimensional midpoint method as we did in function midpoint_double1 when we
have the formula (6.29). However, we could alternatively ask, much as we did in
the mathematics, can we reuse a well-tested implementation for one-dimensional
integrals to compute double integrals? That is, can we use function midpoint
def midpoint(f, a, b, n):
h = (b-a)/n
f_sum = 0
