我试着集成这个讨厌的积分,它由两个函数组成。
import matplotlib.pyplot as plt
import numpy as np
import os
import scipy.integrate as integrate
path = '/users/username/Desktop/untitled folder/python files/document/'
os.chdir( path )
data = np.load('msii_phasespace.npy',mmap_mode='r')
# data.size: 167197
# data.shape: (167197,)
# data.dtype: dtype([('x', '<f4'), ('y', '<f4'), ('z', '<f4'),
# ('velx', '<f4'), ('vely', '<f4'), ('velz', '<f4'), ('m200', '<f4')])
## Constant
rho_m = 3.3e-14
M = data['x'] Mass of dark matter haloes
R = ((3*M)/(rho_m*4*(3.14)))**(1.0/3.0) # Km // Radius of sphere
k = 0.001 # Mpc h^-1 // Wave Dispersion relation
delt_c = 1.686
h = 0.73 # km s^-1 Mpc^-1
e = 2.718281 # Eulers number
T_CMB = 2.725
Omega_m = 0.27
kR = k*R
def T(k):
q = k/((Omega_m)*h**2)*((T_CMB)/27)**2
L = np.log(e+1.84*q)
C = 14.4 + 325/(1+60.5*q**1.11)
return L/(L+C*q**2)
def P(k):
A = 0.75
n = 0.95
return A*k**n*T(k)**2
def W(kR): # Fourier Transfrom in the top hat function
return 3*(np.sin(k*R)-k*R*np.cos(k*R))/(k*R)**3
def sig(R):
def integrand(k,P,W):
return k**2*P(k)*W(kR)**2
I1 = integrate.quad(integrand, lambda k: 0.0, lambda k: np.Inf, args=(k,))
return ((1.0/(2.0*np.pi**2)) * I1)**0.5打印出的sig(R)给我TypeError: a float is require。
我需要指出,R是一个物体的半径,与它的质量成正比。质量由一个结构化的数组来给出。我不确定我是否用正确的积分命令来评价所有的群众。只有数组有一组值。
如果你还需要更多的信息,请告诉我,我很乐意分享我所能分享的一切。如有任何建议,将不胜感激。
发布于 2015-12-10 19:15:50
用integrand.quad()计算1.积分
阅读文档
args参数用于将附加参数传递给integrand,因此您不需要传递k,因为通过这个参数进行集成。您将需要通过w,正如我接下来所展示的。2. TypeError:浮点数是必需的
W(kR)来返回一个浮点,以使集成工作。让我们循环遍历每个值并计算积分。sig(R)不使用R,W(kR)不使用kR。W()。3.变量和函数的命名约定()
看看答案,这里。尝试使用有意义的变量和函数名,以尽量减少错误和混淆。所有局部变量和函数都应该是小写。常量的全局变量应该是大写。这是我的尝试,但您将更好地了解这些变量和函数的含义。
#global constants should be capitalized
RHO_M = 3.3e-14
H = 0.73 # km s^-1 Mpc^-1
EULERS_NUM = 2.718281 # Eulers number
T_CMB = 2.725
OMEGA_M = 0.27
#nonconstant variables should be lower case
mass = data['x'] #Mass of dark matter haloes
radius = ((3*mass)/(RHO_M*4*(3.14)))**(1.0/3.0) # Km // Radius of sphere
#not sure if this is a constant???
mpc_h = 0.001 # Mpc h^-1 // Wave Dispersion relation
mpc_radius = mpc_h*radius
#this stays constant for all integrations so let's just compute it once
fourier_transform = 3*(np.sin(mpc_h*radius)*mpc_h*radius*np.cos(mpc_h*radius))/(mpc_h*radius)**3
def t_calc(k):
q = k/((OMEGA_M)*H**2)*((T_CMB)/27)**2
#I didn't change this because lower case L creates confusion
L = np.log(EULERS_NUM+1.84*q)
c = 14.4 + 325/(1+60.5*q**1.11)
return L/(L+c*q**2)
def p_calc(k):
a = 0.75
n = 0.95
return a*k**n*t_calc(k)**2
def sig():
I_one = np.zeros(len(fourier_transform))
def integrand(k, w):
return k**2*p_func(k)*w
for i, w in enumerate(fourier_transform):
I_one[i], err = integrate.quad(integrand, 0.0, np.inf, args = w)
print I_one[i]
return ((1.0/(2.0*np.pi**2)) * I_one)**0.5def integrand(k, P) 5.
虽然integrand()是嵌套的,但它仍然可以在全局命名空间中搜索函数P,所以不要传递它。
https://stackoverflow.com/questions/34194707
复制相似问题