您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

numpy中高斯-勒让德正交的不同间隔

numpy中高斯-勒让德正交的不同间隔

更改间隔,请使用以下方法将x值从[-1,1]转换为[a,b]:

t = 0.5*(x + 1)*(b - a) + a

然后将正交公式缩放为(b-a)/ 2:

gauss = sum(w * f(t)) * 0.5*(b - a)

这是您的示例的修改版本:

import numpy as np
from scipy import integrate

# Define function and interval
a = 0.0
b = np.pi/2
f = lambda x: np.cos(x)

# Gauss-Legendre (default interval is [-1, 1])
deg = 6
x, w = np.polynomial.legendre.leggauss(deg)
# Translate x values from the interval [-1, 1] to [a, b]
t = 0.5*(x + 1)*(b - a) + a
gauss = sum(w * f(t)) * 0.5*(b - a)

# For comparison
quad, quad_err = integrate.quad(f, a, b)

print 'The QUADPACK solution: {0:.12} with error: {1:.12}'.format(quad, quad_err)
print 'Gauss-Legendre solution: {0:.12}'.format(gauss)
print 'Difference between QUADPACK and Gauss-Legendre: ', abs(gauss - quad)

它打印:

QUADPACK解决方案:1.0,错误:1.11022302463e-14
高斯-勒根德雷解决方案:1.0
QUADPACK和Gauss-Legendre之间的差异:4.62963001269e-14
其他 2022/1/1 18:42:03 有403人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