Plot the points inside and outside the circle: fig, ax = plt.subplots()Īx.scatter(x_inside, y_inside, color='g', marker='s')Īx. So if that comes to less than 0.5, the point is inside the. Pi=4*(points in the circle)/(total points) pi = 4*inside/n Download scientific diagram Calculation of Pi using Monte Carlo method and multiprocessing module from publication: On parallel software engineering. You can calculate it as sqrt(dx x dx + dy x dy) where dx is the x distance and dy is the y distance. (points in the circle)/(total points) = (pi*radius^2)/(2*radius)^2 To estimate pi, the points in the circle correspond to the area of the circle enclosing it (pi*radius^2) and the total points correspond to the area of the square enclosing it (2*radius)^2. Here is a variation on hiro protagonist's code, using random.uniform() to allow for random numbers between -1.0 and 1.0, allowing all the points to be plotted, and not just 1/4 of it (not the most elegant code, but it is spelled-out to learn the basics of the Monte Carlo Simulation): import matplotlib.pyplot as plt To introduce how it can be used to calculate Pi, consider a circle circumscribed by a square: If you were to randomly throw arrows at this figure, how many of.
0 Comments
Leave a Reply. |