Numerical Integration Assignment
Due Friday, April 20, 2012 by 5pm
The file Integrate.java contains a
program to do some numerical integration. Currently the program
implements the composite Trapezoid rule and Simpson's rule
and has built in the following 7 functions:
- f(x) = 4 / (1 + x2)
- f(x) = Math.sqrt(x) * Math.log(x)
- f(x) = cos(x)
- f(x) = 1 / (1 + 100x2)
- f(x) = Math.sqrt( |x| )
- f(x) = (100/x2)sin(10/x)
- f(x) = x16cos(x16)
The correct
values for each of the 7 integrals are also built in (for the specific
limits of integration that correspond to most of the problems) so that
actual errors in the approximations can be studied.
Copy the file
to your directory then do the following:
- Implement the composite Midpoint Rule. The stub for the method is
already in the program.
Replace the "return 0" statement with the code.
- Use the program to estimate PI using the first function (the
integral of that function from 0 to 1 is PI) using
the three quadrature rules (Midpoint, Trapezoid, and Simpson) as follows.
All will be computed when you choose option I in the program. You must
first select the function. Use 0 and 1 as the endpoints of the intervals.
For each
use a variety of values of
n (and hence h).
-
Examine the error for each approximation and
see how well it matches the theoretical error.
- For each, find a value of n
beyond which there is no further improvement in the accuracy of the
answer.
Get output from the program to
support your answers (don't waste paper -- decide what output shows
what you want then print just that). (Be sure to actually write some
answers -- I don't want just output with no analysis!)
- In class, we derived a way of using the midpoint
rule and the trapezoid rule to estimate the error in the midpoint
rule (and hence also in trapezoid since the two are related). Add
code to the integrate method to compute and print this error estimate.
(Print it after the table showing the results of the three composite
methods.)
- Use the program to evaluate the integrals of functions 2 (using
limits of integration of 0 and 1) and 3 - 5
(using limits of integration of -1 and 1) using
several values of n (to get different sizes of interval subdivision).
Answer the following questions about error and accuracy:
- Examine the error for each approximation. Compare it
to both the theoretical and, in the case of the midpoint rule,
to the approximation.
- Explain any unexpected results (anything that doesn't appear
to be quite right even if you expected it!)
- For each find a value of n beyond which there is no further
improvement.
- Use the Adaptive Simpson method on function 5 (the square root
of the absolute value) from -1 to 1. Use a tolerance of 0.0001.
- On the graph of the function (Mathematica printout) mark the
subdivisions used by the method on the x-axis.
- Explain why the subdivisions appear as they do.
- Verify that using composite Simpson you would need at least
n = 1000 to get results as good.
- Use the Monte Carlo method to evaluate the integral of
function #1 (and thus estimate PI). Try different numbers n of
sample points. Record your results - number of sample points
and the approximate integral.
Try to determine the relationship between the number of points and the
error. Also compare the amount of work for this method
to the others.
- Use all methods (including Romberg) on function #7 from 0.8 to 1.3 (the
part of the function in the graph from Mathematica). The
correct answer is -0.0510397. Compare the methods both in
terms of accuracy and number of calculations. (Note: the "correct"
answer built into the program is for different a and b - you can
change it to -0.0510397 so the error is calculated for you
for the composite methods). For Romberg, look both down and across
in the table generated to see when/where the solution gets best.
Hand in: A printout of your final program and your answers to
the questions. Email a copy of the program to ingram@roanoke.edu.