Numerical Integration Notes

Numerical Integration

Goal and Approach

  • Goal: Approximate the definite integral of a function f(x)f(x) from aa to bb numerically: abf(x)dx\int_{a}^{b} f(x) dx, where f:RRf : \mathbb{R} \rightarrow \mathbb{R}.
  • Quadrature Formula: Approximate the definite integral using a weighted sum of function values at specific points: <em>abf(x)dxω</em>0f(x<em>0)++ω</em>nf(xn)\int<em>{a}^{b} f(x) dx \approx \omega</em>0 f(x<em>0) + \cdots + \omega</em>n f(x_n)
    • x<em>0,,x</em>nx<em>0, \dots, x</em>n are the quadrature nodes.
    • ω<em>0,,ω</em>n\omega<em>0, \dots, \omega</em>n are the quadrature weights.
  • Definition: A quadrature rule is exact for polynomials of degree nn if it gives the exact value when f(x)f(x) is a polynomial of degree less than or equal to nn.

Change of Variables


  • Transformation: Use the change of variable y=xabay = \frac{x-a}{b-a} to transform the integral over the interval [a,b][a, b] to an integral over [0,1][0, 1]:
    <em>abf(x)dx=(ba)</em>01g(y)dy\int<em>{a}^{b} f(x) dx = (b - a) \int</em>{0}^{1} g(y) dy, where g(y)=f(a+(ba)y)g(y) = f(a + (b - a)y).

  • Interval Transformation: Quadrature formulas on [0,1][0, 1] can be transformed to any interval [a,b][a, b]:

IntervalWeightsNodes
[0,1][0, 1]omega<em>k\\omega<em>k | kn\frac{k}{n} | | [a,b][a, b] | (ba)ω</em>k(b - a)\omega</em>ka+kbana + k \cdot \frac{b-a}{n}

Basic Closed Newton-Cotes Formulas

  • Idea: Replace f(x)f(x) with an interpolating polynomial P<em>n(x)P<em>n(x) using n+1n+1 equispaced nodes in [a,b][a, b]: P</em>n(x)=<em>k=0nf(x</em>k)<em>k(x)P</em>n(x) = \sum<em>{k=0}^{n} f(x</em>k) \ell<em>k(x), where </em>k(x)=<em>jkxx</em>jx<em>kx</em>j\ell</em>k(x) = \prod<em>{j \neq k} \frac{x - x</em>j}{x<em>k - x</em>j}, and xk=a+kbanx_k = a + k \cdot \frac{b - a}{n}.
  • Closed Quadrature Newton-Cotes Formulas: Approximate the integral as:
    <em>abf(x)dx</em>k=0nf(x<em>k)α</em>k\int<em>{a}^{b} f(x) dx \approx \sum</em>{k=0}^{n} f(x<em>k) \alpha</em>k, where α<em>k=</em>abk(x)dx\alpha<em>k = \int</em>{a}^{b} \ell_k(x) dx.

Weights of the NC Quadrature Rules

  • Lemma: The coefficients alpha<em>k\\alpha<em>k are unique such that </em>abp(x)dx=<em>k=0np(x</em>k)αk\int</em>{a}^{b} p(x) dx = \sum<em>{k=0}^{n} p(x</em>k) \alpha_k for any polynomial p(x)p(x) of degree less than or equal to nn.
  • Calculating Weights: To find the values of alpha<em>k\\alpha<em>k, take p</em>j(x)=xjp</em>j(x) = x^j for j=0,,nj = 0, \dots, n. Since <em>01xjdx=1j+1\int<em>{0}^{1} x^j dx = \frac{1}{j+1} for j=0,,nj = 0, \dots, n, we solve the following linear system: \begin{aligned} \alpha0 + \alpha1 + \cdots + \alpha{n-1} + \alphan &= \frac{1}{1} \ \frac{1}{n}\alpha1 + \cdots + \frac{n-1}{n} \alpha{n-1} + \alphan &= \frac{1}{2} \
    \vdots \
    \left(\frac{1}{n}\right)^n \alpha1 + \cdots + \left(\frac{n-1}{n}\right)^n \alpha{n-1} + \alpha_n &= \frac{1}{n+1}
    \end{aligned}

