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
 f (x)dx = 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 = xi = a + h , (14)
and

 dxf (x) = f (xi)h + N O(h 3) = 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

 (x,y,z) d (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 () around an ellipse with semimajor axis of length a parallel to , semiminor axis of length b in the y - z plane making an angle of with the y axis, we can define the path parametrically as
 (u) = acos(u) + bsin(u)cos() + bsin(u)sin() (17)
where u runs from 0 to 2 . The line integral can be approximated by

 ui i (ui) - (ui - 1) (x,y,z) d = () + 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 ,

 f (x,y)dxdy = + fx(a + ha/2,b + hb/2)(x - a - ) + 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,

 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 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,

 () d = 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

 d . (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 u , 0 v 2 . As with any parametric definition, there are an infinity of choices you can make. This parameterization defines the vector

 (u,v) = x(u,v) + y(u,v) + z(u,v) (24)

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

 d = udv , (25)

where we have chosen the sign for our particular problem so that d points outward. The analytic expression for the surface integral is

 dvdu (x(u,v),y(u,v),z(u,v)) (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 = hv = ui = (i - )hu vj = (j - )hv = d = (x(ui,vj),y(ui,vj),z(ui,vj)) (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 = 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

 = (ui hu/2,vj 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: 3 Conclusion Up: Introduction to some Basic Previous: 1 Numerical Derivatives