Skip to main content
 首页 » 编程设计

python之使用输出数组在 Python 中计算数组的积分

2024年12月31日9Terrylee

我想计算形式的积分

我希望将结果作为数组(最终将它们绘制为 omega 的函数)。我有

    import numpy as np 
    import pylab as plt 
    from scipy import integrate 
     
    w = np.linspace(-5, 5, 1000) 
 
    def g(x): 
        return np.exp(-2*x) 
 
    def complexexponential(x, w): 
        return np.exp(-1j*w*x) 
 
    def integrand(x, w): 
        return g(x)*complexexponential(x, w) 
 
    integrated = np.real(integrate.quad(integrand, 0, np.inf, args = (w))) 

这给了我错误“提供的函数没有返回有效的 float ”。我对 Scipy 的集成功能不是很熟悉。非常感谢您的提前帮助!

请您参考如下方法:

Scipy integrate.quad 似乎不支持矢量输出。如果您遍历 w 的所有值并且一次只提供其中一个作为参数,您的代码似乎可以正常工作。

此外,它不处理复杂的集成,您可以使用 this answer 中概述的过程绕过它.