Is it possible to calculate pi in this way? Are you surprised (50)

Hello children, hello big friends!

I'm Cat Girl, a primary school student who fell in love with Python programming.

Learn Python with Cat Girl and learn programming together.

today's theme

What is pi?

How to calculate pi with Monte Carlo method? 

pi

Pi is represented by the Greek letter π (pronounced [paɪ]), which is a constant (approximately equal to 3.141592654) and represents the ratio of the circumference of a circle to its diameter.

It is an irrational number, that is, an infinite non-repeating decimal.

Pi is the ratio of the circumference of a circle to its diameter, generally represented by the Greek letter π, and is a mathematical constant that exists widely in mathematics and physics.

π is also equal to the ratio of the area of ​​a circle to the square of the radius, and is a key value for accurately calculating geometric shapes such as the circumference of a circle, the area of ​​a circle, and the volume of a sphere.

In analysis, π can be strictly defined as the smallest positive number x satisfying sinx=0.

In daily life, 3.14 is usually used to represent pi for approximate calculations. However, 3.141592654 with nine decimal places is sufficient for general calculations.

Even if engineers or physicists want to perform more precise calculations, they only need to take values ​​to a few hundred decimal places at best.

pizza

Do you like to eat pizza?

Have you ever noticed that pizzas are generally round, but the boxes in which they are packaged are square?

Something like this:

We choose 1/4, and randomly sprinkle sesame seeds on it, assuming there are enough sesame seeds.

What is the area of ​​the square above?

2*2=4

What is the area of ​​the circle above?

pi*1*1=pi

Take 1/4 of the upper right corner as 1 for the area of ​​a square, and π/4 as the 1/4 of the upper right corner for the area of ​​a circle.

Suppose the number of sesame seeds falling on the 1/4 circle is x, and the number of sesame seeds falling on the 1/4 square is y.

Then x:y=π/4:1.

We use a computer to randomly generate a large amount of data, and simulate the number falling in a circle and a square, can't we calculate π?

Monte Carlo method to calculate pi

The above algorithm is proposed by Monte Carlo, also known as the Monte Carlo method (Monte Carlo method).

Its theoretical basis is the law of large numbers.

Law of Large Numbers

The law of large numbers is a law describing the results of a considerable number of repeated experiments, under the guarantee of the law of large numbers:

Use the frequency of an event as an approximation of the probability of the event occurring.

As long as a random experiment is designed, the probability of an event is related to an unknown, and then the approximate value of the unknown can be obtained by repeating the experiment and expressing the probability with a frequency approximation.

The larger the sample size, the closer the average will be to the true value.

This method can solve differential equations, find multiple integrals, find eigenvalues, etc.

The first limit theorem in the history of probability theory belongs to Bernoulli, and later generations call it "the law of large numbers".

The phenomenon of the Bernoulli family's shining stars and abundant talents has been praised by people for hundreds of years, and it also gives people a profound inspiration: the "accumulation of advantages" in the family is the cradle for the growth of outstanding talents.

In probability theory, the law that the arithmetic mean of a random variable sequence converges to the arithmetic mean of each mathematical expectation of the random variable is discussed.

In the repeated occurrence of a large number of random events, there is often an almost inevitable law, which is the law of large numbers.

In layman's terms, this theorem is that, under the condition that the experiment remains unchanged, the experiment is repeated many times, and the frequency of random events approximates its probability. Accidentally contains a kind of necessity.

The laws of large numbers are divided into weak laws of large numbers and strong laws of large numbers.

Python code implementation

It can be deduced from the above that the accuracy of the calculation of pi by the Monte Carlo method is related to the number of data. The larger the amount of data, the more accurate the pi.

Test Results:

Well, let's learn this today!

If you encounter any problems, let's communicate and solve them together.

I'm Cat Girl, see you next time!

Guess you like

Origin blog.csdn.net/parasoft/article/details/130918180