Gauss Quadrature Method for Numerical Integration

by admin in , , on April 30, 2019

This code calculates the Numerical Integration of a given equation on a predefined interval using Gauss Quadrature Method. This codes uses variable step size and gives accurate and fast results.

Code Outputs:

  • Chart of Integration History
  • Chart of Residuals
  • Printed values of Final Iteration value, Accuracy and Number of Iterations.

Result is: 0.86271, Accuracy: 0.00026456, NO Iterations: 7

Input Requirements:

  • The equation you intended to integrate
  • Integration interval [a b].
  • predefined error (default 0.001).
  • Maximum number of iterations (default 1000).

About The Method (Gaussian quadrature):

Comparison between 2-point Gaussian and trapezoidal quadrature.

Comparison between 2-point Gaussian and trapezoidal quadrature. The blue line is the polynomial: y(x) = 7x^3 – 8x^2 – 3x + 3, whose integral in [−1, 1] is 2/3. The trapezoidal rule returns the integral of the orange dashed line, equal to y(-1) + y(1) = -10. The 2-point Gaussian quadrature rule returns the integral of the black dashed curve, equal to y(-sqrt(1/3)) + y(sqrt(1/3)) = 2/3. Such a result is exact, since the green region has the same area as the sum of the red regions.

In numerical analysis, a quadrature rule is an approximation of the definite integral of a function, usually stated as a weighted sum of function values at specified points within the domain of integration. (See numerical integration for more on quadrature rules.) An n-point Gaussian quadrature rule, named after Carl Friedrich Gauss, is a quadrature rule constructed to yield an exact result for polynomials of degree 2n − 1 or less by a suitable choice of the nodes xi and weights wi for i = 1, …, n. The most common domain of integration for such a rule is taken as [−1,1], so the rule is stated as

which is exact for polynomials of degree 2n-1 or less. This exact rule is known as the Gauss-Legendre quadrature rule. The quadrature rule will only be an accurate approximation to the integral above if f(x) is well-approximated by a polynomial of degree 2n-1 or less on [-1,1].

The Gauss-Legendre quadrature rule is not typically used for integrable functions with endpoint singularities. Instead, if the integrand can be written as

where g(x) is well-approximated by a low-degree polynomial, then alternative nodes x’i and weights w’i will usually give more accurate quadrature rules. These are known as Gauss-Jacobi quadrature rules, i.e.,

Common weights include 1/sqrt(1-x^2) (Chebyshev–Gauss) and sqrt(1-x^2). One may also want to integrate over semi-infinite (Gauss-Laguerre quadrature) and infinite intervals (Gauss–Hermite quadrature).

It can be shown (see Press, et al., or Stoer and Bulirsch) that the quadrature nodes xi are the roots of a polynomial belonging to a class of orthogonal polynomials (the class orthogonal with respect to a weighted inner-product). This is a key observation for computing Gauss quadrature nodes and weights.

Fundamental Theorem:

Let pn be a nontrivial polynomial of degree n such that

If we pick the n nodes xi to be the zeros of pn, then there exist n weights wi which make the Gauss-quadrature computed integral exact for all polynomials h(x) of degree 2n − 1 or less. Furthermore, all these nodes xi will lie in the open interval (ab) (Stoer & Bulirsch 2002, pp. 172–175).

The polynomial pn is said to be an orthogonal polynomial of degree n associated to the weight function ω(x). It is unique up to a constant normalization factor. The idea underlying the proof is that, because of its sufficiently low degree, h(x) can be divided by Pn(x) to produce a quotient q(x) of degree strictly lower than n, and a remainder r(x)of still lower degree, so that both will be orthogonal to Pn(x), by the defining property of Pn(x). Thus

Because of the choice of nodes xi, the corresponding relation

holds also. The exactness of the computed integral for h(x) then follows from corresponding exactness for polynomials of degree only n or less (as is r(x)).

General formula for the weights:

The weights can be expressed as

