Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中,是什么导致此错误(AttributeError:';Mul';对象没有属性';cos';)的?_Python_Sympy_Integral - Fatal编程技术网

在Python中,是什么导致此错误(AttributeError:';Mul';对象没有属性';cos';)的?

在Python中,是什么导致此错误(AttributeError:';Mul';对象没有属性';cos';)的?,python,sympy,integral,Python,Sympy,Integral,在Python中尝试计算定积分时,我得到以下错误代码 AttributeError Traceback (most recent call last) <ipython-input-7-2be8718c68ec> in <module>() 7 x, n = symbols('x n') 8 ----> 9 f = (cos(n*x))*(x**2-pi**2)^2 10 in

在Python中尝试计算定积分时,我得到以下错误代码

AttributeError                            Traceback (most recent call last)
<ipython-input-7-2be8718c68ec> in <module>()
      7 x, n = symbols('x n')
      8 
----> 9 f = (cos(n*x))*(x**2-pi**2)^2
     10 integrate(f,(x,-n*pi,n*pi))
     11 

AttributeError: 'Mul' object has no attribute 'cos' 

您的问题在于名称空间冲突

from sympy import *
from numpy import *
因为
numpy
sympy
都有自己的
cos
定义。错误是告诉您
Mul
对象(即
n*x
)没有余弦方法,因为解释器现在混淆了
sympy
numpy
方法。改为这样做

import pylab as pl
import numpy as np
import sympy as sp

x, n = sp.symbols('x n')
f = (sp.cos(n*x))*(x**2-sp.pi**2)**2
sp.integrate(f,(x,-n*sp.pi,n*sp.pi))
还请注意,我已将
^
更改为
**
,因为
^
中的
运算符。这里,我假设您需要
sympy.core.numbers.Pi
中的符号
Pi
,而不是
numpy
中的数字。如果你想要后者,那么就这样做

f = (sp.cos(n*x))*(x**2-np.pi**2)**2
sp.integrate(f,(x,-n*np.pi,n*np.pi))

您正在使用模块导入的
污染您的命名空间*
f = (sp.cos(n*x))*(x**2-np.pi**2)**2
sp.integrate(f,(x,-n*np.pi,n*np.pi))