为了工作,你需要整合区域,我从一个简单的图形开始 - 一个椭球体。 Python代码:
import scipy.integrate as integral
x0 = 0
y0 = 0
z0 = 0
a = 2
b = 1
c = 1
def f(x, y, z):
return 1
def bounds_z(x, y):
return [z0 - c*(1 - ((x - x0)/a)**2 - ((y - y0)/b)**2)**0.5, z0 + c*(1 - ((x - x0)/a)**2 - ((y - y0)/b)**2)**0.5]
def bounds_y(x):
return [y0 - b*(1 - ((x - x0)/a)**2)**0.5, y0 + b*(1 - ((x - x0)/a)**2)**0.5]
def bounds_x():
return [x0 - a, x0 + a]
res = integral.nquad(f, [bounds_z, bounds_y, bounds_x])
print(res)
执行因错误而中断:
TypeError: '<' not supported between instances of 'complex' and 'complex'
从逻辑上讲,积分的限制及其顺序设置正确。我查看了Wolfram alpha,一切都按其应有的方式解决了。
我尝试将数字放入边界函数而不是参数变量(例如a)中,没有任何变化。如果删除一维,例如z,则一切都很重要。如果您使用球体而不是椭球体,则一切都很重要。如果增加参数b,那么从b = 1.9 .. 2之间的某个位置开始,所有内容都会开始正确计算。
这里出了什么问题以及如何将所有内容与代码中的参数集成?