Sei sulla pagina 1di 4

gauss_quad.

nb 1

Gauss-Legendre Integration

Numerical integration always has the same form. To approximate an integral Ÿa f HxL „ x , you pick some points x1 , …, xn
b

from the interval @a, bD and develop weights A1 , …, An such that

Ÿa f HxL „ xºA1 f Hx1 L + ∫ + An f Hxn L


b

So far, all the integration methods we have used were based on evenly spaced points. Gauss discovered that much more
accurate methods can be derived if you just use the right unequally-spaced points.

A good way to create a method of numerical integration is to find a scheme that gives exactly correct results for polynomi-
als 1, x, x2 , …, xn . That scheme will usually give pretty accurate results for any function. The higher n is, the more
accurate is the method.

First a little detour. In the following sections, we will be presenting methods for integrals of the form Ÿ-1 f HxL „ x . However
1

if we can do these integrals, we can do any integral at all using substitution. For example:

‡ t „ t = ÅÅÅÅÅÅÅÅÅ ‡ Hx + 1L „ Ñ
3 1
2 27 2
0 8 -1

‡ sin t „ t = ÅÅÅÅÅÅ ‡ sinJ ÅÅÅÅÅÅÅÅÅ + ÅÅÅÅÅÅ N „ x


p
p 1
px p
0 2 -1 2 2

From now on we only consider integrals from -1 to 1.

Let's look for 1 < x1 < x2 < 1 and numbers A1 and A2 such that

‡ f HxL „ x º A1 f Hx1 L + A2 f Hx2 L


1

-1

We will find values for xi and Ai such that the approximation is exact for f HxL = 1, x, x2 and x3 :

2 = A1 + A2
0 = A1 x1 + A2 x2
ÅÅÅÅ23 = A1 x1 2 + A2 x2 2
0 = A1 x1 3 + A2 x2 3

If you let A1 x1 = c , then A2 xx = -c. We have ÅÅÅÅ23 = c x1 - c x2 and 0 = c x1 2 - c x2 2 . Therefore x1 2 - x2 2 = 0 but


è!!! è!!!
ÅÅÅÅ!ÅÅ . Thus:
Ñ ÅÅÅÅ!ÅÅ and x2 = ÅÅÅÅ
-1
x1 - xx ∫ 0. Thus x1 = -x2 and A1 = A2 . Since 2 = A1 + A2 , A1 = A2 = 1. Also 2 x1 2 = ÅÅÅÅ23 , so x1 = ÅÅÅÅ
1
3 3

i -1 yz ij 1 yz
‡ f HxL „ x º f jjj ÅÅÅÅÅÅÅÅ z j ÅÅÅÅÅ zz.
è!!!ÅÅÅÅÅ z + f j ÅÅÅÅÅÅÅÅ
è!!!
1

