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
The inability of BAE Automated Systems to create an automated baggage handling system led to a significant delay in the opening
Sergeu [11.5K]

Answer:

True

Explanation:

Airport was truly opened outside the city of Denver, because of the faults in BAE automated system.

6 0
3 years ago
The destructor automatically executes when the class object goes out of ____.
Tema [17]

Answer:

scope    

Explanation:

Destructor is a member function and it call automatically when the class object goes out of scope.

Out of scope means, the program exit, function end etc.

Destructor name must be same as class name and it has no return type.

syntax:

~class_name() { };

For example:

class xyz{

  xyz(){

        print(constructor);

   }

~xyz(){

        print(destructor);

   }

}

int main(){

    xyz num;

   

}//end program

when the object is create the constructor will called and when the program end destructor will call automatically.

4 0
2 years ago
Question 2 (2 points)
swat32

I beleive all of the above for question one

4 0
2 years ago
Describe how computer are used in ticket counter?​
m_a_m_a [10]

Answer:

These systems are commonly used in facilities such as public libraries to ensure equitable use of limited numbers of computers. Bookings may be done over the internet or within the library itself using a separate computer set up as a booking terminal.

6 0
2 years ago
Lime is using social media to ask consumers about the qualities they desire in an electric scooter. The firm hopes to develop a
tresset_1 [31]

Answer:

The correct answer to the following question will be "Product".

Explanation:

  • Product marketing is often a mechanism by which a brand is marketed and delivered to a client. It's also known as the intermediate feature between product innovation and brand recognition or awareness.
  • It's something to which comparison is made when selling a product on the market to the consumer.

Therefore, the Product is the right answer for the above question.

8 0
3 years ago
Other questions:
  • Which of the following is the rarest and most valuable mineral ore among the native elements?
    13·1 answer
  • An archive of files that usually contain scripts that install the software contents to the correct location on the system is ref
    11·1 answer
  • Does YouTube have the potential to change academia as we know it, or is that a bit of a stretch? Explain.
    9·2 answers
  • What was bill gates first operating system he created?
    7·1 answer
  • A _____ is a harmful program that resides in the active memory of a computer and duplicates itself. Select one: a. scareware b.
    7·1 answer
  • All of the following are examples of being computer literate, EXCEPT ________. Group of answer choices knowing how to build and
    7·1 answer
  • Write a Java program that creates a two-dimensional array of type integer of size x by y (x and y must be entered by the user).
    7·1 answer
  • "Random Bars" is considered to be a Transition style<br> O True<br> O False
    11·1 answer
  • Discuss five processes for analyzing a qualitative study
    11·2 answers
  • Write the line of code to calculate the area of a circle with radius 3 and store it in a variable called
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!