获取类型错误:**或pow()的操作数类型不受支持:“内置函数”或“方法”和“int”在Python中求解方程时

2024-06-01 04:23:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Mathematica中为f(x,t)创建了一个原始方程:

uExact[x_, t_] := (1 + I*t^2)* Cos[2 * Pi *x];

f[x_, t_] := 
  4 * Pi^2 * (1 + I * t^2) * Cos[2* Pi * x] + 2*I*t*Cos[2 Pi*x] - 
   I * g * log[(t^4 + 1) * Cos^2[2*Pi*x]];

我已将其转换为以下代码:

def u_exact(x, t):

    return (1 + I*t**2)* cmath.cos(2 * cmath.pi *x)
    


#check!!!
def f(x,t):
    #1return 4 * cmath.pi**2 * (1 + I * t**2) * cmath.cos(2* cmath.pi * x) + 2*I*t*cmath.cos(2 * cmath.pi*x) - I * g * cmath.log((t**4 + 1) * cmath.cos**2(2*cmath.pi*x))
    

    #alternative

    p1 = 4 * pow(cmath.pi,2) * (1 + I * pow(t,2)) * pow(cmath.cos,2)* (2*cmath.pi * x)
    p2 = 2*I*t*cmath.cos(2 * cmath.pi*x)
    p3 = - I * g * cmath.log(pow(t,4) + 1) * pow(cmath.cos,2) * (2*cmath.pi*x)

    return (p1 + p2 + p3)

在第1种情况和第2种情况下,我不断得到相同的错误:不支持**或pow()的操作数类型:“内置函数”或“方法”

有什么问题吗?我错过什么了吗


Tags: logreturndefpi情况coscmath方程
2条回答

cmath.cos是一个函数。您正在应用pow函数,并将函数作为基础

>>> pow(cmath.cos, 2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for ** or pow(): 'builtin_function_or_method' and 'int'
>>> pow(cmath.cos(1), 2)
(0.2919265817264289+0j)

在两种备选方案中,您都试图将幂运算应用于函数(math.cos):

cmath.cos**2
pow(cmath.cos,2)

cmath.cos是函数,如果你想应用幂函数,你需要一个浮点,所以你需要调用函数:cmath.cos(value)(用你需要的任何东西替换value

相关问题 更多 >