CSC/MAT/PHY 340 - Assignment 4
Numerical Integration

DUE: Thursday, 13 November 2003, 5:00 p.m.

  1. 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:
    	f1(x) = sin(x),  on [0, Pi]
    	f2(x) = 2 exp(x)/[exp(5) - exp(1)],  on [1, 5]
    	f3(x) = 7 x5/2,  on [0, 1]
    	f4(x) = 3 x1/2,  on [0, 1]
    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 .

  2. I have written a Java class, located in file, 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.

    	f5(x) = .01/(2x2 - 23/2x + 1.001),  on [0, 1]
    	f6(x) = .01 x9,  on [1, 2]
    	f7(x) = 1 - cos(32x),  on [0, 2Pi]
    Use Maple to calculate (to 16 significant digits) the definite integral above involving f5(x) (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 f5(x):
       .01[arctan[(21/2-1)/C1] + arctan(1/C1)]/C2, where C1 = (.001)1/2 and C2 = (.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
Document last modified on