我有一个定义积分的函数。
稍后通过将所述函数乘以0.5 / pi来定义阵列A [i,j]。
我收到错误:
TypeError:*:' float'不支持的操作数类型和' NoneType'
显然这是因为我将一些没有类型的东西乘以一个数字,而python无法解释。
你们所建议的是最容易解决这个错误的方法吗?
我在下面包含了我的代码(不包括所有代码,因此sum变量可能看起来未定义)
def integral_normal(p_i, p_j):
"""Evaluates the contribution of a panel at the center-point of another,
in the normal direction.
Arguments
---------
p_i -- panel on which the contribution is calculated.
p_j -- panel from which the contribution is calculated.
Returns
-------
Integral over the panel of the influence at a control-point.
"""
def func(s):
return ( (+(p_i.xc-(p_j.xa-sin(p_j.beta)*s))*cos(p_i.beta)
+(p_i.yc-(p_j.ya+cos(p_j.beta)*s))*sin(p_i.beta))
/((p_i.xc-(p_j.xa-sin(p_j.beta)*s))**2
+(p_i.yc-(p_j.ya+cos(p_j.beta)*s))**2) )
return integrate.quad(lambda s:func(s), 0., p_j.length)[0]
# computes the source influence matrix
A = np.empty((N_panels, N_panels), dtype=float)
np.fill_diagonal(A, 0.5)
# use enumerate to access element panels individually from 0 to i, locates element of A to fill.
for i, p_i in enumerate(panels):
for j, p_j in enumerate(panels):
if i != j:
A[i,j] = 0.5/pi*integral_normal(p_i, p_j)
# computes right hand side of linear system
b = - u_inf * np.cos([p.beta for p in panels])
# solves the linear system
sigma = np.linalg.solve(A, b)
for i, panel in enumerate(panels):
panel.sigma = sigma[i]
这是错误:
TypeError Traceback (most recent call last)
<ipython-input-101-b5c8b9263c2d> in <module>()
26 for j, p_j in enumerate(panels):
27 if i != j:
----> 28 A[i,j] = 0.5/pi*integral_normal(p_i, p_j)
29
30 # computes right hand side of linear system
TypeError: unsupported operand type(s) for *: 'float' and 'NoneType'
答案 0 :(得分:0)
假设这是integral_normal()
的整个定义,您实际上永远不会从函数返回值。默认情况下,如果您没有return语句,python将返回None
,这意味着您对第28行的integral_normal(p_i, p_j)
的递归调用将始终返回None
。这有很多话要说“你需要实际返回一个值。”