def monte_carlo_pi(N=1e+4): \"\"\" generate N # of random points (x,y), where x
ID: 3601062 • Letter: D
Question
def monte_carlo_pi(N=1e+4):
""" generate N # of random points (x,y), where x and y are uniformly distributed on [-1,1],
count the n # of points that are inside the disk center at (0,0) with radius 1.
the probability of # points landing inside this disk is n/N, which is approximately pi/4,
thus one can estimate the value pi by computing n/N then multiply by 4.
return this approximate value of pi.
"""
if N<=1500: N=1500 #not allowing too small N
#add code below to get an estime of pi and return this value
def simulate():
Nmin=8000; Nmax=60000; Nstep=2000
Nrange = range(Nmin, Nmax, Nstep)
#creat an empty list to store the simulated pi value
pie = []
#add code to call the above function for a given N (this N should loop N in Nrange),
#store the estimated value of Pi for each N in the list pie
#add code below to plot the simulated values of pi
#add code to draw a line with the true value of pi (using np.pi)
#add code to draw a linear of the average of the sumulated pi
#save the plot in a file for submission
plt.savefig("pi_MC_{}_{}_{}.png".format(Nmin,Nmax,Nstep))
plt.show()
Not sure how to get started on this problem
Prob. 5 This problem asks you to apply a simple Monte Carlo method to approximate the value of . The idea behind this Monte Carlo method for approximating the value of is fairly simple: A unit disk centered at (0.0) has area , we can inscribe this disk in a square centered at (0,0) with edge length 2. We generate a relatively large number of points with coordinates (, y), where x and y are random numbers uniformly distributed on [-1,1). (Note the bounds [-1, 1) means all the random points generated are located inside the square that encloses the unit disk. You need to check up the uniformC) function from numpy.random, because .rand() is uniform on [0,1) but not on [-1, 1), thus .rand) cannot be directly used here, unless you use a different model.) We can count the number of points that are located inside the circle (i.e., r2 +y2Explanation / Answer
Please add the code to generate graph plot.
#!/usr/bin/env python
import math
import numpy as np
def monte_carlo_pi(N=1e+4):
""" generate N # of random points (x,y), where x and y are uniformly distributed on [-1,1],
count the n # of points that are inside the disk center at (0,0) with radius 1.
the probability of # points landing inside this disk is n/N, which is approximately pi/4,
thus one can estimate the value pi by computing n/N then multiply by 4.
return this approximate value of pi.
"""
if N<=1500: N=1500 #not allowing too small N
#add code below to get an estime of pi and return this value
n=0
for i in range(N):
x = np.random.uniform(-1,1)
y = np.random.uniform(-1,1)
area = pow(x,2) + pow(y,2)
if area <=1:
n = n + 1
prob = n/N
pi = prob*4
return pi
def simulate():
Nmin=8000; Nmax=600000; Nstep=2000
Nrange = range(Nmin, Nmax, Nstep)
#creat an empty list to store the simulated pi value
pie = []
#add code to call the above function for a given N (this N should loop N in Nrange),
for N in Nrange:
pie_val = monte_carlo_pi(N)
pie.append(pie_val)
#store the estimated value of Pi for each N in the list pie
print pie
simulate()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.