scipy.optimize.newton 报错 TypeError: 不能将整数与元组连接

0 投票
1 回答
1898 浏览
提问于 2025-04-18 09:16

我的整个程序都是正确的(我在不同阶段都检查过了)。不过,这个模块中标记的那一行却返回了错误:

TypeError: can only concatenate tuple (not "int") to tuple

我不知道为什么会这样。funcPsat 返回的是浮点数值。我会很感激任何有用的建议!

import scipy.optimize.newton as newton

def Psat(self, T):
    pop= self.getPborder(T)
    boolean=int(pop[0])
    P1=pop[1]
    P2=pop[2]
    if boolean:
        Pmin = min([P1, P2])
        Pmax = max([P1, P2])
        if Pmin > 0.0: 
            Pguess = 0.5*(Pmin+Pmax) 
        else:
            Pguess=0.5*Pmax
        solution = newton(self.funcPsat, Pguess, args=(T))   #error in this line
        return solution
    else:
        return None

1 个回答

4

我觉得问题在于,根据文档的说明

args: 元组,可选

在函数调用中使用的额外参数。

这里的 args 参数应该是一个 元组

光用括号是没用的;元组的语法需要用到逗号。例如:

>>> T = 0
>>> type((T))
<type 'int'>
>>> type((T,))
<type 'tuple'>

试试:

solution = newton(self.funcPsat, Pguess, args=(T,))
                                              # ^ note comma

撰写回答