R package for performing numerical integration using Gregory's Method. To install, run install.packages("GregoryQuadrature")
from your R console.
Gregory's Formula expresses an integral as a sum of equally spaced integrand values:
In contrast to Newton-Cotes approximation formulas, the values are equally weighted through the middle of the integral interval with corrections applied only at the end values. In this way, Gregory's Formula is an enhancement to the well-known Trapezoidal Rule, preserving spectral accuracy through the middle and decreasing error at the ends relative to the Trapezoidal Rule. Gregory's Method also avoids the evaluation of high-order derivatives required by Newton-Cotes formulas.
Gregory's Formula is exact for polynomials of degree
To use GregoryQuadtrature
evaluate the integrand at equal intervals, apply the weights provided by the Gregory_weights
method, and sum.
For example, to evaluate the integral of
# define number of abscissas
n_nodes = 11
# define order of evaluation
order = 8
# calculate step-size
h = 2/(n_nodes-1)
# evaluate the integrand
x = pracma::linspace(-1, 1, n_nodes)
f = exp(x)
# determine weights
w = Gregory_weights(n_nodes, h, order)
# calculate the integral
int = f %*% w
# calculate the error for known integrals
exact = exp(1) - exp(-1)
error = int - exact
print(error)
Ralston, Anthony. A First Course in Numerical Analysis. New York: McGraw-Hill, 1965.
Fornberg, B., Reeger, J.A. An improved Gregory-like method for 1-D quadrature. Numer. Math. 141, 1–19 (2019). https://doi-org.mines.idm.oclc.org/10.1007/s00211-018-0992-0