我正在尝试创建一个将在计算中进一步使用的整数值数组。问题是integrate.quad返回(回答,错误)。我不能在其他计算中使用它,因为它不是浮点数;它是一组两个数字。
答案 0 :(得分:10)
@BrendanWood的答案很好,你已经接受了,所以它显然对你有用,但还有另一个python习惯用于处理这个问题。 Python支持“多重赋值”,这意味着您可以说x, y = 100, 200
来分配x = 100
和y = 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]