python - 使用 scipy 集成功能?

标签 python scipy

我正在尝试使用 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/

相关文章:

python - Python 上的信号量

Python抽象方法默认逻辑,super()方法

python - Scipy `fmin_cg` 参数与我的函数参数不匹配

python - opt.curve_fit 只有一个参数

python - 在矩阵中查找最小值的索引

Python - 在函数中使用来自另一个模块的函数

python - 如何使用 scrapy 在两个连续请求中进行回调

python - 如何使用 pip 安装带有额外组件的本地 Python 包?

python - 为什么在 locals 中添加 key 实际上会创建变量?

python-3.x - 使用scipy的solve_ivp求解非线性摆运动