Monte carlo 圆的面积
Monte carlo area of a circle
是否可以使用Monte carlo来计算半径大于1的圆的面积?
我试着这样做,但它只适用于半径为 1 的圆。
N = 10000
incircle = 0
count = 0
while (count<N)
x = random()
y = random()
if sqrt((x-a)^2 +(y-b)^2) <= R then
incircle = incircle+1
endif
count = count+1
so
不知道你用的是什么语言,我会在Python中写下下面的代码,希望你能看懂。
我针对半径为2
的圆计算面积的情况进行了计算。
这里Monte Carlo算法如下。让我们创建一个范围 [-R ; R]
内具有 X
和 Y
的正方形。现在让我们在这个 square.We 内随机均匀生成点,将计算生成点的总数和由给定正方形界定的半径 R
的圆内的点数。一个点如果X^2 + Y^2 <= R^2
落入圆内,否则落在圆外。最后正方形的面积是 (2 * R)^2
,而圆形面积(大约)等于正方形面积的第 num_points_in_circle / num_points_total
部分。
在控制台输出中,我还显示了 error
,它显示了与通过公式 Pi * R^2
.
精确计算的预期值的绝对差异
在Python中x ** 2
表示x
的2
次方。
import random, math
R = 2
niters = 300_000
total, in_circle = 0, 0
for j in range(20):
for i in range(niters):
x = random.uniform(-R, R)
y = random.uniform(-R, R)
if x ** 2 + y ** 2 <= R ** 2:
in_circle += 1
total += 1
area = (2 * R) ** 2 * in_circle / total
expected = math.pi * R ** 2
print(f'After {(j + 1) * niters:>8} iterations area is {area:.08f}, ' +
f'error is {abs(area - expected):.08f}', flush = True)
输出:
After 300000 iterations area is 12.59130667, error is 0.02493605
After 600000 iterations area is 12.58341333, error is 0.01704272
After 900000 iterations area is 12.58072889, error is 0.01435827
After 1200000 iterations area is 12.57965333, error is 0.01328272
After 1500000 iterations area is 12.57741867, error is 0.01104805
After 1800000 iterations area is 12.57348444, error is 0.00711383
After 2100000 iterations area is 12.57067429, error is 0.00430367
After 2400000 iterations area is 12.57078000, error is 0.00440939
After 2700000 iterations area is 12.56853926, error is 0.00216864
After 3000000 iterations area is 12.56956800, error is 0.00319739
After 3300000 iterations area is 12.56826182, error is 0.00189120
After 3600000 iterations area is 12.56863111, error is 0.00226050
After 3900000 iterations area is 12.56763897, error is 0.00126836
After 4200000 iterations area is 12.56839238, error is 0.00202177
After 4500000 iterations area is 12.56855111, error is 0.00218050
After 4800000 iterations area is 12.56861667, error is 0.00224605
After 5100000 iterations area is 12.56857725, error is 0.00220664
After 5400000 iterations area is 12.56881185, error is 0.00244124
After 5700000 iterations area is 12.56925193, error is 0.00288132
After 6000000 iterations area is 12.56927733, error is 0.00290672
是否可以使用Monte carlo来计算半径大于1的圆的面积? 我试着这样做,但它只适用于半径为 1 的圆。
N = 10000
incircle = 0
count = 0
while (count<N)
x = random()
y = random()
if sqrt((x-a)^2 +(y-b)^2) <= R then
incircle = incircle+1
endif
count = count+1
so
不知道你用的是什么语言,我会在Python中写下下面的代码,希望你能看懂。
我针对半径为2
的圆计算面积的情况进行了计算。
这里Monte Carlo算法如下。让我们创建一个范围 [-R ; R]
内具有 X
和 Y
的正方形。现在让我们在这个 square.We 内随机均匀生成点,将计算生成点的总数和由给定正方形界定的半径 R
的圆内的点数。一个点如果X^2 + Y^2 <= R^2
落入圆内,否则落在圆外。最后正方形的面积是 (2 * R)^2
,而圆形面积(大约)等于正方形面积的第 num_points_in_circle / num_points_total
部分。
在控制台输出中,我还显示了 error
,它显示了与通过公式 Pi * R^2
.
在Python中x ** 2
表示x
的2
次方。
import random, math
R = 2
niters = 300_000
total, in_circle = 0, 0
for j in range(20):
for i in range(niters):
x = random.uniform(-R, R)
y = random.uniform(-R, R)
if x ** 2 + y ** 2 <= R ** 2:
in_circle += 1
total += 1
area = (2 * R) ** 2 * in_circle / total
expected = math.pi * R ** 2
print(f'After {(j + 1) * niters:>8} iterations area is {area:.08f}, ' +
f'error is {abs(area - expected):.08f}', flush = True)
输出:
After 300000 iterations area is 12.59130667, error is 0.02493605
After 600000 iterations area is 12.58341333, error is 0.01704272
After 900000 iterations area is 12.58072889, error is 0.01435827
After 1200000 iterations area is 12.57965333, error is 0.01328272
After 1500000 iterations area is 12.57741867, error is 0.01104805
After 1800000 iterations area is 12.57348444, error is 0.00711383
After 2100000 iterations area is 12.57067429, error is 0.00430367
After 2400000 iterations area is 12.57078000, error is 0.00440939
After 2700000 iterations area is 12.56853926, error is 0.00216864
After 3000000 iterations area is 12.56956800, error is 0.00319739
After 3300000 iterations area is 12.56826182, error is 0.00189120
After 3600000 iterations area is 12.56863111, error is 0.00226050
After 3900000 iterations area is 12.56763897, error is 0.00126836
After 4200000 iterations area is 12.56839238, error is 0.00202177
After 4500000 iterations area is 12.56855111, error is 0.00218050
After 4800000 iterations area is 12.56861667, error is 0.00224605
After 5100000 iterations area is 12.56857725, error is 0.00220664
After 5400000 iterations area is 12.56881185, error is 0.00244124
After 5700000 iterations area is 12.56925193, error is 0.00288132
After 6000000 iterations area is 12.56927733, error is 0.00290672