1

我有一个定义积分的函数。

稍后通过将所述函数乘以 0.5/pi 来定义数组 A[i,j]。

我得到错误:

TypeError: *: 'float' 和 'NoneType' 的不支持的操作数类型

显然,这是因为我将无类型的东西乘以一个 Python 无法解释的数字。

你们都建议什么是解决此错误的最简单方法?

我在下面包含了我的代码(不包括所有代码,因此总和变量可能看起来未定义)

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'
4

1 回答 1

0

假设这是 的完整定义integral_normal(),您实际上永远不会从函数返回值。默认情况下,如果您没有 return 语句,python 将 return None,这意味着您对第 28 行的递归调用integral_normal(p_i, p_j)将始终 return None。有很多话要说“你需要实际返回一个值”。

于 2014-11-24T20:41:36.010 回答