【发布时间】:2021-12-04 23:00:58
【问题描述】:
我目前正在尝试使用 SciPy 解决这个积分问题:
我首先被建议使用插值,我尝试过但由于某种原因无法弄清楚,但可能是一个好方法。我发现 this post 关于使用 np.vectorize 并且我认为它可能仍然有效,但我遇到了一个错误。这是我迄今为止编写的代码(还要注意 n 和 n,eq 不是索引,它们只是变量名):
import numpy as np
from scipy import integrate
def K(x): #This is a function in the integral.
b = 0.252
return b*(((4/(x**3))+(3/(x**2))+1/x) + (4/(x**3) + 1/(x**2))*np.exp(-x))
def Xntot_integrand(x,z): #Defining the integrand
Xneq_x = (1+np.exp(x))**(-1) #This is the term outside the integral and squared within it.
return Xneq_x(x)**2 * np.exp(K(z) - K(x)) * np.exp(x)
Xntot_integrand = np.vectorize(Xntot_integrand)
def Xntot_integrated(x,z):
return quad(Xntot_integrand, 0, z)
Xntot_integrated=np.vectorize(Xntot_integrated)
T_narrow = np.linspace(1,0.01,100) #Narrow T range from 1 to 0.01 MeV
z_narrow = Q/T_narrow
final_integrated_Xneq = Xntot_integrated(z_narrow)
当我调用Xntot_integrated 时,我收到了一个错误,即我缺少一个位置参数(这是有道理的,我认为它仍然在两个变量 x 和 z 中)。
所以我想问题出在我使用quad() 的地方,因为在它被集成之后,x 应该会消失。有什么建议吗?我应该改用制表/插值吗?
【问题讨论】:
-
np.vectorize(Xntot_integrand)不应该是Xntot_integrand = np.vectorize(Xntot_integrand)吗? -
好点,我改变了它,不幸的是,它似乎仍然有同样的问题。
标签: python arrays integration