蒙特卡罗方法和pi [英] Monte Carlo Method and pi

查看:66
本文介绍了蒙特卡罗方法和pi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个任务,我必须使用上面的

方法计算pi。


所以我写了以下程序:

---

随机导入

导入数学


n = long (raw_input("请输入迭代次数:))

sy = 0#函数值之和


for i in range (0,n):

x = random.random()#coordinates

sy + = math.sqrt(1-math.sqrt(x))#calculate y and添加到sy


打印4 * sy / n#compute pi

---


不幸的是,甚至对于n = 2000000,结果是~2,13 ....

它收敛到pi非常慢,我不知道为什么。


请帮助我!


问候

Karl

Hi,
I got a task there I have to compute pi using the
Method above.

So I wrote the following program:
---
import random
import math

n = long(raw_input("Please enter the number of iterations: "))
sy = 0 # sum of the function-values

for i in range(0, n):
x = random.random() # coordinates
sy += math.sqrt(1-math.sqrt(x)) # calculate y and add to sy

print 4*sy/n # compute pi
---

Unfortunately, even for n = 2000000 the result is ~2,13... .
It converges very slow to pi and I don''t know why.

Please help me!

Regards
Karl

推荐答案



" Karl Pech" <嘉****** @ users.sf.net>在消息中写道

新闻:cc ************* @ news.t-online.com ...

"Karl Pech" <Ka******@users.sf.net> wrote in message
news:cc*************@news.t-online.com...


我有一个任务,我必须使用上面的
方法计算pi。

所以我编写了以下程序:
--- 导入随机导入数学

n = long(raw_input("请输入迭代次数:))
sy = 0#函数值之和

对于范围内的i(0,n):
x = random.random()#coordinates
sy + = math.sqrt(1-math.sqrt(x) )#calculate y并添加到sy
Hi,
I got a task there I have to compute pi using the
Method above.

So I wrote the following program:
---
import random
import math

n = long(raw_input("Please enter the number of iterations: "))
sy = 0 # sum of the function-values

for i in range(0, n):
x = random.random() # coordinates
sy += math.sqrt(1-math.sqrt(x)) # calculate y and add to sy




此行错误。我认为它应该是sy + = math.sqrt(1 - x ** 2)。做

给我~1.1次1000次迭代。


尼克



This line is wrong. I think it should be sy += math.sqrt(1 - x ** 2). Doing
that gives me ~3.1 for 1000 iterations.

Nick


其他人发现问题你实现了近似的

方法。


你可以用numarray大大加快速度。它得到3.141 ......作为

在这款sub-GHz
笔记本电脑上使用n = 2000000的近似值约为4秒。当然,即使用纯Python编写,收敛速度更快的方法也会在不到4秒的时间内达到3.141




Jeff


导入numarray

导入numarray.random_array

def approx_pi(n):

a = numarray.random_array.uniform(0,1,n)

返回4 * numarray.sum(numarray.sqrt(1 - a ** 2))/ n


如果__name__ ==''__ main__'':

n = int(raw_input("请输入迭代次数:))

print approx_pi (n)


----- BEGIN PGP SIGNATURE -----

版本:GnuPG v1.2.4(GNU / Linux)

iD8DBQFA7cbRJd01MZaTXX0RAoxMAJ4wCeN / z6C7LY7uXg21Y / TYYkpApACZAfBa

8w3R5vITWyFxLNBKG4dS10E =

= Ch / k

----- END PGP SIGNATURE -----

Others spotted the problem with your implementation of the approximatoin
method.

You can greatly speed things up with numarray. It gets "3.141..." as
the approximation using n=2000000 in about 4 seconds on this sub-GHz
laptop. Of course, a method with faster convergence would get to 3.141
in a lot less than 4 seconds, even if written in pure Python.

Jeff

import numarray
import numarray.random_array

def approx_pi(n):
a = numarray.random_array.uniform(0, 1, n)
return 4 * numarray.sum(numarray.sqrt(1 - a**2)) / n

if __name__ == ''__main__'':
n = int(raw_input("Please enter the number of iterations: "))
print approx_pi(n)

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFA7cbRJd01MZaTXX0RAoxMAJ4wCeN/z6C7LY7uXg21Y/TYYkpApACZAfBa
8w3R5vITWyFxLNBKG4dS10E=
=Ch/k
-----END PGP SIGNATURE-----


" Karl Pech" <嘉****** @ users.sf.net>写道:
"Karl Pech" <Ka******@users.sf.net> writes:
我有一个任务,我必须使用上面的
方法计算pi。


这听起来像是一个家庭作业问题...我在范围内(0,n):
x = random.random()#coordinates
sy + = math.sqrt(1-math.sqrt(x))#calculate y并添加到sy

print 4 * sy / n#compute pi
---
<不幸的是,即使对于n = 2000000,结果也是~2,13 ......。
它收敛到pi很慢,我不知道为什么。
I got a task there I have to compute pi using the
Method above.
This sounds like a homework problem... for i in range(0, n):
x = random.random() # coordinates
sy += math.sqrt(1-math.sqrt(x)) # calculate y and add to sy

print 4*sy/n # compute pi
---

Unfortunately, even for n = 2000000 the result is ~2,13... .
It converges very slow to pi and I don''t know why.




更好地确保公式是正确的,通过问自己为什么

模拟应该可以工作。



Better make sure the formulas are correct, by asking yourself why the
simulation is supposed to work at all.


这篇关于蒙特卡罗方法和pi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