2014-02-14 21 views
1

我最近一直在學習如何在python中執行雙積分。這是我在用的:在限制矩陣上執行雙積分

myint = dblquad(lambda xa,xb: np.exp(-(xa-xb)**2),-np.inf,x1,lambda x: -np.inf, lambda x: x2) 

,並用於測試目的,我選擇x1和x2是說5和10這似乎是工作。

但實際上,我的x1 = [1,2,3,4,5]和x2 = [5,6,7,8,9],我希望對每個x1組合執行雙重積分和x2即矩陣。我想可以用2來做這個循環,但我認爲可能有更好的方法。

所以我的問題只是 - 如何在極限矩陣上執行雙重積分。

謝謝。


編輯:

我得到以下警告:

UserWarning: The maximum number of subdivisions (50) has been achieved. 
If increasing the limit yields no improvement it is advised to analyze 
the integrand in order to determine the difficulties. If the position of a 
local difficulty can be determined (singularity, discontinuity) one will 
probably gain from splitting up the interval and calling the integrator 
on the subranges. Perhaps a special-purpose integrator should be used. 

這是否意味着它不收斂?我不太瞭解這個消息。

當我的情節:

y = exp(-(x-5)^2) 

例如,它只是看起來像高斯曲線,因此不存在問題進行積分,對嗎?是因爲雙重積分的問題嗎?

謝謝。


編輯:

啊,我明白了。謝謝拉曼沙阿,我現在明白了這個問題。

+3

確定操作,這個積分收斂? – Stefan

+1

Stefan的評論很重要,使用'x1,x2 =(5,10)'給出警告,說明積分器不能收斂。考慮增加細分的數量。 – Hooked

+1

如果您只集成了高斯函數,則應該查看特殊函數(例如錯誤函數)來實現其功能。這可能會更快,更準確。編輯:我認爲你有不同的東西,因爲對於xa = xb,被積函數爲1會一直回到-infinity。 –

回答

1

使用itertools你可以創建一個限制迭代器來遍歷。這實質上是一個雙循環,但對於更多的擴展,你可以有與itertools.product輸入任意號碼,你沒有所有限制存儲在一次:

import numpy as np 
from scipy.integrate import dblquad 
import itertools 

f = lambda xa,xb: np.exp(-(xa-xb)**2) 
intg = lambda (x1,x2): dblquad(f,-np.inf,x1, 
           lambda x:-np.inf, 
           lambda x:x2) 


X1 = np.arange(1,6) 
X2 = np.arange(5,10) 
for limit in itertools.product(X1,X2): 
    print limit, intg(limit) 

如果需要更快的速度,你可以看看進入並行計算的multiprocessing模塊,因爲每個進程都是獨立的。

0

爲什麼不使用蟒蛇壓縮功能,從要當作輸入您的二重積分的每個元組喂準確的數值,然後用圖/適用於在該等離散對