我正在尝试使用 SciPy 来集成此功能:
y(x) = (e^-ax)*cos(x) 在 0 到 4pi 之间。
这是我目前的代码:
from numpy import *
from scipy.integrate import simps
a = 0
x = linspace(0 , 4*pi, 100)
y = (e^-(a*x))*cos(x)
integral_value = simps(y,x)
print integral_value
然而它似乎并没有起作用。任何帮助将不胜感激,谢谢!
最佳答案
好吧,如果您运行该程序,您会收到以下错误:
TypeError: ufunc 'bitwise_xor' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
所以您知道问题出在函数中的 ^
(按位异或)。在 Python 中,使用 **
取指数。
如果有人写:
y = (e**-(a*x))*cos(x)
取而代之的是:
>>> print integral_value
-0.000170200006112
完整程序:
from numpy import *
from scipy.integrate import simps
a = 0
x = linspace(0 , 4*pi, 100)
<b>y = (e**-(a*x))*cos(x)</b>
integral_value = simps(y,x)
print integral_value
您还可以显式使用 numpy
函数:
from numpy import *
from scipy.integrate import simps
a = 0
x = linspace(0 , 4*pi, 100)
y = <b>exp(-a*x)</b>*cos(x)
integral_value = simps(y,x)
print integral_value
为了提高精度,可以增加点数(100个不算多)。
关于python - 使用 scipy 集成功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42717147/