Question: Compose a function mc_pi( n ) to estimate the value of using the Buffon's Needle method. n describes the number of points to be used

Compose a function mc_pi( n ) to estimate the value of using the Buffon's Needle method. n describes the number of points to be used in the simulation. mc_pishould return its estimate of the value of as a float. Your process should look like the following:

1. Prepare an array of coordinate pairs xy. This should be of shape ( n,2 ) selected from an appropriate distribution (see notes 1 and 2 below).

2. Calculate the number of coordinate pairs inside the circle's radius. (How would you do this mathematically? Can you do this in NumPy without a loop?although a loop is okay.)

3. Calculate the ratio ncircle/nsquare = Acircle/Asquare, which implies (following the development above), 4 ( ncircle/ nsquare )

4. Return this estimate of .

5. You may find it edifying to try the following values of n, and compare each result to the value of math.pi: 10, 100, 1000, 1e4, 1e5, 1e6, 1e7, 1e8. How does the computational time vary? How about the accuracy of the estimate of ?

You will need to consider the following notes:

1. Which kind of distribution is most appropriate for randomly sampling the entire area? (Hint: if we could aim, it would be the normal distributionbut we shouldn't aim in this problem!)

2. Since numpy.random distributions accept sizes as arguments, you could use something like np.random.distribution( n,2 ) to generate coordinate pairs (in the range [0,1)[0,1) which you'll then need to transform)but use the right distribution! Given a distribution from [0,1)[0,1), how would you transform it to encompass the range [1,1)[1,1)? (You can do this to the entire array at once since addition and multiplication are vectorized operations.)

Your submission should include a function mc_pi( n ).

(This assignment is based on Langtangen, Exercise 8.29.)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!