Ch19.5 Part 2: Gauss Numerical Integration

Ch19.5: Gauss Numerical Integration

  • This method is typically referred to as Gauss quadrature.
  • Gauss quadrature achieves high accuracy for very few nodes.
    • Nodes and weights strategically computed.
  • Greater accuracy and fewer flops than for Newton-Cotes.
    • Left figure: 2-Point Trapezoid Rule
    • Right figure: 2-Point Gauss Quadrature Rule

Humor



Sometimes I tuck my knees into my chest and lean forward.

That's just how I roll.

History of Quadrature

  • Greek method for finding area of nonstandard region was to geometrically construct a square having same area.
  • This is where the term quadrature comes from.

N-Point Riemann Sums

  • On [a, b], a typical numerical integration method looks like

\[ \small{\int_{a}^b f(x)dx \cong \sum_{k=1}^N c_k f(x_k), \,\, c_k = \Delta x = \frac{b-a}{N} } \]

Two-Point Gauss Quadrature

On [-1, 1] with two nodes, GQ uses non-endpoint trapezoid:

\[ \small{ \begin{aligned} c_1 &= c_2 = 1, \, \, x_1 = -\frac{1}{\sqrt{3}}, \, \, x_2 = \frac{1}{\sqrt{3}} \\ \int_{a}^b f(x)dx &\cong \sum_{k=1}^n c_k f(x_k) = 1 \cdot f(x_1) + 1 \cdot f(x_2) \end{aligned} } \]

Example 1: Two-Point Gauss Quadrature

For this example, \( f(x) = 1.7x^3 + 3x^2 + 2.4x +4 \)

www.desmos.com/calculator/udbnhupofh

Example 1: Trapezoid Rule

For this example, \( f(x) = 1.7x^3 + 3x^2 + 2.4x +4 \)

www.desmos.com/calculator/uiuvdb8nsg

Two-Point GQ Derivation

For \( n=2 \) data points, want GQ to be exact for polynomials of degree \( 2n-1 = 2(2)-1 = 3 \) or less on [-1,1]:

\[ \small{ \int_{-1}^1 P_3(x)dx = c_1 \cdot P_3(x_1) + c_2 \cdot P_3(x_2) } \]

Two-Point GQ Derivation

  • For \( n=2 \) data points, Gauss quadrature is exact for polynomials of degree \( 2n-1 = 3 \) or less on [-1,1].
  • For this example, \( f(x) = 1.7x^3 + 3x^2 + 2.4x +4 \).

N-Point Gauss Quadrature

  • For \( N \) data points, Gauss quadrature is exact for polynomials of degree \( 2N-1 \) or less on [-1,1].
  • The \( x_k \) are also the roots of Legendre polynomials.

Python: Legendre Polynomials

import numpy as np
import matplotlib.pyplot as plt
from scipy.special import legendre

for n in range(0,6):
    Pn = legendre(n)
    x = np.linspace(-1,1,100)
    y = Pn(x)
    plt.plot(x, y)

plt.xlabel('x')
plt.legend(('P0','P1','P2','P3','P4','P5'),loc=0)
plt.title('Legendre Polynomials')
plt.show()

Python: Legendre Polynomials Plot

plot of chunk unnamed-chunk-3

Python: Nodes & Weights

On [-1, 1] with two nodes, GQ uses non-endpoint trapezoid:

\[ \small{ \begin{aligned} c_1 &= c_2 = 1, \, \, x_1 = -\frac{1}{\sqrt{3}}, \, \, x_2 = \frac{1}{\sqrt{3}} \\ \int_{a}^b f(x)dx &\cong \sum_{k=1}^n c_k f(x_k) = 1 \cdot f(x_1) + 1 \cdot f(x_2) \end{aligned} } \]

From the Scipy Special Functions Package:

from scipy.special.orthogonal import p_roots
[x,w] = p_roots(2)
print('x = ', x, ',','w = ', w)
x =  [-0.57735027  0.57735027] , w =  [1. 1.]

Python: Vectorized GQ Program

from scipy.special.orthogonal import p_roots

def GQ1(f,n): #n-pt GQ on [-1,1]
    [x,w] = p_roots(n+1)
    G = sum(w*f(x))
    return G

def GQ2(f,n,a,b): #n-pt GQ on [a,b]
    [x,w] = p_roots(n+1)
    G = 0.5*(b-a)*sum(w*f(0.5*(b-a)*x+0.5*(b+a)))
    return G

Example: Python GQ1

f = lambda x: 1.7*x**3 + 3*x**2 + 2.4*x + 4
GQ1(f,2)
10.0
GQ1(f,3)
10.0

Example: Python GQ for Gaussian

f = lambda x: np.exp(-x**2)
GQ2(f,3,0,1)
0.7468244681309939

Conclusion

  • Gauss quadrature achieves high accuracy for very few nodes.
    • Nodes and weights strategically computed.
    • Nodes and weights same for all \( f(x) \).
    • Linear and quadratic interpolation (\( n=2 \), \( n=3 \))
  • Greater accuracy and fewer flops than for Newton-Cotes.