如何集成复杂(不是真正的复杂定义)函数

问题描述 投票:0回答:1

我有一个球体周围压力分布的怪物表达式,表达式是Re [A]是预定义系数的实部,P是legendre多项式,j和n是球形Bessel和Neumann函数。我希望将它整合到theta上,所以我为上面的等式定义了一个函数,如下所示

def P_rms_calc(k,a,n_max,theta): # Return the P_rms function

# Obtain Coefficients
A_m = A_m_coeff(k,a,P_a,l,z0,n_max)

P_t = []
for m in range(n_max):
    for l in range(m+1):
        P_t.append(0.5*numpy.real(A_m[l])*numpy.real(A_m[m-l])*(legendre(l)(numpy.cos(theta)))*(legendre(m-l)(numpy.cos(theta)))*((spherical_jn(l,k*r)*spherical_jn(m-l,k*r))+
                                                                                                                            (spherical_yn(l,k*r)*spherical_yn(m-l,k*r))))
    P_rms = numpy.sqrt(sum(P_t))
return P_rms

但是,当我尝试使用scipy.integrate.quad进行集成时,

a0, err = quad(P_rms_calc(k,a,n_max,theta),-numpy.pi,numpy.pi)

它给出'错误:quad:第一个参数不可调用'。如果我不给出这样的函数的参数,

a0, err = (1/(2*numpy.pi))*quad(P_rms_calc,-numpy.pi,numpy.pi)

它给'TypeError:P_rms_calc()需要8个参数(给定1个)'

我是否在使用集成工具quad时遗漏了一些简单的东西?如果没有,有没有更好的方法来尝试和集成这个表达式?请随意推荐一种更有效的方法来定义rms压力表达式。作为参考,我只是从m = 0到6左右求和,所以使用for循环计算时间并不可怕。谢谢你的帮助!

python scipy acoustics
1个回答
0
投票

使用args参数传递被积函数的额外参数:

>>> def func(x, a, b):
...    return a * x**b 
... 
>>> quad(func, 0, 1, args=(1., 2.))
(0.33333333333333337, 3.700743415417189e-15)
© www.soinside.com 2019 - 2024. All rights reserved.