Answer in Python please:
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_pi should return its estimate of the value of π as a float. Your process should look like the following:
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).
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.)
Calculate the ratio ncirclensquare=AcircleAsquare, which implies (following the development above), π≈4ncirclensquare.
Return this estimate of π.
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:
Which kind of distribution is most appropriate for randomly sampling the entire area? (Hint: if we could aim, it would be the normal distribution—but we shouldn't aim in this problem!)
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) which you'll then need to transform)—but use the right distribution! Given a distribution from [0,1), how would you transform it to encompass the range [−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 ).
`Hey,
Note: If you have any queries related the answer please do comment. I would be very happy to resolve all your queries.
Note: Brother sometimes while uploading on HomeworkLib the indentations change. So, I request you to verify it with screenshot once. This is the link where I have saved the code too
https://trinket.io/python3/e0a2ee1763
import numpy as np
def sample_cosine():
rr=2.
while rr > 1.:
u1=np.random.uniform(0,1.)
u2=np.random.uniform(0,1.)
v1=2*u1-1.
rr=v1*v1+u2*u2
cc=(v1*v1-u2*u2)/rr
return cc
class mc_pi:
def __init__(self,x,y,n,m):
self.x = float(x) #width of the needle
self.y = float(y) #witdh of the space
self.r = [] #coordinated of the centre of the needle
self.z = [] #measure of the alignment of the needle
self.n = n #no of throws
self.m = m #no of simulations
self.p = self.x/self.y
self.pi_approx = []
def samples(self):
# throwing the needles
for i in range(self.n):
self.r.append(np.random.uniform(0,self.y))
C=sample_cosine()
self.z.append(C*self.x/2.)
return [self.r,self.z]
def simulation(self):
# m simulation
for j in range(self.m):
self.r=[]
self.z=[]
self.samples()
# n throw
hits = 0 #setting the success to 0
for i in range(self.n):
#condition for a hit
if self.r[i]+self.z[i]>=self.y or
self.r[i]-self.z[i]<0.:
hits += 1
else:
continue
est =self.p*float(self.n)/float(hits)
self.pi_approx.append(est)
return self.pi_approx
y = mc_pi(1,2,80000,5)
print (y.simulation())
Note: Brother According to Chegg's policy we are only allowed to answer first part if there are many. So, I request you to post other part as separate posts
Kindly revert for any queries
Thanks.
Answer in Python please: Compose a function mc_pi( n ) to estimate the value of π...
Please write python code and please answer all parts and separate them into their parts, thank you. Given the sequence: with the initial condition Xo chosen in the interval [0,1], and "a" is a given parameter between 0 and4 This sequence is a simple model to describe how a population (or civilization) evolves in a closed system with a finite amount of resources. The parameter "a" describes the rate of development and expansion of the civilization. The population is described...