logo

Numerical Integration 📂Numerical Analysis

Numerical Integration

Definition 1

Let’s assume that f:[a,b]Rf : [a,b] \to \mathbb{R} is integrable over [a,b][a,b], and [a,b][a,b] is divided into nodes like a=x0<<xn=ba = x_{0} < \cdots < x_{n} = b.

  1. The integral operator II is defined as I(f):=abf(x)dx\displaystyle I(f) := \int_{a}^{b} f(x) dx.
  2. The integral operator InI_{n} is defined as In(f):=k=1nxk1xkf(x)dx\displaystyle I_{n} (f) := \sum_{k=1}^{n} \int_{x_{k-1}}^{x_{k}} f(x) dx.
  3. The error EnE_{n} is defined as En(f):=I(f)In(f)E_{n} (f) := I (f) - I_{n} ( f ).
  4. limnE~n(f)En(f)=1\displaystyle \lim_{n \to \infty} {{\tilde{E}_{n} (f) } \over { E_{n} (f) }} = 1, satisfying E~n\tilde{E}_{n}, is referred to as the asymptotic error for EnE_{n}.

Explanation

No one would dispute that calculating definite integrals is one of the goals of numerical analysis. In fact, the idea behind the method of exhaustion suggests that the more we divide the space, the better we can approximate the value of the integral. Thus, when it comes to numerical integration of interest in numerical analysis, it is about how much error there is, how to integrate when not in a closed interval, etc.

Example

Trapezoidal Rule

As an example of numerical integration, let’s take a look at the trapezoidal rule for fC2[a,b]f \in C^2 [a,b]. The Trapezoidal Rule approximates the value for I(f)I(f) by linearly interpolating ff over the interval [a,b][a,b] and taking the integral of that function as the approximation for I(f)I(f).

20190611\_151146.png

I11(f):=(ba2)[f(a)+f(b)] I_{1}^{1} (f) := \left( {{ b - a } \over { 2 }} \right) [ f(a) + f(b) ] In this case, the actual error between I(f)I(f) and I11(f)I_{1}^{1} (f) E11(f)E_{1}^{1} (f) can be computed as follows for some ξ[a,b]\xi \in [a,b].

Polynomial Interpolation:

  • [4] Error with the actual function: For a differentiable f:RRf : \mathbb{R} \to \mathbb{R} up to (n+1)(n+1) times and for some ξH{x0,,xn}\xi \in \mathscr{H} \left\{ x_{0} , \cdots , x_{n} \right\}, the polynomial interpolation of ff pnp_{n} satisfies f(t)pn(t)=(tx0)(txn)(n+1)!f(n+1)(ξ)\displaystyle f(t) - p_{n} (t) = {{ (t - x_{0}) \cdots (t - x_{n}) } \over { (n+1)! }} f^{(n+1)} ( \xi ) for some tRt \in \mathbb{R}.

E11(f):=I(f)I11(f)=ab[f(x)f(b)(xa)f(a)(xb)ba]dx=ab[f(x)p1(x)]dx=12f(ξ)ab(xa)(xb)dx=[12f(ξ)][16(ba)3]=112(ba)3f(ξ) \begin{align*} \displaystyle E_{1}^{1} (f) :=& I(f) - I_{1}^{1} (f) \\ =& \int_{a}^{b} \left[ f(x) - {{ f(b) ( x - a ) - f(a) (x - b) } \over { b - a }} \right] dx \\ =& \int_{a}^{b} \left[ f(x) - p_{1} (x) \right] dx \\ =& {{1} \over {2}} f '' ( \xi ) \int_{a}^{b} (x-a) (x-b) dx \\ =& \left[ {{1} \over {2}} f '' ( \xi ) \right] \left[ - {{1} \over {6}} (b-a)^{3} \right] \\ =& - {{1} \over {12}} (b-a)^{3} f '' ( \xi ) \end{align*}

Composite Trapezoidal Rule