-1 k 3 { k 3 {

Let's try it on an integral whose value we know


gauss_quad.nb 2

‡ ÅÅÅÅÅÅ cosJ ÅÅÅÅÅÅÅÅÅ N „ x = 2


1
p px
-1 2 2

In[22]:= Pi ê 2 HCos@−Pi ê Sqrt@3D ê 2D + Cos@Pi ê Sqrt@3D ê 2DL êê N

Out[22]= 1.93582

Compare to the midpoint method using two points:

In[23]:= Pi ê 2 HCos@−Pi ê 4D + Cos@Pi ê 4DL êê N

Out[23]= 2.22144

The two-point Gauss-Legendre method has an error of 3.2%; the two-point midpoint method has an error of 11%.

If we used n points and n weights we could hope to integrate polynomials up to x2 n exactly, and we could use the property
to construct the points x1 , …, xn and weights A1 , …, An . This method his called Gauss-Legendre Integration. The actual
methods used to find the points and the weights is slightly more complicated and requires solving polynomial equations of
high degree. However the work has been done, and we can look it up on the website below. You may find other sources,
even Mathematica. Whatever sources you use, the points and weights should be given with 15 digit accuracy.

http://www.math.ntnu.no/num/nnm/Program/Numlibc/gauss_co.c

Let's try the integral above with four points, and compare the result to the midpoint method with four panels.

The parameters are given in order 8xi , Ai <. Only two xi values are given, because the xi are symmetric and the weight at a
negative point is the same as the weight at a positive point.

In[24]:= gc4 = 880.861136311594053, 0.347854845137454<, 80.339981043584856, 0.652145154862546<<;

gaussEstimate =
Sum@gc4@@i, 2DD HPi ê 2 Cos@−Pi gc@@i, 1DD ê 2D + Pi ê 2 Cos@Pi gc4@@i, 1DD ê 2DL, 8i, 1, 2<D
In[25]:=

midpointEstimate = Sum@0.5 ∗ Pi ê 2 Cos@Pi x ê 2D, 8x, −0.75, 0.75, .5<D

Out[25]= 1.99998

Out[26]= 2.05234

Now let's try the 24 point estimate. Again, only 12 points and 12 weights are given; the other 12 come from symmetry.

gc24 = 880.995187219997021, 0.012341229799987<, 80.974728555971309, 0.028531388628933<,


80.938274552002732, 0.044277438817419<, 80.886415527004401, 0.059298584915436<,
In[27]:=

80.820001985973902, 0.073346481411080<, 80.740124191578554, 0.086190161531953<,


80.648093651936975, 0.097618652104113<, 80.545421471388839, 0.107444270115965<,
80.433793507626045, 0.115505668053725<, 80.315042679696163, 0.121670472927803<,
80.191118867473616, 0.125837456346828<, 80.064056892862605, 0.127938195346752<<;

In[28]:= f@x_D := Pi ê 2 Cos@Pi x ê 2D


gauss_quad.nb 3

In[29]:= Sum@Hf@−gc24@@i, 1DDD + f@gc24@@i, 1DDDL gc24@@i, 2DD, 8i, 12<D êê InputForm

Out[29]//InputForm=
1.9999999999999933

ü Integrals on any domain

We can map the interval H-1, 1L to the interval Ha, bL with the transformation x = ÅÅÅÅ
a+b
2
b-a
ÅÅÅÅÅÅ + ÅÅÅÅ b-a
ÅÅÅÅÅÅ t, dx = ÅÅÅÅ
2
ÅÅÅÅÅÅ dt. Then
2

i a+b y
‡ f HxL „ x = ‡ f jj ÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅ + ÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅ tzz „ t
b-a
k 2
b-a
{
b 1
ÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅ
a -1 2 2

If we have Gauss-Legendre points xi and weights Ai , then the Gauss-Legendre approximation is:

b-a i a+b b-a y


‡ f HxL „ x º ‚ Ai ÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅ f jj ÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅ - ÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅ xi zz
k 2 {
b

a i
2 2

Thus we can write a general Gauss-Legendre integrator, where gc is the array of Gauss-Legendre coefficients on the
positive side of 0:

gaussLegendre@f_, a_, b_, gc_D :=


b−a i a+b y
In[30]:=

j
jfA gc@@i, 1DDEz
z,
b−a a+b b−a
2 k {
SumAgc@@i, 2DD − gc@@i, 1DDE + fA +

8i, 1, Length@gcD<E
2 2 2 2

Let's use this method to integrate Ÿ0 x cos x „ x using 24 points. The exact value is -2.
p

gaussLegendre@f, 0., Pi, gc24D êê InputForm


In[31]:= f@x_D := x Cos@xD

Out[32]//InputForm=
-1.999999999999979

ü Error Estimates

sufficient order on the closed domain of integration. The error estimate for the approximation of Ÿa f HxL „ x using n points
We can estimate the error in the Gauss-Legendre approximation, provided our integrand has continuous derivatives of
b

is:

22 n+1 Hn!L4
ÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅ yH2 nL HqL
H2 n + 1L@H2 nL!D3
Error = ÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅ

for some q œ @a, bD. Note that the coefficient of yH2 nL HqL rapidly decreases to 0 as n increases, so if the derivatives yH2 nL HqL do
not grow too fast then the Gauss-Legendre estimates approach the value of the integral as n Ø ¶. In particular, if the
derivatives are bounded then the estimates approach the integral. Of course, in a concrete problem, you have to come up
with actual estimates of all the quantities involved.
gauss_quad.nb 4

Example: Ÿ0 sin x „ x. Since all the derivatives of sin x have absolute value less than 1, the error in the n-point Gauss-Leg-
p

22 n+1 Hn!L4
H2 n+1L@H2 nL!D3
endre approximation will be less than ÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅ
ÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅ . If n = 24, this error bound is:

n = 24;
22 n+1 Hn !L4
In[33]:=

êê N
H2 n + 1L HH2 nL !L3

Out[34]= 8.89959 × 10−76

accuracy. Let's see what we get if we try to estimate Ÿ0 sin x „ x using the 20-point Gauss-Legendre method and 16-digit
The theoretical error is much smaller than the error due to round-off. Using floating point arithmetic limits us to 16-digit
p

floating point arithmetic:

gaussLegendre@f, 0, Pi, gc24D êê InputForm


In[35]:= f@x_D := Sin@xD

Out[36]//InputForm=
1.9999999999999933

Round-offs gave a small error, limiting us to 15-digit accuracy.

Potrebbero piacerti anche