Basic Closed NC Quadrature Rules for n = 1, 2

  • Trapezoid's Rule (n = 1):
    abf(x)dx(ba)f(a)+f(b)2\int_{a}^{b} f(x) dx \approx (b - a) \cdot \frac{f(a) + f(b)}{2}
  • Simpson's Rule (n = 2):
    abf(x)dxba6[f(a)+4f(a+b2)+f(b)]\int_{a}^{b} f(x) dx \approx \frac{b - a}{6} \cdot \left[ f(a) + 4 \cdot f\left(\frac{a+b}{2}\right) + f(b) \right]

Error in the Basic NC Formulas

  • Recall: The error in polynomial interpolation:
    f(x)=P<em>n(x)+</em>i=0n(xxi)f(n+1)(ξ(x))(n+1)!f(x) = P<em>n(x) + \frac{\prod</em>{i=0}^{n} (x - x_i) f^{(n+1)}(\xi(x))}{(n+1)!}, where aξ(x)ba \leq \xi(x) \leq b.
  • Integrating: Integrating the error term gives:
    <em>abf(x)dx=</em>ab[P<em>n(x)+</em>i=0n(xx<em>i)f(n+1)(ξ(x))(n+1)!]dx\int<em>{a}^{b} f(x) dx = \int</em>{a}^{b} \left[ P<em>n(x) + \frac{\prod</em>{i=0}^{n} (x - x<em>i) f^{(n+1)}(\xi(x))}{(n+1)!} \right] dx=</em>abP<em>n(x)dx+</em>ab<em>i=0n(xx</em>i)f(n+1)(ξ(x))(n+1)!dx= \int</em>{a}^{b} P<em>n(x) dx + \int</em>{a}^{b} \frac{\prod<em>{i=0}^{n} (x - x</em>i) f^{(n+1)}(\xi(x))}{(n+1)!} dx
    =<em>k=0nf(x</em>k)α<em>k+</em>ab<em>i=0n(xx</em>i)f(n+1)(ξ(x))(n+1)!dx.= \sum<em>{k=0}^{n} f(x</em>k) \alpha<em>k + \int</em>{a}^{b} \frac{\prod<em>{i=0}^{n} (x - x</em>i) f^{(n+1)}(\xi(x))}{(n+1)!} dx.
  • Error Bound: The error is thus bounded by
    E<em>n(f):=</em>abf(x)dx<em>k=0nf(x</em>k)α<em>k=</em>ab<em>i=0n(xx</em>i)f(n+1)(ξ(x))(n+1)!dxE<em>n(f) := \left| \int</em>{a}^{b} f(x) dx - \sum<em>{k=0}^{n} f(x</em>k) \alpha<em>k \right| = \left| \int</em>{a}^{b} \frac{\prod<em>{i=0}^{n} (x - x</em>i) f^{(n+1)}(\xi(x))}{(n + 1)!} dx \right|
    <em>ab</em>i=0nxx<em>if(n+1)(ξ(x))(n+1)!dx(ba)n+1max</em>axbf(n+1)(x)(n+1)!ab1dx.\leq \int<em>{a}^{b} \frac{\prod</em>{i=0}^{n} |x - x<em>i| |f^{(n+1)}(\xi(x))|}{(n + 1)!} dx \leq \frac{(b - a)^{n+1} \max</em>{a \leq x \leq b} |f^{(n+1)}(x)|}{(n + 1)!} \int_{a}^{b} 1 dx.
  • Error Bound for Simple Closed Newton-Cotes Formula: E<em>n(f)(ba)n+2(n+1)!max</em>axbf(n+1)(x).E<em>n(f) \leq \frac{(b - a)^{n+2}}{(n+1)!} \cdot \max</em>{a \leq x \leq b} |f^{(n+1)}(x)| .
  • Even n Error: If nn is even, then E<em>n+1(f)c</em>n(ba)n+3max<em>axbf(n+2)(x)E<em>{n+1}(f) \leq c</em>n \cdot (b - a)^{n+3} \cdot \max<em>{a \leq x \leq b} |f^{(n+2)}(x)|, where c</em>n=1(n+2)!0nt2(t1)(tn)dtc</em>n = \frac{1}{(n+2)!} \left| \int_{0}^{n} t^2 (t - 1) \cdots (t - n) dt \right|.

