如何定义 d(sqrt(x)) 上的积分

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

我坚持了如何定义如下所示的函数。 特别是我想知道如何定义下面等式中的黄色框。

image 1

下面是我的脚本。


C0 = 0.05

def lin_equation(t, Ds):
    
    def integrand1(td):
        return Cs_func(t - td)
    

    def integrand2(td):
        return Cs_func(td)

    r = r_func(t)
    
    integral1, _ = quad(integrand1, 0, t**0.5)
    integral2, _ = quad(integrand2, 0, t)
    
    term1 = 2 * (Ds / np.pi)**0.5 * (C0 * np.sqrt(t) - integral1)
    term2 = (Ds / r) * (C0 * t - integral2)
    
    return term1 + term2

如果我运行下面的代码,

Ds = 1e-10

t_lin = np.linspace(min(t_data), max(t_data), 100)
lin_eq = lin_equation(t_lin, Ds)

它给了我如下的错误

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[455], line 4
      1 Ds = 1e-10
      3 t_lin = np.linspace(min(t_data), max(t_data), 100)
----> 4 lin_eq = lin_equation(t_lin, Ds)
      6 print(lin_eq)

Cell In[454], line 12, in lin_equation(t, Ds)
      8     return Cs_func(td)
     10 r = r_func(t)
---> 12 integral1, _ = quad(integrand1, 0, t**0.5)
     13 integral2, _ = quad(integrand2, 0, t)
     15 term1 = 2 * (Ds / np.pi)**0.5 * (C0 * np.sqrt(t) - integral1)

File C:\ProgramData\anaconda3\Lib\site-packages\scipy\integrate\_quadpack_py.py:438, in quad(func, a, b, args, full_output, epsabs, epsrel, limit, points, weight, wvar, wopts, maxp1, limlst, complex_func)
    435     args = (args,)
    437 # check the limits of integration: \int_a^b, expect a < b
--> 438 flip, a, b = b < a, min(a, b), max(a, b)
    440 if complex_func:
    441     def imfunc(x, *args):

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

所以我有两个问题。

首先,我上面的代码似乎只是积分 dt,而不是积分 d(t^0.5)。我该如何详细说明这一点?

第二,我该如何修复该错误?

谢谢你

python scipy integral
1个回答
0
投票

首先,我上面的代码似乎只是积分 dt,而不是积分 d(t^0.5)。

既然你的方程中有 tt_d ,我想你的意思是你的代码只是微分 d(t_d) 的积分,而不是 d(sqrt(t_d) )。是的。要纠正这个问题,请使用链式法则:

将其代入您的被积函数。根据积分限制的定义方式(w.r.t. t_d 或 sqrt(t_d)?),可能需要进行其他调整,但这些数学问题可以通过查看 Riemann-Stieltjes Integral 和/或使用Mathematics Stack Exchange

第二,我该如何修复该错误?

正如评论中提到的,

scipy.integrate.quad
预计积分的标量限制。您可以通过一次超过一项积分限制来避免错误。也就是说,您不能简单地传递数组
t_lin
,因为
quad
没有被矢量化来处理它;你需要循环遍历元素。

© www.soinside.com 2019 - 2024. All rights reserved.