CS 1401 Spring 2013 Lab 6 Due Mon/Tue April 8/9 at beginning of

advertisement
CS 1401 Spring 2013 Lab 6
Due Mon/Tue April 8/9 at beginning of lab (no extensions!)
Monte Carlo Simulation of Definite Integrals
Learning outcomes: After completing this lab, students will be able to:
o Define and invoke methods with parameters and return values
o Design and implement a modular program
o Use Monte Carlo simulation to solve a calculus problem
A definite integral

1
0

b
a
f (x) dx is the area under the curve for f(x) from a to b. For example,
(1  x 2 ) is the area under the curve shown below.


For this lab, you will write a modular program that contains a method for computing the
value of a real non-negative integrable function f(x) at a point x, as well as a Boolean
method isHit() for determining if a randomly generated (x, y) point falls under the curve for
f(x). You may assume that the (x, y) values of the function being considered are within a
rectangle with height h and width b – a. You should also write a main program that
estimates the value of the integral using numbers of points 104, 105, 106, and 107 and
compares the estimates with the actual value. The estimate of the integral will be
Area * nHits/nTrials, where Area is the area of the rectangle. You should test your program
with function f(x) = x3 on the interval [0.0,2.0] with h = 8.0, and with function f(x) =
(1  x 2 ) on the interval [0.0,1.0] with h = 1.0 and turn in both versions. If you do not know

how to compute the definite integrals of these functions analytically, you may use Wolfram
Alpha to compute them. Your program should still work correctly if we substitute a
different function definition and a different [a,b] and h when grading it. You should define a,
b, h, and the exact answer as constants at the top of your main program.
Lab 6 will be graded as follows:
o Correct output for the integrals given in the assignment (10 points)
o Correct output for an integral chosen by the grader (5 points)
o Comments that explain the use of variables and the algorithmic steps (3 points)
o Good programming style (2 points)
Download