如何在NumPy包numpy.polynomial.legendre.leggauss以外的间隔上使用[-1, 1]?
下面的示例将scipy.integrate.quad与区间[-1, 1]上的Gauss方法进行比较。
import numpy as np
from scipy import integrate
# Define function and interval
a = -1.
b = 1.
f = lambda x: np.cos(x)
# Gauss-Legendre (default interval is [-1, 1])
deg = 6
x, w = np.polynomial.legendre.leggauss(deg)
gauss = sum(w * f(x))
# 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)输出:
The QUADPACK solution: 1.68294196962 with error: 1.86844092378e-14
Gauss-Legendre solution: 1.68294196961
Difference between QUADPACK and Gauss-Legendre: 1.51301193796e-12发布于 2017-03-22 09:34:36
四边形 (我的一个小项目)是一种更简单的语法:
import numpy
import quadpy
out, err = quadpy.quad(numpy.cos, 1.1, 1.2)https://stackoverflow.com/questions/33457880
复制相似问题