Statistical Physics: Determine an approximate value of by the following method,
ID: 3161557 • Letter: S
Question
Statistical Physics:
Determine an approximate value of by the following method, called Monte Carlo. Generate by a computer pairs of numbers. {x, y}. each uniformly distributed between -1/2 and 1/2: equivalently. this means you generate random points in a unit square. Count the fraction of those which happen to be inside the circle inscribed in this square. This should allow you to estimate pi. How accurate is your result? As you should have learned from other examples, the quality of a computational method is determined by how rapidly the result improves with the increasing computational load. Try performing your Monte Carlo determination of pi with varying number of Monte Carlo trials, n, and look how Delta_n = | pi_estimated - pi |/pi depends on n.Explanation / Answer
You can try the following it works well. For part 2 try changing the trials
Chegg answer
#!/usr/bin/env python
from random import random
from math import hypot
from PIL import Image
from PIL import ImageDraw
TRIALS=10**6
XMAX=500
YMAX=500
def main():
inside = 0
img = Image.new('RGB', (XMAX, YMAX), "white")
draw = ImageDraw.Draw(img)
for i in xrange(TRIALS):
x = random()
y = random()
if hypot(x, y) < 1:
inside +=1
draw.point((int(x*XMAX), int(y*YMAX)), fill="red")
img.save('pi.png')
print 'pi is: ', 4.0 * inside / TRIALS
if __name__ == '__main__':
main()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.