Approximating definite integrals

To approximate the definite integral $\int_a^b f(x) dx$ we have different means, including

  • (right) Riemann sum: $I \approx f(b)\cdot (b-a)$

  • trapezoid method: $I \approx 1/2 \cdot (f(b) + f(a)) \cdot (b-a)$

  • Simpson's rule: $I \approx (b-a)/6 \cdot (f(a) + 4f(a/2 + b/2)) + f(b)$

  • Gauss 7-point quadrature $I \approx \sum w_k f(n_k)$ for selected weights and nodes.

The first 3 are applied below to a partition of $[a,b]$ into $n$ equal-sized intervals. The latter adaptively used in quadgk

$\int_0^\pi \sin(x) dx = 2$.

Using 7-point Gauss on $[0, \pi]$: 2.0000000000017906

Using quadgk n $[0, \pi]$: 2.000000021101344

Which method =

$n =$ 8

1.974231601946
riemann (generic function with 2 methods)
quadgk (generic function with 1 method)
g7 (generic function with 1 method)