我想集成两个函数的乘积,但似乎我一直得到一个错误
属性错误:“穆尔”对象没有属性“cos”
import numpy as np
import sympy as sp
x = sp.symbols('x')
dp_xi = []
dp_eta = []
for n in range (1,5,1):
for m in range (1,5,1):
dp_xi.append(np.cos((n-2)*(np.pi/180)*x)*(n-2)*np.pi/180)
dp_eta.append(np.cos((m-2)*(np.pi/180)*x)*(m-2)*np.pi/180)
df_xi = sum(dp_xi)
df_eta = sum(dp_eta)
I = sp.integrate(df_xi*df_eta, (x, 0, 1))
print("I=",I)
有什么想法吗谢谢
我试着用sp代替np但积分不起作用
3条答案
按热度按时间t9aqgxwy1#
如前所述,如果要使用解析积分,则应仅使用符号。
n6lpvg4x2#
完整的错误消息在哪里?
看看我在运行你的代码时得到了什么
它指向包含
np.cos
的行。自己运行该表达式:去掉
cos
,我们看到的参数是:简短的回答,正如其他人解释的那样,
np.cos
不能用于sympy.symbol
或表达式。sympy
自己的cos
运行,但是,由于x
仍然是一个符号,它不会尝试返回一个数值:使用
sp
代替:和积分:
gxwragnw3#
如果你使用
sum
的代码,你的代码可以很好地使用sp而不是np。(另外,由于每个表达式只依赖于一个循环变量,所以不需要双循环。)产出