使用Python中的fsolve和Understancess软件包求解带不确定性的积分方程

2024-10-02 04:23:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一些不确定的变量,这些是

w_m = u.ufloat(0.1430, 0.0011)
z_rec = u.ufloat(1089.92, 0.25)
theta_srec = u.ufloat(0.0104110,  0.0000031)
r_srec = u.ufloat(144.43, 0.26)

还有一些常量

c = 299792.458  # speed of light in [km/s]
N_eff = 3.046
w_r = 2.469 * 10**(-5) * (1 + (7/8)*(4/11)**(4/3) * N_eff)

我的问题是我需要解一个由这个函数定义的积分

def D_zrec(z):
        return (c/100) / sqrt(w_m * (1+z)**3 + w_r * (1+z)**4 + (h**2 - w_m - w_r))

该函数是针对dz计算的,但它还包含我们需要找到的未知h,并具有相应的不确定性。所以我需要写一个代码来找到h

这是我的全部代码

    from numpy import sqrt, vectorize
from scipy.integrate import quad
import uncertainties as u
from uncertainties.umath import *
from scipy.optimize import fsolve


####  Important Parameters #####
c = 299792.458  # speed of light in [km/s]
N_eff = 3.046
w_r = 2.469 * 10**(-5) * (1 + (7/8)*(4/11)**(4/3) * N_eff)

w_m = u.ufloat(0.1430, 0.0011)
z_rec = u.ufloat(1089.92, 0.25)
theta_srec = u.ufloat(0.0104110,  0.0000031)
r_srec = u.ufloat(144.43, 0.26)

D_zrec_true = r_srec / theta_srec

@u.wrap
def D_zrec_finder(h, w_m, z_rec, D_zrec_true):
    def D_zrec(z):
        return (c/100) / sqrt(w_m * (1+z)**3 + w_r * (1+z)**4 + (h**2 - w_m - w_r))
    result, error = quad(D_zrec, 0, z_rec)
    return D_zrec_true - result

def h0_finder(w_m, z_rec, D_zrec_true):
    vfunc = vectorize(D_zrec_finder)
    sol = fsolve(vfunc, u.ufloat(0.6728, 0.01), args=(w_m, z_rec, D_zrec_true))[0]
    return sol

print(h0_finder(w_m, z_rec, D_zrec_true))

总之,我有一个名为D_zrec的积分,它是z的函数,但也包含一个未知数h,我们需要使用fsolve来找到它

我发现了3个可能对编码人员有用的站点。如果你想帮忙,请看一下

https://kitchingroup.cheme.cmu.edu/blog/2013/03/07/Another-approach-to-error-propagation/

https://kitchingroup.cheme.cmu.edu/blog/2013/07/10/Uncertainty-in-an-integral-equation/

https://kitchingroup.cheme.cmu.edu/blog/2013/01/23/Solving-integral-equations-with-fsolve/

我看了他们写我的代码,但没有运气

谢谢你的帮助


Tags: 函数infromimporttruereturnfinderdef

热门问题