Ch5.3: Gauss Quadrature

Ch5.3: Gauss Quadrature

  • Gauss quadrature achieves high accuracy for very few nodes.
    • Nodes and weights strategically computed.
    • Linear and quadratic interpolation (\( n=2 \), \( n=3 \))
  • Greater accuracy and fewer flops than for Newton-Cotes.
    • Left figure: 2-Point Trapezoid Rule
    • Right figure: 2-Point Gauss Quadrature Rule

Humor



What do you call a farm that makes bad jokes?

Corny!

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) + 1 \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.

Two-Pt GQ Values on [-1,1] with Pracma

library(pracma)
gaussLegendre(2,-1,1)
$x
[1] -0.5773503  0.5773503

$w
[1] 1 1

3-Pt GQ Values on [-1,1] with Pracma

library(pracma)
gaussLegendre(3,-1,1)
$x
[1] -7.745967e-01  8.881784e-16  7.745967e-01

$w
[1] 0.5555556 0.8888889 0.5555556

GQ Values on [a,b] with Pracma

gaussLegendre(2,0,1)
$x
[1] 0.2113249 0.7886751

$w
[1] 0.5 0.5
gaussLegendre(3,1,10)
$x
[1] 2.014315 5.500000 8.985685

$w
[1] 2.5 4.0 2.5

Example 2: Probability Value

  • In STAT 200, probabilities are calculated from integrals of standard normal distribution:

\[ \int_a^b e^{-x^2} dx \]

www.desmos.com/calculator/chjetmv2ae

R Code in Book for [-1,1]

gaussint <- function(f,x,w) {
  y <- f(x)
  return (sum(y*w))}
w <- c(1,1)
x <- c(-1/sqrt(3),1/sqrt(3))
f <- function(x){x^2}
gaussint(f,x,w)
[1] 0.6666667

\[ \small{ \int_{a}^b f(x)dx \cong c_1 \cdot f(x_1) + c_2 \cdot f(x_2)} \]

Two-Point Gauss Quadrature on [a, b]

  • To switch from [-1,1] to \( [a,b] \), need to use a \( u \)-substitution:

\[ \small{ u = \frac{2x-a-b}{b-a}, \,\, a \leq x \leq b} \]

  • R program:
gaussquad2 <- function(f,a,b) {
  w <- c(1,1)
  x <- c(-1/sqrt(3),1/sqrt(3))
  d <- (b-a)/2
  s <- (b+a)/2
  u <- d*x + s
  y <- f(d*x + s)
  return(d*sum(y*w))}

Example 3

title

f <- function(x) { x^2 }
G<-gaussquad2(f,0,1)
T<-trap(f,0,1,m = 10)
c(G,T)
[1] 0.3333333 0.3350000
c(abs(1/3-G),abs(1/3-T))
[1] 0.000000000 0.001666667

Example 4

title

f <- function(x) { x^4 }
G<-gaussquad2(f,0,1)
T<-trap(f,0,1,m = 10)
c(G,T)
[1] 0.1944444 0.2033300
c(abs(1/5-G),abs(1/5-T))
[1] 0.005555556 0.003330000

Three-Point Gauss Quadrature on [a, b]

gaussquad3 <- function(f,a,b) {
  w <- c(5/9,8/9,5/9)
  x <- c(-sqrt(3/5),0,sqrt(3/5))
  d <- (b-a)/2
  s <- (b+a)/2
  u <- d*x + s
  y <- f(d*x + s)
  return(d*sum(y*w))}

Example 5

title

f <- function(x) { x^2 }
G<-gaussquad3(f,0,1)
S<-simp(f,0,1,m = 10)
c(G,S)
[1] 0.3333333 0.1133333
c(abs(1/3-G),abs(1/3-S))
[1] 5.551115e-17 2.200000e-01

Example 6

title

f <- function(x) { x^4 }
G<-gaussquad3(f,0,1)
S<-simp(f,0,1,m = 10)
c(G,S)
[1] 0.20000000 0.06778083
c(abs(1/5-G),abs(1/5-S))
[1] 0.0000000 0.1322192

Example 7: Probability Value

title title

f <- function(x){exp(-x^2)}
G<-gaussquad3(f,1,1.5)
S<-simp(f,1,1.5,m = 10)
c(G,S)
[1] 0.1093642 0.3696090
A <- 0.109364260812
c(abs(A-G),abs(A-S))
[1] 6.478000e-08 2.602447e-01

5.3 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.

title