如何隔离scipy.integrate.quad函数的结果,而不是计算结果和错误?

时间:2013-07-23 19:20:07

标签: python arrays scipy integration quad

我正在尝试创建一个将在计算中进一步使用的整数值数组。问题是integrate.quad返回(回答,错误)。我不能在其他计算中使用它,因为它不是浮点数;它是一组两个数字。

2 个答案:

答案 0 :(得分:10)

@BrendanWood的答案很好,你已经接受了,所以它显然对你有用,但还有另一个python习惯用于处理这个问题。 Python支持“多重赋值”,这意味着您可以说x, y = 100, 200来分配x = 100y = 200。 (有关介绍性python教程中的示例,请参阅http://docs.python.org/2/tutorial/introduction.html#first-steps-towards-programming。)

要将此想法与quad一起使用,您可以执行以下操作(修改Brendan的示例):

# Do the integration on f over the interval [0, 10]
value, error = integrate.quad(f, 0, 10)

# Print out the integral result, not the error
print('The result of the integration is %lf' % value)

我发现这段代码更容易阅读。

答案 1 :(得分:4)

integrate.quad返回两个值的tuple(在某些情况下可能会返回更多数据)。您可以通过引用返回的元组的第0个元素来访问答案值。例如:

# import scipy.integrate
from scipy import integrate

# define the function we wish to integrate
f = lambda x: x**2

# do the integration on f over the interval [0, 10]
results = integrate.quad(f, 0, 10)

# print out the integral result, not the error
print 'the result of the integration is %lf' % results[0]