where ak is the coefficient of x^k in Pk(x). To prove this, note that using Lagrange interpolation one can express r(x) in terms of r(xi) as

because r(x) has degree less than n and is thus fixed by the values it attains at n different points. Multiplying both sides by ω(x) and integrating from a to b yields

The weights ωi are thus given by

This integral expression for ωi can be expressed in terms of the orthogonal polynomials Pn(x) and Pn-1(x) as follows.

We can write

where an is the coefficient of x^n in Pn(x). Taking the limit of x to xi yields using L’Hôpital’s rule

We can thus write the integral expression for the weights as:

In the integrand, writing

yields

provided k <= n, because

is a polynomial of degree k-1 which is then orthogonal to Pn(x). So, if q(x) is a polynomial of at most nth degree we have

We can evaluate the integral on the right hand side for

as follows. Because Pn(x)/(x-xi)  is a polynomial of degree n-1, we have

where s(x) is a polynomial of degree n-2. Since s(x) is orthogonal to Pn-1(x) we have

We can then write

The term in the brackets is a polynomial of degree n-2, which is therefore orthogonal to Pn-1(x). The integral can thus be written as

According to equation (2 wi), the weights are obtained by dividing this by P’n-1(xi) and that yields the expression in equation (1 wi). wi can also be expressed in terms of the orthogonal polynomials Pn(x) and now Pn+1(x). In the 3-term recurrence relation Pn+1(xi) = (a) Pn(xi) + (b) Pn-1(xi) the term with Pn(xi) vanishes, so Pn-1(xi) in Eq. (1) can be replaced by Pn+1(x)/b.

Proof that the weights are positive:

Consider the following polynomial of degree 2n-2

where, as above, the xj are the roots of the polynomial Pn(x). Clearly f(xj) = deltaij. Since the degree of f(x) is less than 2n-1, the Gaussian quadrature formula involving the weights and nodes obtained from Pn(x) applies. Since f(xi) = 0 for j not equal to i, we have

                       

Since both w(x) and f(x) are non-negative functions, it follows that wi > 0.

Computation of Gaussian quadrature rules:

There are many algorithms for computing the nodes xi and weights wi of Gaussian quadrature rules. The most popular are the Golub-Welsch algorithm requiring O(n2) operations, Newton’s method for solving Pn(x) = 0 using the three-term recurrence for evaluation requiring O(n2) operations, and asymptotic formulas for large n requiring O(n) operations.

Recurrence Relation:

Orthogonal polynomials Pr with (Pr, Ps) = 0 for r =/= s for a scalar product Pr = r and leading coefficient one (i.e. monic orthogonal polynomials) satisfy the recurrence relation

and scalar product defined

for r = 0,1,…,n-1  where n is the maximal degree which can be taken to be infinity, and where

First of all, the polynomials defined by the recurrence relation starting with P0(x) = 1 have leading coefficient one and correct degree. Given the starting point by P0, the orthogonality of Pr can be shown by induction. For r = s = 0 one has

Now if P0,P1,…,Pr are orthogonal, then also Pr+1, because in

all scalar products vanish except for the first one and the one where Ps meets the same orthogonal polynomial. Therefore,

However, if the scalar product satisfies (xf,g) = (f,xg)  (which is the case for Gaussian quadrature), the recurrence relation reduces to a three-term recurrence relation: For s < r-1, xps is a polynomial of degree less than or equal to r − 1. On the other hand,  is orthogonal to every polynomial of degree less than or equal to r − 1. Therefore, one has (xpr,ps) = (pr,xps) = 0 and ar,s = 0 for s < r − 1. The recurrence relation then simplifies to

or

(with the convention P-1(x) = 0 ) where

(the last because of

since xPr-1 differs from Pr by a degree less than r).

The Golub-Welsch Algorithm:

The three-term recurrence relation can be written in matrix form

where

en is the nth standard basis vector, i.e., en = [0,…,0,1]^T , and J is the so-called Jacobi matrix:

