我想將integrate.quad與陣列一起使用,但它回傳給我:“TypeError:只有size-1陣列可以轉換為Python標量”
我了解所需的第一個引數必須是標量。但是我想使用的引數來自一個依賴于一個引數并回傳一個陣列的函式,我無法解決問題:
這是我在 Python 中的腳本:
from scipy import integrate as intg
DfH_ref_jp = np.array([0,-393.52,-110.53,-74.87,-241.83,0]) *1000
n_jp = np.array([1.2,0.2,0.15,0.001,0.49,0.30])
Tref = 1298
Ta = 1310
def c_jp(T):
t = T/1000
XC = np.array([1,t,t**2,t**3,t**(-2)])
Mah = M_ah(T) # a matrix defined before
c = np.dot(Mah[:,:5],XC)
return c
H1_jp = n_jp * (DfH_ref_jp intg.quad(c_jp,Tref,Ta)[0]) # this where it doesn't work / [0] because intg.quad returns an array and I want the first value
所以我嘗試了一個回傳標量的函式:
def c_jp0(T):
t = T/1000
XC = np.array([1,t,t**2,t**3,t**(-2)])
Mah = M_ah(T)
c = np.dot(Mah[0,:5],XC)
return c
H1_jp = np.zeros(6, dtype=float)
H1_jp[0] = n_jp[0] * (DfH_ref_jp[0] intg.quad(c_jp0,Tref,Ta)[0])
它有效,但我不想指定 6 個函式:c_jp0 ... c_jp6。有誰知道怎么做?謝謝
uj5u.com熱心網友回復:
quad方法旨在僅在一個標量變數上積分標量函式。
如果您的積分只有一個積分變數,但取決于其他引數,您可以通過 params in 傳遞它們quad(func, a, b, params)。
如果您想整合多個變數,您可以使用nquad,在這種情況下,您必須為每個變數設定一個限制。
如果限制是恒定的,您可以直接傳遞它們,如果限制取決于其他積分變數,您必須將限制作為尚未積分的變數的函式傳遞。
例如,計算一個立方體的體積,一個角在原點,另一個角在點 (1,1,1)。
from scipy import integrate as intg
nquad(lambda x,y,z: 1.0, [(0,1), (0,1), (0,1)])
或頂點在 (0,0,1)、(-1,-1,0) 和 (1,1,0) 的方形底金字塔的體積。的限制x和y取決于z
from scipy import integrate as intg
intg.nquad(lambda x,y,z: 1.0, [lambda y,z: (z-1,1-z), lambda z: (z-1,1-z), (0,1)])
uj5u.com熱心網友回復:
您正在尋找quad_vec: https ://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.quad_vec.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/412390.html
標籤:
上一篇:Barrating不是函式
