Answer to Question #162577 in Python for Rukiyah

Question #162577

Modify the Monte Carlo simulation to plot points in the entire circle. You will have to adjust the calculated value of pi accordingly.



1
Expert's answer
2021-02-10T13:19:15-0500


import random as rnd
from matplotlib import pyplot as plt


n = 2500 # number of iterations


ins_number = 0
inside_x = []
inside_y = []
outside_x = []
outside_y = []


for i in range(n):
    x = rnd.uniform(-1,1)
    y = rnd.uniform(-1,1)
    if pow(x, 2) + pow(y, 2) <= 1:
        inside_x += [x]
        inside_y += [y]
        ins_number += 1
    else:
        outside_x += [x]
        outside_y += [y]


pi = 4*ins_number/n
print(pi)


fig = plt.figure() 
plt.plot(inside_x, inside_y, "bo")   # blue o inside circle
plt.plot(outside_x, outside_y, "ro") # red o outside of circle
plt.savefig("plot.png") 

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS