我有一个定义积分的函数。
数组Ai,j稍后通过将所述函数乘以0.5/pi来定义。
我得到了错误:
浮点数:*:‘TypeError’和'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'发布于 2014-11-25 04:41:36
假设这就是integral_normal()的完整定义,那么您永远不会从函数中实际返回一个值。默认情况下,如果没有return语句,python将返回None,这意味着在第28行对integral_normal(p_i, p_j)的递归调用将始终返回None。要说“你需要实际返回值”这句话实在太多了。
https://stackoverflow.com/questions/27113673
复制相似问题