next up previous
Next: 3 Conclusion Up: Introduction to some Basic Previous: 1 Numerical Derivatives

2 Numerical Integrals

We can use the Taylor series to calculate numerical approximations to integrals too. Let's look at integrating the function f (x) from x0 to x0 + h numerically. Expanding f (x) around the midpoint of the interval we have
$\displaystyle\int_{a}^{a+h}$f (x)dx = $\displaystyle\int_{a}^{a+h}$$\displaystyle\left [ f(a+h/2) + f'(a+h/2) \left (x-a-\frac{h}{2}
\right) +O(h^2)\right]d$x   
  = f (a + h/2)h + O(h 3) (13)
To apply this to the integral from a to b , we can divide it into N subintervals with
h = $\displaystyle{\frac{b-a}{N}}$   
xi = a + $\displaystyle\left (i - \frac{1}{2} \right)~$h , (14)
and
 
$\displaystyle\int_{a}^{b}$dxf (x) = $\displaystyle\sum_{i=1}^{N}$f (xi)h + N O(h 3)   
  = $\displaystyle{\frac{b-a}{N}}$$\displaystyle\sum_{i=1}^{N}$f (xi) + O(N - 2) (15)
It is interesting to notice that round off will produce an error of approximately rf (x) in f (x) , where r is the precision defined in section 1, which bounded by (b - a)rmax (f (x)) , which is independent of N if the sum is done carefully. Integrations tend to be much more stable than derivatives when decreasing the step size.

Now that we know how to do a simple integral let's see how to do the various kinds of integrals we will see in electrodynamics. Often we need line integrals like

$\displaystyle\int_{\vec \ell}^{}$$\displaystyle\vec{A}$(x,y,z) $\displaystyle\cdot$ d$\displaystyle\vec{\ell}$ (16)

either around a loop or along a path between two end points. One way is to first define the path parametrically. For example if we need to calculate the line integral of $\vec{A}$($\vec{r}$) around an ellipse with semimajor axis of length a parallel to $\hat{x}$ , semiminor axis of length b in the y - z plane making an angle of $\alpha$ with the y axis, we can define the path parametrically as
$\displaystyle\vec{\ell}$(u) = acos(u)$\displaystyle\hat{x}$ + bsin(u)cos($\displaystyle\alpha$)$\displaystyle\hat{y}$ + bsin(u)sin($\displaystyle\alpha$)$\displaystyle\hat{z}$      (17)
where u runs from 0 to 2$\pi$ . The line integral can be approximated by
 
ui $\textstyle\equiv$ $\displaystyle{\frac{2 \pi}{N}}$i   
$\displaystyle\vec{r}_{i}^{}$ $\textstyle\equiv$ $\displaystyle\vec{\ell}$$\displaystyle\left (\frac{u_i+u_{i-1}}{2} \right)$   
$\displaystyle\vec{h}_{i}^{}$ $\textstyle\equiv$ $\displaystyle\vec{\ell}$(ui) - $\displaystyle\vec{\ell}$(ui - 1)   
$\displaystyle\int_{\vec \ell}^{}$$\displaystyle\vec{A}$(x,y,z) $\displaystyle\cdot$ d$\displaystyle\vec{\ell}$ = $\displaystyle\sum_{i=1}^{N}$$\displaystyle\vec{A}$($\displaystyle\vec{r}_{i}^{}$) $\displaystyle\cdot$ $\displaystyle\vec{h}_{i}^{}$ + O(N - 2) (18)
Notice that this is written in a form exactly analogous to Eq. 15, and is an explicit example of what is shown in figure 1.3 of Jackson. The generalization to other paths should now be easy.

Next we need to consider two-dimensional integrals. One way to perform these is to apply the one-dimensional method for each dimension, or equivalently Taylor series expand in both x and y . To integrate over a small rectangle of sides ha and hb ,

$\displaystyle\int_{a}^{a+h_a}$$\displaystyle\int_{b}^{b+h_b}$f (x,y)dxdy = $\displaystyle\int_{a}^{a+h_a}$$\displaystyle\int_{b}^{b+h_b}$$\displaystyle\left [
f(a+h_a/2,b+h_b/2) \right.$   
  + fx(a + ha/2,b + hb/2)(x - a - $\displaystyle{\frac{h_a}{2}}$)   
  + $\displaystyle\left . f_y(a+h_a/2,b+h_b/2) (y-b-\frac{h_b}{2})
+O(h^2) \right]d$xdy   
  = f (a + ha/2,b + hb/2)hahb + O(h 4) (19)
where O(h 4) etc. means that order of products of the factors ha and hb , and fx and fy are the partial derivatives of f with respect to x and y . Notice this is nothing but the value of the function at the center of the rectangle multiplied by the area of the rectangle. If we integrate over an area using N points total (that is N little rectangles or roughly N 1/2 in each direction), the integration error will be O(N - 1) . Notice we can easily generalize this to other basic shapes by multiplying a value of the function at a central point times the area of the shape.

Going through the same mathematics for a three dimensional integral, we get the obvious generalization of the value of the function at the center of the 3-d rectangular prism times the volume of the prism,

$\displaystyle\int_{a}^{a+h_a}$$\displaystyle\int_{b}^{b+h_b}$$\displaystyle\int_{c}^{c+h_c}$f (x,y,z)dxdydz = f (a + ha/2,b + hb/2,c + hc/2)hahbhc + O(h 5) (20)

and if we integrate over a volume using N points total, the integration error will be O(N - 2/3) . For a general d dimensional integration, the midpoint rule will give an error that goes like O(N - 2/d) . Again we can generalize to other shapes by multiplying a value of the function at a central point times the volume of the shape.

