我目前正在嘗試使用 SciPy 解決這個積分:

我首先被建議使用插值,我嘗試過但由于某種原因無法弄清楚,但可能是一個很好的方法。我發現了這篇關于使用的帖子np.vectorize,我認為它可能仍然有效,但我收到了一個錯誤。這是我到目前為止撰寫的代碼(還要注意 n 和 n,eq 不是索引,它們只是變數名):
import numpy as np
from scipy import integrate
def K(x): #This is a function in the integral.
b = 0.252
return b*(((4/(x**3)) (3/(x**2)) 1/x) (4/(x**3) 1/(x**2))*np.exp(-x))
def Xntot_integrand(x,z): #Defining the integrand
Xneq_x = (1 np.exp(x))**(-1) #This is the term outside the integral and squared within it.
return Xneq_x(x)**2 * np.exp(K(z) - K(x)) * np.exp(x)
Xntot_integrand = np.vectorize(Xntot_integrand)
def Xntot_integrated(x,z):
return quad(Xntot_integrand, 0, z)
Xntot_integrated=np.vectorize(Xntot_integrated)
T_narrow = np.linspace(1,0.01,100) #Narrow T range from 1 to 0.01 MeV
z_narrow = Q/T_narrow
final_integrated_Xneq = Xntot_integrated(z_narrow)
我得到一個錯誤,我在呼叫時缺少位置引數Xntot_integrated(這是有道理的,我認為它仍然在兩個變數 x 和 z 中)。
所以我想問題出在我使用的地方,quad()因為在它集成后, x 應該消失。有什么建議嗎?我應該使用制表/插值法嗎?
uj5u.com熱心網友回復:
您需要使用args關鍵字引數 ofintegrate.quad將其他輸入傳遞給函式,因此它看起來像這樣:
def Xntot_integrated(z):
return integrate.quad(Xntot_integrand, 0, z, args=(z,))
注意這里x不是積分函式的輸入,只有z,被積函式的第一個輸入是積分變數,任何額外的資訊都通過args=(z,)元組傳遞。
或者,您可以定義一個從背景關系中知道 z 并且僅將集成變數作為輸入的包裝器:
def Xntot_integrated(z):
def integrand(x):return Xntot_integrand(x,z)
return integrate.quad(integrand, 0, z)
但大多數采用函式的 API 通常都有一個關鍵字引數來指定這些輸入。(threading.Thread想到了。)
你也Xneq_x應該是一個函式本身,因為你不小心在你的被積函式中使用了它(它現在只是一個值),無論如何你都需要在集成之外使用它:)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/321527.html
上一篇:在javascript中,如何檢查陣列的最后一個元素是否為數字。請注意,最后一個元素可以有數字或字串取決于
下一篇:陣列字串中有多少這個單詞
