Computers and Technology

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

answer
Answers: 3

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 20:00, bartlettcs9817
Which location-sharing service offers items for users as a gaming component and also allows them to collectively link their check-ins to publish a trip? a. whrrl b. buzzd c. foursquare (this option is wrong i already tried) d. gowalla for plato
Answers: 2
image
Computers and Technology, 23.06.2019 19:00, brittneyrenae7338
This question involves a class named textfile that represents a text file. public class textfile { private string filename; private string filename; private arraylist words; // constructors not shown // postcondition: returns the number of bytes in this file public int filesize() { } // precondition: 0 < = index < words. size() // postcondition: removes numwords words from the words arraylist beginning at // index. public void deletewords(int index, int numwords) { } // precondition: 0 < = index < = words. size() // postcondition: adds elements from newwords array to words arraylist beginning // at index. pub lic voidaddwords(int index, string[] newwords) { } // other methods not shown } complete the filesize() method. the filesize() is computed in bytes. in a text file, each character in each word counts as one byte. in addition, there is a space in between each word in the words arraylist, and each of those spaces also counts as one byte. for example, suppose the words arraylist stores the following words: { mary had a little lamb; its fleece was white as snow. } the filesize() method would compute 4 + 3 + 1 + 6 + 5 + 4 + 6 + 3 + 5 + 2 + 5 as the sum of the lengths of each string in the arraylist. the value returned would be this sum plus 10, because there would also be 10 spaces in between the 11 words. complete the filesize() method below: // postcondition: returns the number of bytes in this file public int filesize() { }
Answers: 1
image
Computers and Technology, 25.06.2019 05:00, Kadyn5
Carmina works at a fast-food restaurant. during the slow afternoon hours, carmina always find projects to keep her busy, like washing all the trays or deep-cleaning the drive-thru area. what workplace habit does carmina show by doing this? efficiency initiative interpersonal skills problem solving
Answers: 1
image
Computers and Technology, 25.06.2019 08:10, shawnball8571
In contrast to data in a database data in a data warehouse is described as subject oriented
Answers: 2
Do you know the correct answer?
The Monte Carlo method is commonly used to approximate areas or volumes of shapes. In this problem,...

Questions in other subjects:

Konu
Chemistry, 28.09.2019 04:30
Konu
Health, 28.09.2019 04:30