2013-07-23 52 views

回答

2

integrate.quad返回兩個值(和在某些情況下可能更多的數據)的tuple。您可以通過引用返回的元組的第零個元素來訪問答案值。例如:

# 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] 
7

@ BrendanWood的答案是很好,你已經接受了,所以它顯然爲你工作,但處理這個另一個Python成語。 Python支持「多任務」,這意味着你可以說x, y = 100, 200分配x = 100y = 200。 (在介紹Python的教程的例子見http://docs.python.org/2/tutorial/introduction.html#first-steps-towards-programming。)

要與quad使用這樣的想法,你可以做以下的(a布倫丹的例子的修改):

# 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 

我發現這個代碼更容易讀。

+0

+1,我真的喜歡明確分配結果爲有意義的名稱,否則太容易混淆之後 – EnricoGiampieri