我最終想要繪製三個合併積分到其中每個積分評估z=np.linspace(1e+9,0)
的Python - 功能不產生相同的二維陣列
import numpy as np
import matplotlib.pyplab as plt
import scipy as sp
import scipy.integrate as integrate
z = np.linspace(1e+9, 0, 1000)
mass = 1000
Omega_m0 = 0.3
Omega_L0 = 0.7
h = 0.7
def FreeStreamLength(z, mass, Omega_m0, Omega_L0, h):
kb = 8.617e-5 ## kev K^-1
c = 3e+5 ## km/s
T0 = 2.7 ## K
T_uni = mass/kb
a = 1./(z+1.)
z_nr = T_uni/T0 - 1. ## redshift at non relativistic
a_nr = 1/(z_nr + 1.) ## scale factor at non relativistic
Omega_r0 = (4.2e-5)/h/h
a_eq = Omega_r0/Omega_m0
z_eq = 1/a_eq - 1
a1 = a[a <= a_nr] ## scale factor before particles become non-relativistic
a2 = a[a_nr <= a.all() <= a_eq]
a3 = a[a_eq <= a]
integrand = lambda x: 1./x/x/np.sqrt(Omega_m0/x/x/x + Omega_L0)
epoch_nr = [ c/H0 *integrate.quad(integrand, 0, i)[0] for i in a1]
epoch_nreq = [c/H0/a_nr * integrate.quad(integrand, a2, a_eq)[0] ]
epoch_eq = [c/H0/a_eq * integrate.quad(integrand, i, 1)[0] for i in a3]
return epoch_nr + epoch_nreq + epoch_eq
z
應該通過a
陣列的特定部分,如此有效地,這些值應該相互關聯。
對於return
行我組合了所有的列表來爲我的函數創建這個新的數組。
FSL = FreeStreamLength(z, mass, Omega_m0, Omega_L0, h)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(z, FSL, color="blue", label=r"$z=0$")
plt.show()
我與ValueError: x and y must have same first dimension
爲什麼我的新名單不與名單,我以前還搭配回來了?
我相信它必須與我如何從傳遞的數組中迭代元素,然後才能在函數中定義被積函數。
我無法運行腳本,因爲沒有定義'T_uni'和'T0' – gsmafra
@gsmafra對不起,我編輯了導致它的不相關部分。所以再試一次。 – DarthLazar
我仍然不能運行你的代碼,而沒有做出猜測和更正! – hpaulj