Composite NC Formulas: Basic Setting

  • Idea: Split the interval [a,b][a, b] into subintervals [a,x<em>1][x</em>1,x<em>2][x</em>p1,b][a, x<em>1] \cup [x</em>1, x<em>2] \cup \cdots \cup [x</em>{p-1}, b], integrate over each subinterval, and apply a Newton-Cotes formula with n+1n+1 nodes to each.
    <em>abf(x)dx=</em>ax<em>1f(x)dx+</em>x<em>1x</em>2f(x)dx++<em>x</em>p1bf(x)dx,\int<em>{a}^{b} f(x) dx = \int</em>{a}^{x<em>1} f(x) dx + \int</em>{x<em>1}^{x</em>2} f(x) dx + \cdots + \int<em>{x</em>{p-1}}^{b} f(x) dx,
  • Ingredients:
    • N=npN = np (total number of intervals).
    • hN=baNh_N = \frac{b - a}{N} (integration step).
    • x<em>j=a+jh</em>Nx<em>j = a + j \cdot h</em>N for j=0,,Nj = 0, \dots, N (integration nodes).

Composite NC Formulas for n = 1, 2

  • Composite Trapezoid's Rule (n=1    N=pn = 1 \implies N = p):
    <em>abf(x)dxh</em>N2(f(a)+2f(x<em>1)++2f(x</em>N1)+f(b))\int<em>{a}^{b} f(x) dx \approx \frac{h</em>N}{2} \left( f(a) + 2f(x<em>1) + \cdots + 2f(x</em>{N-1}) + f(b) \right)
  • Composite Simpson's Rule (n=2    N=2pn = 2 \implies N = 2p):
    <em>abf(x)dxh</em>N3(f(a)+4f(x<em>1)+2f(x</em>2)+4f(x<em>3)++2f(x</em>N2)+4f(x<em>N1)+f(b))\int<em>{a}^{b} f(x) dx \approx \frac{h</em>N}{3} \left( f(a) + 4f(x<em>1) + 2f(x</em>2) + 4f(x<em>3) + \cdots + 2f(x</em>{N-2}) + 4f(x<em>{N-1}) + f(b) \right)</em>abf(x)dxhN3(E+4I+2P)\int</em>{a}^{b} f(x) dx \approx \frac{h_N}{3} (E + 4I + 2P)

Error in Composite NC Formulas

  • Error Bound: For nn odd:
    E(f)(ba)(baN)n+1nn+1(n+1)!maxaxbf(n+1)(x)E(f) \leq (b - a) \left( \frac{b - a}{N} \right)^{n+1} \cdot \frac{n^{n+1}}{(n+1)!} \cdot \max_{a \leq x \leq b} |f^{(n+1)}(x)|
  • Error Bound: For nn even:
    E(f)c<em>n(ba)(baN)n+2nn+2max</em>axbf(n+2)(x)E(f) \leq c<em>n \cdot (b - a) \left( \frac{b - a}{N} \right)^{n+2} \cdot n^{n+2} \cdot \max</em>{a \leq x \leq b} |f^{(n+2)}(x)|
  • Note: nn is fixed. This method can be too expensive.

Richardson Extrapolation

  • Concept: Given an approximation A(h)A(h) to some quantity AA, dependent on a stepsize hh:
    A(h)=A+chn+O(hn+1)A(h) = A + c h^n + O(h^{n+1}) (where cc may depend on nn but not on hh).
  • Example: NC for nn odd is an approximation to A=abf(x)dxA = \int_{a}^{b} f(x) dx with order n+1n+1 on h=baNh = \frac{b - a}{N}.
  • Richardson Extrapolation Formula:
    R(h,t):=tnA(h/t)A(h)tn1R(h, t) := \frac{t^n A(h/t) - A(h)}{t^n - 1}
    has convergence order n+1n+1, since
    R(h,t)=tn(A+c(ht)n+O(hn+1))(A+chn+O(hn+1))tn1=A+O(hn+1).R(h, t) = \frac{t^n \left( A + c \left( \frac{h}{t} \right)^n + O(h^{n+1}) \right) - (A + c h^n + O(h^{n+1}))}{t^n - 1} = A + O(h^{n+1}).

