1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
AlexFokin [52]
3 years ago
12

The Monte Carlo method is commonly used to approximate areas or volumes of shapes. In this problem, we will use Monte Carlo to f

ind 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.)
Computers and Technology
1 answer:
sdas [7]3 years ago
4 0

Answer:

import numpy as np

import matplotlib.pyplot as plt

def calculate_pi(x,y):

   points_in_circle=0

   for i in range(len(x)):

       if np.sqrt(x[i]**2+y[i]**2)<=1:

           points_in_circle+=1

       pi_value=4*points_in_circle/len(x)

       return pi_value

length=np.power(10,6)

x=np.random.rand(length)

y=np.random.rand(length)

pi=np.zeros(7)

sample_size=np.zeros(7)

for i in range(len(pi)):

   xs=x[:np.power(10,i)]

   ys=y[:np.power(10,i)]

   sample_size[i]=len(xs)

   pi_value=calculate_pi(xs,ys)

   pi[i]=pi_value

 

print("The value of pi at different sample size is")

print(pi)

plt.plot(sample_size,np.abs(pi-np.pi))

plt.xscale('log')

plt.yscale('log')

plt.xlabel('sample size')

plt.ylabel('absolute error')

plt.title('Error Vs Sample Size')

plt.show()

Explanation:

The python program gets the sample size of circles and the areas and returns a plot of one against the other as a line plot. The numpy package is used to mathematically create the circle samples as a series of random numbers while matplotlib's pyplot is used to plot for the visual statistics of the features of the samples.

You might be interested in
You would like to put the data in order by product number. What should you do?
Dafna11 [192]

If someone wants to put data in order (ascending or descending) they should sort the data.

c. Sort

<u>Explanation:</u>

Sorting is the process of arranging the data on the basis of some property or attribute. Using the find command, conducting a query, and mapping the data would not help.

There are various sorting algorithms like the bubble sort, selection sort, insertion sort, quick sort, and merge sort. Out of all the attributes, one or a combination of attributes can be selected to serve as the basis for sorting.

8 0
3 years ago
................. are used to summarize data (option) (a) report (b) action ​
Tems11 [23]
I think it A I feel like report is right for the answer
5 0
3 years ago
Read 2 more answers
Which of the following is true about main content? Select all that apply.
lions [1.4K]

Answer:

TRUE - Main Content should be created with time, effort, and expertise, and should not be copied from another source.

True -High quality Main Content (MC) allows the page to achieve its purpose well.

Explanation:

Web design and development is the creation of web pages.web pages can be static or links to multiple pages. A static web page has only one page and a site address. Multiple web pages, are series of related web pages linked together with a link tag line of code.

In a web page, a main content is the main information needed to be passed across by the programmer or blogger, it varies from page to page in a multiple web site. It must be unique and contain the main information of the web page.

7 0
3 years ago
Read 2 more answers
Suppose you are using a Mac to read your e-mail messages, and you receive an
Gekata [30.6K]

Answer:

D

Explanation:

4 0
3 years ago
Read 2 more answers
What may happen if a large number of computer users are attempting to access a web site at the same time that you are?
Andrews [41]
D (you may be unable to link to the site). The site cannot handle the large load of requests and are queued therefore, the response will be slowed down immensely. <span> </span>
5 0
3 years ago
Other questions:
  • you were discussing software privacy with a friend were surprised to learn how software privacy can impact your life everyday. w
    10·1 answer
  • Which of these computers was marketed as a computer/game machine?
    15·2 answers
  • 1. Access and PowerPoint are not included in all configurations of Microsoft Office 2013.
    14·1 answer
  • For a wire with a circular cross section and a diameter = 3.00mm calculate the following: (m means meter. mm means millimeter. c
    5·1 answer
  • In 1-2 sentences, describe how to change the font.
    8·2 answers
  • What connectionless protocols are implemented at the lower layers of the OSI model, what is usually used to acknowledge that the
    5·2 answers
  • Write a procedure ConvertToBinary that takes an input as a number from 0 to 16 (including 0 but not 16) and converts it to a bin
    11·1 answer
  • What is it called when you remove some information from a file or remove a file from the disk ? A) save b) delete c) edit d) rem
    5·1 answer
  • It's generally best to use what types of sites for factual internet research? Select all that apply from the choices below.
    5·2 answers
  • When trying to upload assignment on canvas wont let me click choose file.
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!