首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Scipy.integrate浮点误差

Scipy.integrate浮点误差
EN

Stack Overflow用户
提问于 2015-12-10 05:56:10
回答 1查看 1.4K关注 0票数 1

我试着集成这个讨厌的积分,它由两个函数组成。

代码语言:javascript
复制
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是一个物体的半径,与它的质量成正比。质量由一个结构化的数组来给出。我不确定我是否用正确的积分命令来评价所有的群众。只有数组有一组值。

如果你还需要更多的信息,请告诉我,我很乐意分享我所能分享的一切。如有任何建议,将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-10 19:15:50

integrand.quad()计算1.积分

阅读文档

  • integrand.quad返回长度2元组,第一个值保持积分的估计值。确保只得到第一个元素,而不是整个元组。
  • 你不需要兰达,就能通过极限。lambda用于代替integrand函数,而不是限制。
  • args参数用于将附加参数传递给integrand,因此您不需要传递k,因为通过这个参数进行集成。您将需要通过w,正如我接下来所展示的。

2. TypeError:浮点数是必需的

  • 您需要函数W(kR)来返回一个浮点,以使集成工作。让我们循环遍历每个值并计算积分。
  • 记住,您只需要传递函数将使用的参数。sig(R)不使用RW(kR)不使用kR
  • 只需计算和存储一次傅里叶变换,因为它不依赖于积分。没有必要每次调用W()
  • 为了解决这种类型误差,我们将从傅里叶变换的数组循环,并计算积分。 fourier_transform = 3*(np.sin(k*R)-k*R*np.cos(k*R))/(k*R)**3 def sig():I_one = np.zeros(len(fourier_transform)) def integrand(k,w):返回k**2*p_func(k)*w for i,w in枚举(Fourier_transform):I_onei,err = integrate.quad(integrand,0.0,np.inf,打印I_onei返回((1.0/(2.0*np.pi**2)) * I_one)**0.5

3.变量和函数的命名约定()

看看答案,这里。尝试使用有意义的变量和函数名,以尽量减少错误和混淆。所有局部变量和函数都应该是小写。常量的全局变量应该是大写。这是我的尝试,但您将更好地了解这些变量和函数的含义。

代码语言:javascript
复制
#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.5

def integrand(k, P) 5.

虽然integrand()是嵌套的,但它仍然可以在全局命名空间中搜索函数P,所以不要传递它。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34194707

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档