158
6 Computing Integrals and Testing Code
how can we approximate this integral by numerical methods?
Derivation via One-Dimensional Integrals Since we know how to deal with
integrals in one variable, a fruitful approach is to view the double integral as two
integrals, each in one variable, which can be approximated numerically by previous
one-dimensional formulas. To this end, we introduce a help function g(x) and write
b
a
d
c
f (x, y)dydx =
b
a
g(x)dx, g(x) =
d
c
f (x, y)dy .
Each of the integrals
b
a
g(x)dx, g(x) =
d
c
f (x, y)dy
can be discretized by any numerical integration rule for an integral in one variable.
Let us use the midpoint method (6.20) and start with g(x) =
d
c f (x, y)dy. We
introduce n y intervals on [c, d] with length h y . The midpoint rule for this integral
then becomes
g(x) =
d
c
f (x, y)dy ≈ h y
n y −1
j =0
f (x, y j ), y j = c +
1
2
h y + j h y .
The expression looks somewhat different from (6.20), but that is because of the
notation: since we integrate in the y direction and will have to work with both x and
y as coordinates, we must use n y for n, h y for h, and the counter i is more naturally
called j when integrating in y. Integrals in the x direction will use h x and n x for h
and n, and i as counter.
The double integral is
b
a g(x)dx, which can be approximated by the midpoint
method:
b
a
g(x)dx ≈ h x
n x −1
i=0
g(x i ), x i = a +
1
2
h x + ih x .
Putting the formulas together, we arrive at the composite midpoint method for a
double integral:
b
a
d
c
f (x, y)dydx ≈ h x
n x −1
i=0
h y
n y −1
j =0
f (x i , y j )
= h x h y
n x −1
i=0
n y −1
j =0
f (a +
h x
2
+ ih x , c +
h y
2
+ j h y ) .
(6.29)
6 Computing Integrals and Testing Code
how can we approximate this integral by numerical methods?
Derivation via One-Dimensional Integrals Since we know how to deal with
integrals in one variable, a fruitful approach is to view the double integral as two
integrals, each in one variable, which can be approximated numerically by previous
one-dimensional formulas. To this end, we introduce a help function g(x) and write
b
a
d
c
f (x, y)dydx =
b
a
g(x)dx, g(x) =
d
c
f (x, y)dy .
Each of the integrals
b
a
g(x)dx, g(x) =
d
c
f (x, y)dy
can be discretized by any numerical integration rule for an integral in one variable.
Let us use the midpoint method (6.20) and start with g(x) =
d
c f (x, y)dy. We
introduce n y intervals on [c, d] with length h y . The midpoint rule for this integral
then becomes
g(x) =
d
c
f (x, y)dy ≈ h y
n y −1
j =0
f (x, y j ), y j = c +
1
2
h y + j h y .
The expression looks somewhat different from (6.20), but that is because of the
notation: since we integrate in the y direction and will have to work with both x and
y as coordinates, we must use n y for n, h y for h, and the counter i is more naturally
called j when integrating in y. Integrals in the x direction will use h x and n x for h
and n, and i as counter.
The double integral is
b
a g(x)dx, which can be approximated by the midpoint
method:
b
a
g(x)dx ≈ h x
n x −1
i=0
g(x i ), x i = a +
1
2
h x + ih x .
Putting the formulas together, we arrive at the composite midpoint method for a
double integral:
b
a
d
c
f (x, y)dydx ≈ h x
n x −1
i=0
h y
n y −1
j =0
f (x i , y j )
= h x h y
n x −1
i=0
n y −1
j =0
f (a +
h x
2
+ ih x , c +
h y
2
+ j h y ) .
(6.29)
