【问题标题】:How to create a function that is dependent on an array of values?如何创建一个依赖于值数组的函数?
【发布时间】:2017-09-18 01:10:06
【问题描述】:

我要编写的函数是 f(x) = beta/(pi*(x^(2) + beta^(2)) 对于 beta 值 0、10、20、50、100

所以我想在从负无穷到无穷的 x 范围内整合这个函数。我有下面代码的基础,但不确定如何将数组传递给 beta。

感谢您的帮助!

from scipy.integrate import quad
from sympy import Symbol 
from math import pi
import numpy as np
import matplotlib.pyplot as plt

def integrand(x):
    return limit(beta/(np.pi*(x**2 + beta**2)));
xlo = '-inf'
xhi = 'inf'
result = quad(integrand, xlo, xhi, args=(beta))
print result 

【问题讨论】:

  • import scipy as quad?你不是说from scipy.integrate import quad吗?

标签: arrays python-3.x matplotlib


【解决方案1】:

试试这个:

from scipy.integrate import quad
from math import pi
import numpy as np

def integrand(x, beta):
    return beta/(np.pi*(x**2 + beta**2))

for beta in [0., 1., 10., 20., 50., 100.]:
    xlo = -np.inf
    xhi = np.inf
    result = quad(integrand, xlo, xhi, args=(beta))
    print('beta: {}, result: {}, error estimate: {}'.format(beta, *result))

如果此表达式的 beta > 0,则积分应始终为 1,无论 beta 是多少,如果您 integrate it。我不知道为什么limit 在那里或者它应该来自哪里,所以我把它删除了。如果 beta 为零,则该函数在 x=0 处未定义,您可能需要注意这一点。

【讨论】:

    猜你喜欢
    • 2020-12-04
    • 2019-03-13
    • 2020-11-13
    • 2016-10-22
    • 1970-01-01
    • 2010-10-06
    • 2012-04-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多