My Coding >
Numerical simulations >
Stochastic methods >
MonteCarlo methods >
Circle length calculation with the MonteCarlo method
Circle length calculation with the MonteCarlo methodThis is an indirect application of the Monte Carlo techniques because you can't calculate the length of the circle (or the surface area of a ball) but you can calculate related values, for example, the area of a thin line around a circle, or a thin layer around a sphere. Believing, that the layer is very thin, then its volume will be proportional to the length or surface area, depending on how many dimensions we are working. This was the theory, but now we will do some practical calculations. Calculation length of the circleCode for Monte Carlo simulationFirst of all, we need to load libraries, numpy for vectorized calculations and matplotlib. I will create code, which will work in any dimensional space.
Now we can define the following parameters: dim  the size of our dimension; N  number of trials; circle  radius of out circle;  half size of the box side; delta  thickness of the layer;
Now, let's generate the random dataset and calculate the distances. We will calculate randomly located dots in the box sized 2*box, and our data set will contain N dots with dimension dim. For calculating distances from the centre we can apply the equation of the distance calculation along axis=1.
When we have our test box filled with random dots, we need to classify these random dots, which one belongs to our studying surface, and which one belongs to the test square. For the studying surface, we will take dots the volumes from both sides, to compensate for the potential curvicity of the line (or surface). For the test square of the known volume (actually line with known length, we will take only dots from one side). C_inside  mask for dots within delta from the circle; P_inside  mask for dots
Now these masks have only required dots and the relation of the volumes and respective circle lengths will be proportional to the sum of True values in these masks.
The sum will only count True values. Volume  this is a very important equation, we need to take only half of the circle data because we've calculated from both sides and we need to take the average half and divide by plane dots. Then this ratio we need to multiply this by the test line or square size. For circle length or twodimensional case, we can check results by calculating π as follows: print(f"PI = {V/2}") and we will have some values in the vicinity of 3.14 Code for visualisationVisualisation is a straightforward procedure, and I will not explain it in details, only will give you a code.
You will have something like that::
It is important to mention, that for this technique, you only need to know, how to calculate the distance from the test point to the surface and choose the correct test surface. But this I will show in the following tutorials. Also, it is very important to have a reasonable balance between the thickness of the layer and the number of random test dots.

Last 10 artitles
9 popular artitles


© 2020 MyCoding.uk My blog about coding and further learning. This blog was writen with pure Perl and frontend output was performed with TemplateToolkit. 