subject

The Monte Carlo method is commonly used to approximate areas or volumes of shapes. In this problem, we will use Monte Carlo to find the area of a circle, allowing us to estimate the value of Ï. Furthermore, we will use several different sample sizes to see how the accuracy increases with larger samples. PYTHON Part 1

According to the formula A=Ïr^2, for a circle of radius 1, the area of the circle will be equal to Ï. Therefore, if we can use Monte Carlo to approximate the area of the circle, we can find an estimate for Ï.

Write a function calculate_pi that accepts a sample of x and corresponding y coordinates as arguments and returns an estimate for Ï (since we want to calculate Ï for various sample sizes, it will be useful for it to be a function). To simplify things, we will only consider a single quadrant of the unit circle (see gif above). Therefore, the area of the full circle will be 4 times the area we calculate for the single quadrant (and your function should take this into account).

Part 2

Using the calculate_pi function you wrote in Part 1, estimate the value of ÏÏ for different sample size of powers of 10 ranging from 10^0 to 10^6 (inclusive). Store these values in an array of size 7 named pi. Use the first portion of the coordinates for this purpose. So, if you needed 10 samples, you would do: xs[:10] to get the first 10 samples in xs

Part 3

Plot the absolute error of your approximations vs. the sample size on a log-log plot. The error can be obtained by subtracting the true value of ÏÏ from your approximations.

You should notice that the error decreases with larger sample sizes, although there may be a considerable amount of noise due to randomization. The order of convergence of Monte Carlo is O(1ân), so the slope of your plot should be around â1/2.

Output

The setup code gives the following variables:

Name Type Description
xs 1-d numpy array random numbers from 0 to 1
ys 1-d numpy array random numbers from 0 to 1
Your code snippet should define the following function(s) and variable(s):

Name Type Description
calculate_pi function function accepting 2 arrays (x and y sample coordinates, respectively) and returning an estimate for ÏÏ
pi 1-d numpy array estimates for ÏÏ as described above
We also ask for a plot of error vs. sample size. (Provide appropriate axes labels and a title.)

ansver
Answers: 3

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 17:00
The two main ways in which marketers address the competition with their strategies are by satisfying a need better than a competition and by
Answers: 2
question
Computers and Technology, 23.06.2019 04:00
Write a method that takes in an array of point2d objects, and then analyzes the dataset to find points that are close together. be sure to review the point2d api. in your method, if the distance between any pair of points is less than 10, display the distance and the (x,y)s of each point. for example, "the distance between (3,5) and (8,9) is 6.40312." the complete api for the point2d adt may be viewed at ~pf/sedgewick-wayne/algs4/documentation/point2d.html (links to an external site.)links to an external site.. try to write your program directly from the api - do not review the adt's source code.
Answers: 1
question
Computers and Technology, 23.06.2019 18:50
Ais a picture icon that is a direct link to a file or folder
Answers: 1
question
Computers and Technology, 23.06.2019 23:30
Match the following errors with their definitions. a. #name b. #value c. #ref d. 1. when a formula produces output that is too lengthy to fit in the spreadsheet cell 2. when you enter an invalid cell reference in a formula 3. when you type text in cells that accept numeric data 4. when you type in a cell reference that doesn’t exist
Answers: 1
You know the right answer?
The Monte Carlo method is commonly used to approximate areas or volumes of shapes. In this problem,...
Questions
question
Mathematics, 05.07.2019 23:30
question
Mathematics, 05.07.2019 23:30
question
Mathematics, 05.07.2019 23:30
Questions on the website: 13722363