The zeros xj of the polynomials up to degree n, which are used as nodes for the Gaussian quadrature can be found by computing the eigenvalues of this tridiagonal matrix. This procedure is known as Golub–Welsch algorithm.

For computing the weights and nodes, it is preferable to consider the symmetric tridiagonal matrix big J with elements

J and big J are similar matrices and therefore have the same eigenvalues (the nodes). The weights can be computed from the corresponding eigenvectors: If phi^(j) is a normalized eigenvector (i.e., an eigenvector with euclidean norm equal to one) associated to the eigenvalue xj, the corresponding weight can be computed from the first component of this eigenvector, namely:

where  is the integral of the weight function

See, for instance, (Gil, Segura & Temme 2007) for further details.

Error Estimates:

The error of a Gaussian quadrature rule can be stated as follows (Stoer & Bulirsch 2002, Thm 3.6.24). For an integrand which has 2n continuous derivatives,

for some ξ in (ab), where pn is the monic (i.e. the leading coefficient is 1) orthogonal polynomial of degree n and where

In the important special case of ω(x) = 1, we have the error estimate (Kahaner, Moler & Nash 1989, §5.2)

Stoer and Bulirsch remark that this error estimate is inconvenient in practice, since it may be difficult to estimate the order 2n derivative, and furthermore the actual error may be much less than a bound established by the derivative. Another approach is to use two Gaussian quadrature rules of different orders, and to estimate the error as the difference between the two results. For this purpose, Gauss–Kronrod quadrature rules can be useful.

References:

[1] Implementing an Accurate Generalized Gaussian Quadrature Solution to Find the Elastic Field in a Homogeneous Anisotropic Media

[2] Abramowitz, Milton; Stegun, Irene Ann, eds. (1983) [June 1964]. ‘Chapter 25.4, Integration’. Handbook of Mathematical Functions with Formulas, Graphs, and Mathematical Tables. Applied Mathematics Series. 55 (Ninth reprint with additional corrections of tenth original printing with corrections (December 1972); first ed.). Washington D.C.; New York: United States Department of Commerce, National Bureau of Standards; Dover Publications. ISBN 978-0-486-61272-0. LCCN 64-60036. MR 0167642. LCCN 65-12253.

[3] Anderson, Donald G. (1965). ‘Gaussian quadrature formulae for ‘. Math. Comp19 (91): 477–481. doi:10.1090/s0025-5718-1965-0178569-1.

[4] Golub, Gene H.; Welsch, John H. (1969), ‘Calculation of Gauss Quadrature Rules’, Mathematics of Computation23 (106): 221–230, doi:10.1090/S0025-5718-69-99647-1, JSTOR 2004418

[5] Gautschi, Walter (1968). ‘Construction of Gauss–Christoffel Quadrature Formulas’. Math. Comp22 (102). pp. 251–270. doi:10.1090/S0025-5718-1968-0228171-0. MR 0228171.

[6] Gautschi, Walter (1970). ‘On the construction of Gaussian quadrature rules from modified moments’. Math. Comp24. pp. 245–260. doi:10.1090/S0025-5718-1970-0285117-6. MR 0285177.

[7] Piessens, R. (1971). ‘Gaussian quadrature formulas for the numerical integration of Bromwich’s integral and the inversion of the laplace transform’. J. Eng. Math5 (1). pp. 1–9. Bibcode:1971JEnMa…5….1P. doi:10.1007/BF01535429.

[8] Danloy, Bernard (1973). ‘Numerical construction of Gaussian quadrature formulas for and ‘. Math. Comp27 (124). pp. 861–869. doi:10.1090/S0025-5718-1973-0331730-X. MR 0331730.

2 Sales

Share Now!

Release Information

  • Price
    :

    $4.99

  • Released
    :

    April 30, 2019

  • Last Updated
    :

    May 28, 2019

Share Your Valuable Opinions