我最终想要绘制三个合并积分到其中每个积分评估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