在圆形区域中对函数进行积分,而不是收敛到中心的值

Integrating a function in a circle region, not converging to the value in the center

提问人:Martim Pinto Paiva 提问时间:10/10/2023 最后编辑:Martim Pinto Paiva 更新时间:10/10/2023 访问量:34

问:

我有一个函数,我们称之为.目前,我没有明确的公式,因为它是由一堆其他函数定义的,但它只使用算术和三角运算,所以我期待连续性和所有这些良好的属性。u(x,y)

我试图使用一种数值方法来计算一个圆的平均值(通过将积分除以区域的面积),给定它的中心和半径,以及一个正方形,再次给定它的中心和边。u

这是我当前使用的代码(Python):

# Integral in a column, x constant, y in ]y_c - l/2, y_c + l/2[, using n points:
def F(x, y_c, l, n):
    def f(b):
        return u(x,b)

    h = l / n

    y0 = y_c - l / 2
    yn = y_c + l / 2
    s = 0

    for i in range(1, n):
        yi = y0 + h * i
        s += f(yi)

    return h * (f(y0) + f(yn)) / 2 + h * s


# Integral in the circle of center (x_c, y_c) and radius r, using aprox n points:
def int_circ(x_c, y_c, r, n):
    def f(x, l):
        return F(x, y_c, l, int(np.sqrt(n)))

    h = 2 * r / np.sqrt(n)
    x0 = x_c - r
    xn = x_c + r
    s = 0

    for i in range(1, int(np.sqrt(n))):
        xi = x0 + h * i
        li = 2 * np.sqrt(r ** 2 - (xi - x_c) ** 2)
        s += f(xi, li)

    l = 2 * np.sqrt(r ** 2 - (r - h / 4) ** 2)

    return h / 2 * (f(x0 + h / 2, l) + f(xn - h / 2, l)) + h * s


# Integral in the square of center (x_c, y_c) and side 2r, using aprox n points:
def int_sqr(x_c, y_c, r, n):
    def f(x):
        return F(x, y_c, 2 * r, int(np.sqrt(n)))

    h = 2 * r / np.sqrt(n)
    x0 = x_c - r
    xn = x_c + r
    s = 0

    for i in range(1, int(np.sqrt(n))):
        xi = x0 + h * i
        s += f(xi)

    return h * (f(x0) + f(xn)) / 2 + h * s

在分析结果时,我可以看到,当我缩小正方形的边时,它接近中心的函数值,正如预期的那样。然而,对于圆圈,它收敛到一个不同的值,相对误差大约超过预期值,当 n=10000 时。当我用常量函数 =1 替换时,问题仍然存在。´10^-4u

注意:增加 n 确实可以带来更好的近似值,但在我看来,应该有更好的方法来做到这一点。

我尝试了几种不同的圆积分算法,但似乎都以同样的方式失败了,我意识到圆内的某些区域没有被考虑在内,或者圆外的某些区域被考虑在内。这个是最接近的。

我怎样才能让它更好地接近中心的价值?

谢谢!

Python 数值积分

评论

0赞 itprorh66 10/10/2023
欢迎使用 Stack Overflow。我们不提供一般的调试服务。我们可以协助解决具体问题。请使用以下有关调试代码的参考资料,将问题范围缩小到特定问题。如何调试小程序Python 程序员的六种调试技巧
0赞 D.L 10/10/2023
u未定义。还需要.import numpy as np
0赞 Martim Pinto Paiva 10/10/2023
我没有故意为你包括定义,因为它会占用大量空间并且不会影响结果,因为即使我把 ,圆的积分(除以面积后)并不完美,而平方是完美的。重点是积分算法。我很抱歉忘记提到numpy速记。def u(x,y): return 1

答: 暂无答案