Extrapolated Simpson Rule

  • Apply Richardson extrapolation to the simple Simpson rule, S(h)S(h), with t=2t = 2 in [a,b][a, b], where h=bah = b - a.
  • Basic Simpson's Rule: A(h)=S(h)=h6[f(a)+4f(a+b2)+f(b)]A(h) = S(h) = \frac{h}{6} \left[ f(a) + 4f \left( \frac{a+b}{2} \right) + f(b) \right]
  • Composite Simpson's Rule: A(h2)=S2(h)=h12[f(a)+4f(3a+b4)+2f(a+b2)+4f(a+3b4)+f(b)]A(\frac{h}{2}) = S_2(h) = \frac{h}{12} \left[ f(a) + 4f \left( \frac{3a+b}{4} \right) + 2f \left( \frac{a+b}{2} \right) + 4f \left( \frac{a+3b}{4} \right) + f(b) \right]
  • Extrapolated Simpson Rule: Q(h)=16S2(h)S(h)15=h90[7f(a)+32f(3a+b4)+12f(a+b2)+32f(a+3b4)+7f(b)].Q(h) = \frac{16 S_2(h) - S(h)}{15} = \frac{h}{90} \left[ 7 f(a) + 32 f \left( \frac{3a + b}{4} \right) + 12 f \left( \frac{a + b}{2} \right) + 32 f \left( \frac{a + 3b}{4} \right) + 7 f(b) \right].
  • Let h=bah = b - a:
    Q(h)=h90[7f(a)+32f(a+h4)+12f(a+h2)+32f(a+3h4)+7f(b)]Q(h) = \frac{h}{90} \left[ 7 f(a) + 32 f \left(a + \frac{h}{4} \right) + 12 f \left(a + \frac{h}{2} \right) + 32 f \left(a + \frac{3h}{4} \right) + 7 f(b) \right]
  • This is the simple closed Newton-Cotes formula for 5 nodes and is exact for polynomials with degree less than or equal to 5.

Adaptive Integration

  • Goal: Obtain a good approximation to abf(x)dx\int_{a}^{b} f(x) dx while minimizing the number of function evaluations.
  • Idea: Break [a,b][a, b] into pieces. Approximate the integral in each piece using two different methods (e.g., simple and composite formulas). If the difference is small enough, use Richardson extrapolation. Otherwise, subdivide the interval and repeat.
  • Steps:
    1. Compute SS and S2S_2 (simple and composite Simpson's rule with 2 subintervals).
    2. Evaluate E=SS2E = |S - S_2|, which estimates the error.
    3. If EtolE \leq tol, then <em>abf(x)dx16S</em>2S15=Q\int<em>{a}^{b} f(x) dx \approx \frac{16S</em>2 - S}{15} = Q.
    4. If E > tol, repeat steps 1 and 2 in:
      • Step 3.1: the interval [a,a+b2]\left[a, \frac{a+b}{2} \right], and
      • Step 3.2: the interval [a+b2,b]\left[\frac{a+b}{2}, b\right].
    5. Add up the values of QQ obtained in all steps at each iteration.

MATLAB Commands

  • quad: MATLAB's professional method for approximating integrals, using an extrapolated Simpson's rule in an adaptive method.
  • quadtx: version of quad in the NCM book/folder.

Example

  • f(x)=e4xsin(2x)f(x) = e^{-4x} \sin(2x) in [0,4][0, 4]

Exercise

  • I:=40111+x2dx.I := 4 \int_{0}^{1} \frac{1}{1+x^2} dx.
    1. What is the value of II?
    2. Approximate II using the trapezoid rule.
    3. Approximate II using Simpson's rule.
    4. Approximate II using composite Simpson's rule with 8 intervals.
    5. How many subintervals are needed to approximate II to within 101610^{-16} using the composite Simpson's formula?