[英]Multidimensional Monte Carlo Method
I am trying to evaluate the following multidimensional integral:我正在尝试评估以下多维积分:
with the answer being 155/6.答案是 155/6。 However the Python code I've used:但是我使用的 Python 代码:
doesn't work.不起作用。 How do I correct or what code should I do instead?我该如何更正或我应该改写什么代码?
You have couple of bugs in your code.您的代码中有几个错误。
Most important one is when you square point first and sum later, not as shown in formula.最重要的是当你先平方点然后求和时,而不是如公式所示。 Then you divide by 'n' which is undefuned.然后除以未定义的“n”。
Here is correct code这是正确的代码
import numpy as np
rng = np.random.default_rng()
D = 10
N = 1000000
I = 0.0
for k in range(0, N):
pt = rng.random(D)
I += np.sum(pt)**2
print(I/N*6.0/155.0)
On Python 3.8.5 x64, windows 10, it prints在 Python 3.8.5 x64, windows 10 上,它打印
0.9996726183447514
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.