蒙特卡罗方法。三重积分。Python。“+”的操作数父级不受支持

2024-09-30 12:28:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试用蒙特卡罗方法近似三重积分∫∫∫∫xyzdV,其中S=[0,1]×[0,1]×0,1]。 我有这个代码:

from numpy import *
import time
from scipy.integrate import tplquad
numpoints=100000 # number of random sample points
I2d=0.0     # initialize value
I2dsquare=0.0   # initialize to allow for calculation of variance
for n in xrange(numpoints):
  x=random.uniform()
  y=random.uniform()
  z=random.uniform()
  func = lambda x,y,z: x*y*z
  x1,x2 = 0, 1
  y1,y2 = lambda x: 0,lambda x: 1
  z1,z2 = lambda x, y: 0,lambda x, y: 1
  I2d += tplquad(func, x1,x2,y1,y2,z1,z2)
  I2dsquare += (tplquad(func, x1,x2,y1,y2,z1,z2))**2

I2d=I2d/numpoints
I2dsquare=I2dsquare/numpoints
EstimError=4*sqrt( (I2dsquare - I2d**2)/numpoints) # estimated error
I2d=4*I2d
print "Value:  %f" %I2d
print "Error estimate: %f" %EstimError

我有个错误:

^{pr2}$

我知道这段代码中有不同的类型,但我不知道如何修复它。例如,如果我试着为二次方程编写这段代码,一切正常,但不幸的是积分不起作用。在


Tags: lambda代码importrandomuniformfuncx1x2

热门问题