In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces python how to use Monte Carlo method to find the relevant knowledge of pi, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe everyone will read this python how to use Monte Carlo method to find the pi article will have some gains, let's take a look at it.
Monte Carlo method for calculating pi
Introduction:
Monte Carlo method (Monte Carlo method), also known as statistical simulation method, is a kind of very important numerical calculation method which was proposed in the middle of 1940s due to the development of science and technology and the invention of electronic computer. A method of solving many computational problems using random numbers (or more commonly pseudo-random numbers). The answer is deterministic algorithms. Monte Carlo methods are widely used in financial engineering, macroeconomics, computational physics (such as particle transport calculations, quantum thermodynamic calculations, aerodynamic calculations) and other fields.
In fact, the geometric scheme (P= shaded area (or interval or volume)/total area (or interval or volume)) in high school mathematics is a Monte Carlo method.
import numpy as npimport matplotlib.pyplot as pltfrom matplotlib.patches import Circle
#Number of points cast n = 10,000
#Circle information r = 1.0 #Radius a, b = (0., 0.) #Center
#square region boundary x_min, x_max = a-r, a+ry_min, y_max = b-r, b+r
#Random points x = np.random.uniform(x_min, x_max, n) #Uniform distribution y = np.random.uniform(y_min, y_max, n)
#Calculate the distance d = np.sqrt((x-a)**2 + (y-b)**2)
#count the number of points that fall inside the circle res = sum(np.where(d < r, 1, 0))
#Calculate an approximation of pi (essence of Monte Carlo method: approximation of true values with statistics) pi = 4 * res / n
print('pi: ', pi)
spi = 3.1415926535print ('here the approximate pi and 10-bit pi accuracy is: ', (spi - pi)/spi )
#Draw a graph to see fig = plt.figure() axes = fig.add_subplot(111) axes.plot(x, y,'ro',markersize = 1)plt.axis ('equal ') #Prevent image distortion
circle = Circle(xy=(a,b), radius=r, alpha=0.5)axes.add_patch(circle)
plt.show()
Implementation results:
pi: 3.1224 Here the approximate pi is 0.006109211351324613 to the 10-digit pi accuracy.
About "python how to use Monte Carlo method to find pi" The content of this article is introduced here, thank you for reading! I believe everyone has a certain understanding of "how to use Monte Carlo method to calculate pi in python." If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.