我做了一个包含4个变量的多重积分,其中有两个极限作为函数。但是,该错误出现在我的一个常量限制变量上。真搞不懂我们为什么。非常感谢您的建议!
from numpy import sqrt, sin, cos, pi, arcsin, maximum
from sympy.functions.special.delta_functions import Heaviside
from scipy.integrate import nquad
def bmax(x):
return 1.14*10**9/sin(x/2)**(9/7)
def thetal(x,y,z):
return arcsin(3.7*10**15*sqrt(cos(x/2)**2/10**6-1.23*10**10/z+0.003*sin(x/2)**2*(2.51*10**63/sin(x/2)**9/y**7-1))/(z*sin(x/2)**2*cos(x/2)*(2.51*10**63/sin(x/2)**9/y**7-1)))
def rt(x,y):
return 3.69*10**12/(2.5*10**63/sin(x/2)**7*y**7-sin(x/2)**2)
def rd(x,y):
return maximum(1.23*10**10,rt(x,y))
def rl(x,y):
return rd(x,y)*(sqrt(1+5.04*10**16/(rd(x,y)*cos(x/2)**2))-1)/2
def wbound():
return [1.23*10**10,3.1*10**16]
def zbound():
return [10**(-10),pi-10**(-10)]
def ybound(z):
return [0,bmax(z)-10**(-10)]
def xbound(z,y,w):
return [thetal(z,y,w),pi-thetal(z,y,w)]
def f(x,y,z,w):
return [5.77/10**30*sin(z)*sin(z/2)*y*sin(x)*Heaviside(w-rl(z,y))*Heaviside(w-rd(z,y))/w**2]
result = nquad(f, [xbound, ybound,zbound,wbound])发布于 2017-07-31 12:52:44
造成此错误的原因是,虽然不希望这些边界依赖于变量,但nquad仍然将变量传递给提供给它的函数。因此,绑定函数必须接受正确的变量数:
def wbound():
return [1.23*10**10,3.1*10**16]
def zbound(w_foo):
return [10**(-10),pi-10**(-10)]
def ybound(z, w_foo):
return [0,bmax(z)-10**(-10)]
def xbound(z,y,w):
return [thetal(z,y,w),pi-thetal(z,y,w)]现在,函数zbound和ybound接受额外的变量,但是忽略它们。
我不确定最后一个界限,xbound(...):您想要翻转变量y和z吗?根据scipy.integrate.nquad的定义,应该正确的排序是
def xbound(y,z,w):
... 编辑:正如kazemakase所指出的,函数f应该返回一个float,而不是一个列表,因此应该删除返回语句中的方括号[...]。
发布于 2017-07-31 13:59:22
nquad期望它的第二个参数有一个bounds序列,语法相当严格。
如果integrand f依赖于x, y, z, w,并且这是定义的顺序,那么bounds中的术语必须依次为xb、yb、zb和wb,其中每个边界都可以是一个二元组,例如xb = (xmin, xmax)或返回一个二元组的函数。
关键是,这些函数的论点.当我们在dx中执行内部集成时,我们有可用的y、z和w来计算x中的边界,因此必须是
def xb(y,z,w): return(..., ...) -同样是def yb(z,w): return (..., ...)和
def zb(w): return (..., ...)。
关于最后一个积分变量的边界必须是常数。
总结
# DEFINITIONS
def f(x, y, z, w): return .. . # x inner integration, ..., w outer integration
def xb(y,z,w): return (...,...) # or simply xb=(...,...) if it's a constant
def yb(z,w): return (...,...) # or yb=(...,...)
def zb(w): return (...,...) # or zb=(...,...)
wb = (...,...)
# INTEGRATION
result, _ = nquad(f, [xb, yb, zb, wb])https://stackoverflow.com/questions/45415538
复制相似问题