Tải bản đầy đủ (.pdf) (10 trang)

Tài liệu Thuật toán Algorithms (Phần 10) pdf

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (71.97 KB, 10 trang )

INTEGRATION
83
(Recall that the area of a trapezoid is one-half the product of the height and
the sum of the lengths of the two bases.) The error for this method can be
derived in a similar way as for the rectangle method. It turns out that
f(x) dx = t



+ . . . .
Thus the rectangle method is twice as accurate as the trapezoid method.
This is borne out by our example. The following procedure implements the
trapezoid method in the common case where all the intervals are the same
width:
function b: real; N: integer): real;
i: integer; w, t: real;
begin

for to N do
end ;
This procedure produces the following estimates for
10 0.6937714031754
100 0.6931534304818
1000 0.6931472430599
It may seem surprising at first that the rectangle method is more accurate
than the trapezoid method: the rectangles tend to fall partly under the curve,
partly over (so that the error can cancel out within an interval), while the
trapezoids tend to fall either completely under or completely over the curve.
Another perfectly reasonable method is spline quadrature: spline inter-
polation is performed using methods we have discussed and then the integral
is computed by piecewise application of the trivial symbolic polynomial in-


tegration technique described above. we’ll see how this relates to the
other methods.
Compound Methods
Examination of the formulas given above for the error of the rectangle and
trapezoid methods leads to a simple method with much greater accuracy,
called Simpson’s method. The idea is to eliminate the leading term in the error
84 7
by combining the two methods. Multiplying the formula for the rectangle
method by 2, adding the formula for the trapezoid method then dividing by
3 gives the equation
The term has disappeared, so this formula tells us that we can get a method
that is accurate to within by combining the quadrature formulas in the
same way:
If an interval size of is used for Simpson’s rule, then the integral can
be computed to about ten-place accuracy. Again, this is borne out in our
example. The implementation of Simpson’s method is only slightly more
complicated than the others (again, we consider the case where the intervals
are the same width):
function b: real; N: integer): real;
var i: integer; w, s: real;
begin

for to Ndo
end ;
This program requires three “function evaluations” (rather than two) in the
inner loop, but it produces far more accurate results than do the previous two
methods.
10 0.6931473746651
100 0.6931471805795
1000 0.6931471805599

More complicated quadrature methods have been devised which gain
accuracy by combining simpler methods with similar errors. The most
known is Romberg integration, which uses two different sets of subintervals
for its two “methods.”
INTEGRATION
85
It turns out that Simpson’s method is exactly equivalent to interpolating
the data to a piecewise quadratic function, then integrating. It is interesting
to note that the four methods we have discussed all can be cast as piecewise
interpolation methods: the rectangle rule interpolates to a constant (degree-O
polynomial); the trapezoid rule to a line (degree-l polynomial); Simpson’s rule
to a quadratic polynomial; and spline to a cubic polynomial.
Adaptive Quadrature
A major flaw in the methods that we have discussed so far is that the errors
involved depend not, only upon the subinterval size used, but also upon the
value of the high-order derivatives of the function being integrated. This
implies that the methods will not work well at all for certain functions (those
with large high-order derivatives). But few functions have large high-order
derivatives everywhere. It is reasonable to use small intervals where the
derivatives are large and large intervals where the derivatives are small. A
method which does this in a systematic way is called an adaptive quadrature
routine.
The general approach in adaptive quadrature is to use two different
quadrature methods for each subinterval, compare the results, and subdivide
the interval further if the difference is too great. Of course some care should
be exercised, since if two equally bad methods are used, they might agree quite
closely on a bad result. One way to avoid this is to ensure that one method
always overestimates the result and that the other always underestimates the
result,. Another way to avoid this is to ensure that one method is more accurate
than the other. A method of this type is described next.

There is significant overhead involved in recursively subdividing the in-
terval, so it pays to use a good method fo:r estimating the integrals, as in the
following implementation:
function adapt (a, b: real) : real;
begin
if b, b,
then b, 10)
else + b);
end;
Both estimates for the integral are derived from Simpson’s method, one
using twice as many subdivisions as the other. Essentially, this amounts to
checking the accuracy of Simpson’s method over the interval in question and
then subdividing if it is not good enough.
86
CHAPTER 7
Unlike our other methods, where we decide how much work we want
to do and then take whatever accuracy results, in adaptive quadrature we do
however much work is necessary to achieve a degree of accuracy that we decide
upon ahead of time. This means that tolerance must be chosen carefully,
so that the routine doesn’t loop indefinitely to achieve an impossibly high
tolerance. The number of steps required depends very much on the nature of
the function being integrated. A function which fluctuates wildly will require
a large number of steps, but such a function would lead to a very inaccurate
answer for the “fixed interval” methods. A smooth function such as our
example can be handled with a reasonable number of steps. The following
table gives, for various values of the value produced and the number of
recursive calls required by the above routine to compute
0.00001000000 0.6931473746651 1
0.00000010000 0.6931471829695 5
0.00000000100 0.6931471806413 13

0.00000000001 0.6931471805623 33
The above program can be improved in several ways. First, there’s
certainly no need to call intsimp(a, twice. In fact, the function values
for this call can be shared by intsimp(a, b, 5). Second, the tolerance bound
can be related to the accuracy of the answer more closely if the tolerance is
scaled by the ratio of the size of the current interval to the size of the full
interval. Also, a better routine can obviously be developed by using an even
better quadrature rule than Simpson’s (but it is a basic law of recursion that
another adaptive routine wouldn’t be a good idea). A sophisticated adaptive
quadrature routine can provide very accurate results for problems which can’t
be handled any other way, but careful attention must be paid to the types of
functions to be processed.
We will be seeing several algorithms that have the same recursive struc-
ture as the adaptive quadrature method given above. The general technique
of adapting simple methods to work hard only on difficult parts of complex
problems can be a powerful one in algorithm design.
rl
INTEGRATION
87
Exercises
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.

Write a program to symbolically integrate (and differentiate) polynomials
in x and lnx. Use a recursive implementation based on integration by
parts.
Which quadrature method is likely to produce the best answer for in-
tegrating the following functions: f(s) = f(x) = (3 x)(4 + f(s) =
sin(x)?
Give the result of using each of the four elementary quadrature methods
(rectangle, trapezoid, Simpson’s, spline) to integrate y = l/x in the inter-
val
Answer the previous question for the function y = sinx.
Discuss what happens if adaptive quadrature is used to integrate the
function y = l/x in the interval
Answer the previous question for the elementary quadrature methods.
Give the points of evaluation when adaptive quadrature is used to in-
tegrate the function y = l/s in the interval with a tolerance of
Compare the accuracy of an adaptive quadrature based on Simpson’s
method to an adaptive quadrature on the rectangle method for the
integral given in the previous problent.
Answer the previous question for the function y = sinx.
Give a specific example of a function for which adaptive quadrature would
be likely to give a drastically more accurate result than the other methods.

×