Now, considering dividing [a,b][a,b] into nn intervals and approximating the integral at each [xk1,xk][x_{k-1} , x_{k} ] using the trapezoidal rule, the method is called the (Composite) Trapezoidal Rule. In1(f):=k=1nh2(f(xk1)+f(xk)) I_{n}^{1} (f) := \sum_{k=1}^{n} {{ h } \over { 2 }} \left( f (x_{k-1}) + f( x_{k} ) \right) Then, the actual error between I(f)I(f) and In1(f)I_{n}^{1} (f) can be computed as follows for some ξk[xk1,xk]\xi_{k} \in [x_{k-1}, x_{k} ]. En1(f)=I(f)In1(f)=k=1n(h312f(ξk))=h3n12(1nk=1nf(ξk)) \begin{align*} \displaystyle E_{n}^{1} (f) =& I (f) - I_{n}^{1} (f) \\ =& \sum_{k=1}^{n} \left( - {{ h^3 } \over { 12 }} f '' ( \xi_{k} ) \right) \\ =& - {{ h^3 n } \over { 12 }} \left( {{ 1 } \over { n }} \sum_{k=1}^{n} f '' ( \xi_{k} ) \right) \end{align*} Here, since (1nk=1nf(ξk))\displaystyle \left( {{ 1 } \over { n }} \sum_{k=1}^{n} f '' ( \xi_{k} ) \right) is the average of f’’(ξk)f’’ ( \xi_{k} ), due to the Intermediate Value Theorem, there must exist some ξ[a,b]\xi \in [a,b] that satisfies minx[a,b]f(x)f(ξ)maxx[a,b]f(x)\displaystyle \min_{x \in [a,b]} f ''(x) \le f ''( \xi ) \le \max_{x \in [a,b]} f ''(x). Thus, En1(f)=(ba)h212f(ξ) E_{n}^{1} (f) = - {{ (b-a) h^2 } \over {12}} f '' (\xi ) Seeing the error formulated like this lets us understand that the wider the interval [a,b][a,b], the larger the error becomes, and the smaller hh is, the more accurate it gets. However, due to f’’(ξ)f’’ ( \xi ) , we can’t know how much exactly it offsets. To overcome this, the asymptotic error is calculated as follows. limnEn1(f)h2=limn1h2k=1n(h312f(ξk))=112limnk=1nhf(ξk)=112abf(x)dx=112[f(b)f(a)] \begin{align*} \lim_{n \to \infty} {{ E_{n}^{1} (f) } \over { h^2 }} =& \lim_{n \to \infty} {{1} \over {h^2}} \sum_{k=1}^{n} \left( - {{ h^3 } \over { 12 }} f '' ( \xi_{k} ) \right) \\ =& - {{ 1 } \over { 12 }} \lim_{ n \to \infty} \sum_{k=1}^{n} h f '' ( \xi_{k} ) \\ =& - {{ 1 } \over { 12 }} \int_{a}^{b} f ''(x) dx \\ =& - {{ 1 } \over { 12 }} [ f '(b) - f '(a) ] \end{align*} If we only know the error En1(f)E_{n}^{1} (f), according to the Theorem, we know that f’’(ξ)f’’ ( \xi ) exists but couldn’t specify its value. But now, E~n1(f):=limnEn1(f) \tilde{E}_{n}^{1} (f) := \lim_{n \to \infty} E_{n}^{1} (f) for sufficiently large nn, it can be understood that En1(f)h212[f(b)f(a)]=E~n1(f) E_{n}^{1} (f) \approx - {{ h^2 } \over { 12 }} [ f '(b) - f '(a) ] = \tilde{E}_{n}^{1} (f) which makes it relatively simple and accurate to ascertain the error. Moreover, there’s no reason to stop at just knowing the extent of the error. If we can predict the error before even calculating, we can also correct for the error from the beginning. I(f)In1(f)E~n1(f)    I(f)In1(f)+E~n1(f) I(f) - I_{n}^{1} (f) \approx \tilde{E}_{n}^{1} (f) \implies I(f) \approx I_{n}^{1} (f) + \tilde{E}_{n}^{1} (f)

Corrected Trapezoidal Rule

Let’s define the operator for calculating I(f)I(f) as follows. CTn(f):=h[12f(x0)+f(x1)++f(xn1)+12f(xn)]h212[f(b)f(a)] CT_{n} (f) := h \left[ {{1} \over {2}} f (x_{0} ) + f (x_{1} ) + \cdots + f (x_{n-1} ) + {{1} \over {2}} f (x_{n} ) \right] - {{ h^2 } \over { 12 }} [ f '(b) - f '(a) ]

This method is referred to as the (Corrected) Trapezoidal Rule. [ NOTE: If you’ve followed the improvement process of the trapezoidal rule, you’d realize that there’s no need to distinguish between them explicitly. Simply calling it the trapezoidal rule would convey the meaning. ]

Implementation

The results of calculating 0πexcos(x)dx=eπ+1212.0703\displaystyle \int_{0}^{\pi} e^{x} \cos (x) dx = -{{e^{\pi} + 1} \over {2}} \approx - 12.0703 using In1I_{n}^{1} and CTnCT_{n} are as follows.

20190611\_125046.png

The first column represents nn, the second I(f)In1(f)\left| I(f) - I_{n}^{1} (f) \right|, and the third I(f)CT(f)\left| I(f) - CT (f) \right|. It can be observed that the error decreases by a factor of 14\displaystyle {{1} \over {4}} when the number of nodes is doubled in the composite trapezoidal rule, and by 116\displaystyle {{1} \over {16}} in the corrected trapezoidal rule. It’s evident that the corrected trapezoidal rule performs significantly better, especially in terms of speed.

Below is an example code written in Python.

import numpy as np
def f(x) :
    return np.exp(x)*np.cos(x)
 
def d(f,x,tol=10**(-8)) :
    h=1
    d1=1
    d2=0
 
    while(abs(d1-d2)>tol) :
        d1=d2
        d2=(f(x+h)-f(x))/h
        h=h/2
 
    return(d2)
 
def I(f,a,b,n) :
    h=(b-a)/n
    x=np.linspace(a,b,n+1)
    x=f(x)
    x[0]=x[0]/2
    x[-1]=x[-1]/2
    return(h*sum(x))
 
def CT(f,a,b,n) :
    h=(b-a)/n
    x=np.linspace(a,b,n+1)
    x=f(x)
    x[0]=x[0]/2
    x[-1]=x[-1]/2
    return(h*sum(x)-(h**2)*(d(f,b)-d(f,a))/12)
 
 
TV=-(np.exp(np.pi)+1)/2
print("True Value : %2.4f" % TV)
 
print("|   n |  I(f) Err | CT(f) Err |")
print("="*30)
for n in range(1,10) :
    print("| %3d |  %8.2e |  %8.2e |" % (2**n,abs(TV-I(f,0,np.pi,2**n)),abs(TV-CT(f,0,np.pi,2**n))))

  1. Atkinson. (1989). An Introduction to Numerical Analysis(2nd Edition): p249. ↩︎