Numerical Integration

- Write Java methods named
`mid`

,`trap`

, and`simp`

that return`double`

approximations to a definite integral computed using the composite midpoint, trapezoid, and Simpson's rules, respectively, using a regular partition. Each method should use parameters`func`

(a*Function*object representing the integrand),`a`

,`b`

(the endpoints of the integration interval), and`n`

(the number of subintervals to be used).

NOTE: Your`simp`

method should have a one-line body that combines the values returned by`mid`

and`trap`

.

Write a program with a`main`

method that uses`mid`

,`trap`

, and`simp`

to compute an integral and then outputs the following quantities for each quadrature rule:`n`

, the approximation to the integral provided by the rule, the exact error in the approximation, and the ratio of the exact error in the approximation using`n/2`

subintervals to that using`n`

subintervals. These quantities should be output for`n = 1, 2, 4, 8, ...`

EXCEPT that the ratio cannot be output for`n=1`

(since there is no previous result with which to form a ratio). Your program should continue doubling`n`

and applying a rule to an integral until the absolute value of the exact error found when applying the rule is less than`1.0e-8`

. For a given integral, all results for the midpoint rule should be output first, then all results for the trapezoid rule, and finally all results for Simpson's rule (i.e. thus, each integral produces 3*paragraphs*of output, one for each rule). Run your program (four separate times if you like) using the four integrand-interval pairs below:f

_{1}(x) = sin(x), on [0, Pi]f

_{2}(x) = 2 exp(x)/[exp(5) - exp(1)], on [1, 5]f

_{3}(x) = 7 x^{5/2}, on [0, 1]f

Briefly discuss your findings, especially the exact error ratio and what may have caused it to deviate from the expected ratio. Please ensure that you submit a hard copy of all output (whether or not your program was run separately for each integral) and that your output is neatly formatted ._{4}(x) = 3 x^{1/2}, on [0, 1]

- I have written a Java class, located in file TestQuad.java, that includes a Java method for Romberg quadrature (in method
`qRomb`

) as well as a`main`

method that calls`qRomb`

. First, modify`qRomb`

so that it counts the*number of function evaluations*performed in computing an integral. Next, add a Java method with the header below to the`TestQuad`

class:public static double adaptSimp(Function func, double a, double b, double tol)

More will be said about this method in class, but basically it should call a recursive method (name it`adSimpRec`

) that adaptively applies Simpson's rule to an integral to obtain an approximation that (we hope) has an absolute error less than`tol`

. Your code should be written so that no unnecessary function evaluations are done, and it should count the number of function evaluations performed in computing an integral.

Finally, modify`TestQuad`

further so that it applies the`qRomb`

and`adaptSimp`

methods, in an attempt to compute an integral within a tolerance of`1.0e-12`

, to each of the four integrand-interval pairs given above as well as the three pairs given below. Your program should output, for each method, the integral approximation returned by the method, the exact absolute error in the approximation, and the number of function evaluations required. Then, use these results to make (possibly by hand or with a word processor) a table indicating the absolute error and the number of function evaluations for each method applied to each integrand-interval pair. Based on all your results, compare the reliability and efficiency of the`qRomb`

and`adaptSimp`

methods. Also, try to explain any cases where one method or the other performs badly or is unusually successful.

f

_{5}(x) = .01/(2x^{2}- 2^{3/2}x + 1.001), on [0, 1]f

_{6}(x) = .01 x^{9}, on [1, 2]f

Use_{7}(x) = 1 - cos(32x), on [0, 2Pi]*Maple*to calculate (to 16 significant digits) the definite integral above involving`f`

(so that you can calculate exact absolute errors for it). Alternatively, the following formula can be used in your program to calculate the exact value of the integral involving_{5}(x)`f`

:_{5}(x).01[arctan[(2

^{1/2}-1)/C_{1}] + arctan(1/C_{1})]/C_{2}, where C_{1}= (.001)^{1/2}and C_{2}= (.002)^{1/2}.

On or before 5:00 p.m. on the due date, hand in hard copies of all code, all output, and all written work. Also, by the same deadline, send me an e-mail with all your Java files attached to it.

Copyright © 2003 William W. Hackborn