![]() # Now we'll have to generate random values for x1, x2, x3, x4, x5, å, x7, x8 # and x9 all being between (where R = 1 in this tutorial). # By now you should have noticed how easy it is to extend the number of # dimensions. ![]() ![]() pi ** 2 # The known value of the volume of the sphere ratio = V5D / V5Dexact # Compare the calculated volume to the known volume print ( 'The volume of a', d, 'D sphere from the Monte Carlo simulation is', V5D ) print ( 'The exact volume is', V5Dexact, 'and the ratio of the two is', V5D / V5Dexact ) sqrt ( x1 ** 2 + x2 ** 2 + x3 ** 2 + x4 ** 2 + x5 ** 2 ) # Calculate distance from the orgin if R <= 1 : # Check for a hit Zn += 1 # If there's a hit increment Zn V5D = 2 ** d * Zn / n # The Monte Carlo calculation of the sphere volume V5Dexact = ( 8 / 15 ) * np. d = 5 # The number of dimensions n = int ( 1e5 ) # The number of Monte Carlo trials Zn = 0 for i in range ( n ): x1 = np. So that we can # compare our Monte Carlo simulation to expected results, we'll note that a # 5-D sphere has a volume of (8/15)*pi^2*R^5. # Now we'll have to generate random values for x1, x2, x3, x4, and x5 all # being between (where R = 1 in this tutorial). pi ** 2 # The known value of the volume of the sphere ratio = V4D / V4Dexact # Compare the calculated volume to the known volume print ( 'The volume of a', d, 'D sphere from the Monte Carlo simulation is', V4D ) print ( 'The exact volume is', V4Dexact, 'and the ratio of the two is', V4D / V4Dexact ) sqrt ( x1 ** 2 + x2 ** 2 + x3 ** 2 + x4 ** 2 ) # Calculate distance from the orgin if R <= 1 : # Check for a hit Zn += 1 # If there's a hit increment Zn V4D = 2 ** d * Zn / n # The Monte Carlo calculation of the sphere volume V4Dexact = ( 1 / 2 ) * np. d = 4 # The number of dimensions n = int ( 1e5 ) # The number of Monte Carlo trials Zn = 0 for i in range ( n ): x1 = np. So that we can compare our # Monte Carlo simulation to expected results, we'll note that a 4-D sphere # has a volume of (1/2)*pi^2*R^4. # Now we'll have to generate random values for x1, x2, x3, and x4 all being # between (where R = 1 in this tutorial). We cannot picture a 4-D sphere, # but mathematically the definition is obvious: x1^2+x2^2+x3^2+x4^2 < R^2. # Now we're onto something more interesting. pi # The known value of the volume of the sphere ratio = V2D / V2Dexact # Compare the calculated volume to the known volume print ( 'The volume of a', d, 'D sphere from the Monte Carlo simulation is', V2D ) print ( 'The exact volume is', V2Dexact, 'and the ratio of the two is', V2D / V2Dexact ) sqrt ( x1 ** 2 + x2 ** 2 ) # Calculate distance from the orgin if R <= 1 : # Check for a hit Zn += 1 # If there's a hit increment Zn V2D = 2 ** d * Zn / n # The Monte Carlo calculation of the sphere volume V2Dexact = np. d = 2 # The number of dimensions n = int ( 1e5 ) # The number of Monte Carlo trials Zn = 0 for i in range ( n ): x1 = np. Note that the example of the area # of a circle is often used as a Monte Carlo calculation of the numerical # value of pi. One of the virtues of the # Monte Carlo method is that going to higher dimensions requires only VERY # minor changes to our simulation code. The volume of the 2-D sphere is expected # to be the area of a circle. Now we'll have # to generate random values for x1 and x2 both being between # (where R = 1 in this tutorial). sqrt ( x1 ** 2 ) # Calculate distance from the orgin if R <= 1 : # Check for a hit Zn += 1 # If there's a hit increment Zn V1D = 2 ** d * Zn / n # The Monte Carlo calculation of the sphere volume V1Dexact = 2 # The known value of the volume of the sphere ratio = V1D / V1Dexact # Compare the calculated volume to the known volume print ( 'The volume of a', d, 'D sphere from the Monte Carlo simulation is', V1D ) print ( 'The exact volume is', V1Dexact, 'and the ratio of the two is', V1D / V1Dexact ) d = 1 # The number of dimensions n = int ( 1e5 ) # The number of Monte Carlo trials Zn = 0 for i in range ( n ): x1 = np. The volume of the 1-D sphere is just the length of the line. The 1-D sphere is # trivial because ALL of the randomly generated numbers will fall within # the sphere. That is, we'll generate random values # of x1 between (where R = 1 in this tutorial). Throughout this tutorial will restrict # ourselve to the positive quadrant. In 1-D, x1^2< x1 < +R. # We'll start with the trivial example of a 1-D sphere.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |