使用高斯的数值双积分
我正在尝试通过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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的算法中,该行
不正确。它应与返回语句对齐;)。另外,您使用的公式有一个概念上的错误。考虑到您的算法有效,它只能在矩形上求解双重积分,这意味着变量
c
和d
是常数。但是,如果要在更复杂的区域进行集成,则变量c
和d
是函数,(C + D)/2
应该集成在外部积分中。如果您想尝试实现它,我建议您更改此积分这个双重积分正方形[-1,1] x [-1,1]。这样做可能会使您了解如何实现此算法。
In your algorithm, the line
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
andd
are constants. However, if you want integrate over a more complex region, where the variablesc
andd
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.