It is instructive to do the line integral of $\vec{E}$ around the outside of a very small square of side h centered at (x0,y0,z0) . Let's take the square to be oriented along the x and y axes. The line integral is,

$\displaystyle\int$$\displaystyle\vec{E}$($\displaystyle\vec{r}$) $\displaystyle\cdot$ d$\displaystyle\vec{\ell}$ = h[Ey(x0 + h/2,y0,z0) - Ex(x0,y0 + h/2,z0) - Ey(x0 - h/2,y0,z0) + Ex(x0,y0 - h/2,z0)] . (21)

Notice that the right hand side is our central difference formula for the z component of the curl at the center of the square, multiplied by h 2. That is, we have proved Stokes theorem. A similar application to a surface integral over a little cube will give the divergence theorem.

Let's see how to apply our integration to a surface integral like those typically found in electromagnetism. Often we want to integrate the surface normal component of a vector field over a surface. For example, we might want to integrate the normal component of the electric field over a surface

$\displaystyle\int$$\displaystyle\vec{E}$ $\displaystyle\cdot$ d$\displaystyle\vec{A}$ . (22)

Rather than do a trivial problem like the surface of a cube or sphere, lets integrate over an ellipsoidal surface. We do this problem in two ways. First we imagine we are going to set up the problem analytically and then do the double integral numerically at the end. Second we will imagine doing it numerically from the beginning.

One way to define the surface is parametrically, similarly to the way we defined a path parametrically. We pick cartesian coordinates so that the origin is at the center of our ellipsoid with the coordinate axes aligned with the semiaxes of the ellipsoid. The surface can be given by

x(u,v) = asin(u)cos (v)   
y(u,v) = bsin(u)sin (v)   
z(u,v) = ccos (u) (23)
with 0 $\leq$ u $\leq$ $\pi$ , 0 $\leq$ v $\leq$ 2$\pi$ . As with any parametric definition, there are an infinity of choices you can make. This parameterization defines the vector

$\displaystyle\vec{S}$(u,v) = x(u,v)$\displaystyle\hat{x}$ + y(u,v)$\displaystyle\hat{y}$ + z(u,v)$\displaystyle\hat{z}$ (24)

which describes our surface. The directed area of the surface corresponding to a small increment in u and v is

d$\displaystyle\vec{A}$ = $\displaystyle\left [ \frac{\partial \vec S(u,v)}{\partial u}
\times \frac{\partial \vec S(u,v)}{\partial v} \right]d$udv , (25)

where we have chosen the sign for our particular problem so that d$\vec{A}$ points outward. The analytic expression for the surface integral is

$\displaystyle\int_{0}^{2\pi}$dv$\displaystyle\int_{0}^{\pi}$du $\displaystyle\vec{E}$(x(u,v),y(u,v),z(u,v)) $\displaystyle\cdot$ $\displaystyle\left [ \frac{\partial \vec S(u,v)}{\partial u}
\times \frac{\partial \vec S(u,v)}{\partial v} \right]$ (26)

Note: if you have trouble following this to here, you are having trouble with standard analytic vector calculus not numerical vector calculus. To attempt an analytic integration, you would do the derivatives above and simplify the expression.

We will now convert our expression into a form ready for numerical calculation. We could do the derivatives analytically, but for amusement lets do the integrals using the midpoint rule and the derivatives using the central differences.
 

hu = $\displaystyle{\frac{\pi}{N_u}}$   
hv = $\displaystyle{\frac{2 \pi}{N_v}}$   
ui = (i - $\displaystyle{\textstyle\frac{1}{2}}$)hu   
vj = (j - $\displaystyle{\textstyle\frac{1}{2}}$)hv   
$\displaystyle\Delta$$\displaystyle\vec{A}_{ij}^{}$ = $\displaystyle\left [
\vec S(u_i+h_u/2,v_j) - \vec S(u_i-h_u/2,v_j)
\right]\times$$\displaystyle\left [
\vec S(u_i,v_j+h_v/2) - \vec S(u_i,v_j-h_v/2)
\right]$   
$\displaystyle\int$$\displaystyle\vec{E}$ $\displaystyle\cdot$ d$\displaystyle\vec{A}$ = $\displaystyle\sum_{i=1}^{N_u}$$\displaystyle\sum_{j=1}^{N_v}$$\displaystyle\vec{E}$(x(ui,vj),y(ui,vj),z(ui,vj)) $\displaystyle\cdot$ $\displaystyle\Delta$$\displaystyle\vec{A}_{ij}^{}$ (27)

Alternatively, you can think about doing this surface integral from a numerical point of view from the beginning. In that case, we could break up u and v into equally spaced intervals. This would divide the surface into little 4 sided areas (actually at u = 0 and u = $\pi$ one of the sides goes to zero and they become 3 sided there, but this can be ignored for now) and we can think of doing the integration by multiplying the surface area made up of the little areas bounded by the four surface points

$\displaystyle\vec{S}_{i,j,\pm,\pm}^{}$ = $\displaystyle\vec{S}$(ui $\displaystyle\pm$ hu/2,vj $\displaystyle\pm$ hv/2) (28)

If we approximate this area by that of a straight sided quadrilateral with an outward direction we get the cross products of the little vectors that go across the quadrilateral or an equivalent expression. This is just the cross product given in Eq. 27 above. Notice that we can use this ``numerical'' thinking to derive or check our analytic results and gain a better understanding of how everything fits together.


next up previous
Next: 3 Conclusion Up: Introduction to some Basic Previous: 1 Numerical Derivatives