使用高斯的数值双积分

发布于 2025-02-12 04:38:17 字数 1846 浏览 0 评论 0原文

我正在尝试通过python中的高斯 - legendre正交数字法解决双重积分,而无需使用任何具有数字方法的库。但是,当我具有集成限制时,我无法使算法起作用。这是我尝试的:

def integrate(a: float, b: float, n: int, f_xy: callable, upper_func: callable, lower_func: callable) -> float:

    if n < 1:
        raise("n < 1 is invalid.")

    w, t = get_wt(n)

    e1_x = (b-a)/2
    e2_x = (a+b)/2
    sum = 0

    for i in range(n):

        x_i = e1_x*t[i]+e2_x

        if type(upper_func) == int:
            d = upper_func
        else:
            d = upper_func(x_i)

        if type(lower_func) == int:
            c = lower_func
        else:
            c = lower_func(x_i)

        e1_y = (d - c)/2
        e2_y = (c + d)/2

        som = 0
        for j in range(n):
            y_i = e1_y*t[j]+e2_y
            som += w[j]*f_xy(x_i, y_i)

        sum += w[i]*som

    result = (1/4)*(b-a)*(d-c)*sum

    return result

get_wt()函数返回权重和节点。


[编辑]

我设法做到了。它只需要几个调整。

def double_integrate(a: float, b: float, n: int, f_xy: Union[Callable, float],
                     up_func: Union[Callable, float], low_func: Union[Callable, float]) -> float:
     
    w, t = get_wt(n)

    e1_x = (b-a)/2
    e2_x = (a+b)/2

    sum = 0

    for i in range(n):

        som = 0

        x_i = e1_x*t[i]+e2_x

        if not isfunction(up_func):
            d = up_func
        else:
            d = up_func(x_i)

        if not isfunction(low_func):
            c = low_func
        else:
            c = low_func(x_i)

        e1_y = (d - c)/2
        e2_y = (c + d)/2

        for j in range(n):
            y_i = e1_y*t[j]+e2_y

            if isfunction(f_xy):
                som += w[j]*f_xy(x_i, y_i)
            else:
                som += w[j]*f_xy

        sum += w[i]*e1_y*som

    result = e1_x*sum

    return result

I'm trying to solve double integrals through Gauss–Legendre Quadrature numeric method in python without using any library that has numeric methods. But i can't make the algorithm work when I have functions as the limits of integration. This is what I tried:

def integrate(a: float, b: float, n: int, f_xy: callable, upper_func: callable, lower_func: callable) -> float:

    if n < 1:
        raise("n < 1 is invalid.")

    w, t = get_wt(n)

    e1_x = (b-a)/2
    e2_x = (a+b)/2
    sum = 0

    for i in range(n):

        x_i = e1_x*t[i]+e2_x

        if type(upper_func) == int:
            d = upper_func
        else:
            d = upper_func(x_i)

        if type(lower_func) == int:
            c = lower_func
        else:
            c = lower_func(x_i)

        e1_y = (d - c)/2
        e2_y = (c + d)/2

        som = 0
        for j in range(n):
            y_i = e1_y*t[j]+e2_y
            som += w[j]*f_xy(x_i, y_i)

        sum += w[i]*som

    result = (1/4)*(b-a)*(d-c)*sum

    return result

where the get_wt() function returns the weights and nodes.


[Edit]

I managed to do it. It only needed a couple tweaks.

def double_integrate(a: float, b: float, n: int, f_xy: Union[Callable, float],
                     up_func: Union[Callable, float], low_func: Union[Callable, float]) -> float:
     
    w, t = get_wt(n)

    e1_x = (b-a)/2
    e2_x = (a+b)/2

    sum = 0

    for i in range(n):

        som = 0

        x_i = e1_x*t[i]+e2_x

        if not isfunction(up_func):
            d = up_func
        else:
            d = up_func(x_i)

        if not isfunction(low_func):
            c = low_func
        else:
            c = low_func(x_i)

        e1_y = (d - c)/2
        e2_y = (c + d)/2

        for j in range(n):
            y_i = e1_y*t[j]+e2_y

            if isfunction(f_xy):
                som += w[j]*f_xy(x_i, y_i)
            else:
                som += w[j]*f_xy

        sum += w[i]*e1_y*som

    result = e1_x*sum

    return result

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

靖瑶 2025-02-19 04:38:17

在您的算法中,该行

result = (1/4)*(b-a)*(d-c)*sum

不正确。它应与返回语句对齐;)。另外,您使用的公式有一个概念上的错误。考虑到您的算法有效,它只能在矩形上求解双重积分,这意味着变量cd是常数。但是,如果要在更复杂的区域进行集成,则变量cd是函数,(C + D)/2应该集成在外部积分中。
如果您想尝试实现它,我建议您更改此积分这个双重积分正方形[-1,1] x [-1,1]。这样做可能会使您了解如何实现此算法。

In your algorithm, the line

result = (1/4)*(b-a)*(d-c)*sum

is not correct. It should be aligned with the return statement ;). Also, the formula you are using has a conceptual mistake. Considering that your algorithm works, it can only solve double integrals over rectangles, meaning the variables c and d are constants. However, if you want integrate over a more complex region, where the variables c and d are functions, (c + d)/2 should be integrated in the outer integral.
If you wanna try to implement it, I would recommend you transform this integral this double integral into an integral over a square [-1,1]x[-1,1] by hand. Doing so might give you clarity on how to implement this algorithm.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文