# Numerical Methods/Numerical Integration

Often, we need to find the integral of a function that may be difficult to integrate analytically (ie, as a definite integral) or impossible (the function only existing as a table of values).

Some methods of approximating said integral are listed below.

## Trapezoidal Rule

Consider some function, possibly unknown, ${\displaystyle f(x)}$ , with known values over the interval [a,b] at n+1 evenly spaced points xi of spacing ${\displaystyle h={(b-a) \over n}}$ , ${\displaystyle x_{0}=a}$  and ${\displaystyle x_{n}=b}$ .

Further, denote the function value at the ith mesh point as ${\displaystyle f(x_{i})}$ .

Using the notion of integration as "finding the area under the function curve", we can denote the integral over the ith segment of the interval, from ${\displaystyle x_{i-1}}$  to ${\displaystyle x_{i}}$  as:

${\displaystyle \int _{x_{i-1}}^{x_{i}}f(x)\,dx}$  = (1)

Since we may not know the antiderivative of ${\displaystyle f(x)}$ , we must approximate it. Such approximation in the Trapezoidal Rule, unsurprisingly, involves approximating (1) with a trapezoid of width h, left height ${\displaystyle f(x_{i-1})}$ , right height ${\displaystyle f(x_{i})}$ . Thus,

(1) ${\displaystyle \simeq {1 \over 2}h(f(x_{i-1})+f(x_{i}))}$  = (2)

(2) gives us an approximation to the area under one interval of the curve, and must be repeated to cover the entire interval.

For the case where n = 2,

${\displaystyle \int _{x_{a}}^{x_{b}}f(x)\,dx\simeq {1 \over 2}h(f(x_{0})+f(x_{1}))+{1 \over 2}h(f(x_{1})+f(x_{2}))}$  = (3)

Collecting like terms on the right hand side of (3) gives us:

${\displaystyle {1 \over 2}h(f(x_{0})+f(x_{1})+f(x_{1})+f(x_{2}))}$

or

${\displaystyle {1 \over 2}h(f(x_{0})+2f(x_{1})+f(x_{2}))}$

Now, substituting in for h and cleaning up,

${\displaystyle {(b-a) \over 2\cdot 2}(f(x_{0})+2f(x_{1})+f(x_{2}))}$

To motivate the general version of the trapezoidal rule, now consider n = 4,

${\displaystyle \int _{x_{a}}^{x_{b}}f(x)\,dx\simeq {1 \over 2}h(f(x_{0})+f(x_{1}))+{1 \over 2}h(f(x_{1})+f(x_{2}))+{1 \over 2}h(f(x_{2})+f(x_{3}))+{1 \over 2}h(f(x_{3})+f(x_{4}))}$

Following a similar process as for the case when n=2, we obtain

${\displaystyle {(b-a) \over 2\cdot 4}(f(x_{0})+2(f(x_{1})+f(x_{2})+f(x_{3}))+f(x_{4}))}$

Proceeding to the general case where n = N,

${\displaystyle \int _{x_{a}}^{x_{b}}f(x)\,dx\simeq {(b-a) \over 2\cdot n}(f(x_{0})+2(\sum _{k=1}^{N}f(x_{k}))+f(x_{n}))}$

This is an example of what the trapezoidal rule would represent graphicly, here ${\displaystyle y=-x^{2}+5}$ .

### Example

Approximate ${\displaystyle \int _{0}^{1}x^{3}\,dx}$  to within 5%.

First, since the function can be exactly integrated, let us do so, to provide a check on our answer.

${\displaystyle \int _{0}^{1}x^{3}\,dx=\left[{x^{4} \over 4}\right]_{0}^{1}={1 \over 4}=0.25}$  = (4)

We will start with an interval size of 1, only considering the end points.

${\displaystyle f(0)=0}$

${\displaystyle f(1)=1}$

(4) ${\displaystyle \simeq {(1-0) \over (2\cdot 1)}(f(0)+f(1))={1 \over 2.1}(0+1)={1 \over 2}=0.5}$

Relative error = ${\displaystyle \left|{(0.5-0.25) \over 0.25}\right|=1}$

Hmm, a little high for our purposes. So, we halve the interval size to 0.5 and add to the list

${\displaystyle f(0.5)=0.125}$

(4) ${\displaystyle \simeq {(1-0) \over (2\cdot 2)}(f(0)+2f(0.5)+f(1))={1 \over 2\cdot 2}(0+2(0.125)+1)={1.25 \over 4}=0.3125}$

Relative error = ${\displaystyle \left|{(0.3125-0.25) \over 0.25}\right|=0.25}$

Still above 0.01, but vastly improved from the initial step. We continue in the same fashion, calculating ${\displaystyle f(0.25)}$  and ${\displaystyle f(0.75)}$ , rounding off to four decimal places.

${\displaystyle f(0.25)=0.0156}$

${\displaystyle f(0.75)=0.4219}$

(4) ${\displaystyle \simeq {(1-0) \over (2\cdot 4)}(0+2(0.0156+0.125+0.4219)+1)={1 \over 8}(2.2150)=0.2656}$

Relative error = ${\displaystyle \left|{(0.2656-0.25) \over 0.25}\right|=0.0624}$

We are well on our way. Continuing, with interval size 0.125 and rounding as before,

${\displaystyle f(0.125)=0.0020}$

${\displaystyle f(0.375)=0.0527}$

${\displaystyle f(0.625)=0.2441}$

${\displaystyle f(0.875)=0.6699}$

(4) ${\displaystyle \simeq {(1-0) \over (2\cdot 8)}(0+2(0.0020+0.0156+0.0527+0.0125+0.2441+0.4219+0.6699)+1)={1 \over 16}(4.0624)=0.2539}$

Relative error = ${\displaystyle \left|{(0.2539-0.25) \over 0.25}\right|=0.0156}$

Since our relative error is less than 5%, we stop.

### Error Analysis

Let y=f(x) be continuous, well-behaved and have continuous derivatives in [x0,xn]. We expand y in a Taylor series about x=x0,thus-
${\displaystyle \int _{x_{0}}^{x_{1}}y\,dx=\int _{x_{0}}^{x_{1}}[y_{0}+(x-x_{0})y'_{0}+(x-x_{0})^{2}y''_{0}/2!+......]\,dx}$

## Simpson's Rule

Consider some function ${\displaystyle y=f(x)}$  possibily unknown with known values over the interval [a,b] at n+1 evently spaced points then it defined as

${\displaystyle \int _{x_{0}}^{x_{n}}f(x)\,dx\simeq {1 \over 3}h{\bigg \{}f(x_{0})+f(x_{n})+2{\Big (}f(x_{2})+f(x_{4})+...+f(x_{n-2}){\Big )}+4{\Big (}f(x_{1})+f(x_{3})+...+f(x_{n-1}){\Big )}{\bigg \}}}$

where ${\displaystyle h={(b-a) \over n}}$  and ${\displaystyle x_{0}=a}$  and ${\displaystyle x_{n}=b}$ .

### Example

Evaluate ${\displaystyle \int \limits _{0}^{1.2}{x\left({8-x^{3}}\right)^{\frac {1}{2}}dx}}$  by taking ${\displaystyle n=6}$  (${\displaystyle n}$  must be even)

Solution: Here ${\displaystyle f(x)=x\left({8-x^{3}}\right)^{\frac {1}{2}}}$

Since ${\displaystyle a=0}$  & ${\displaystyle b=1.2}$  so ${\displaystyle h={\frac {b-a}{n}}={\frac {1.2-0}{6}}=0.2}$

Now when ${\displaystyle a=x_{0}=0}$  then ${\displaystyle f(x_{0})=0}$

And since ${\displaystyle x_{n}=x_{n-1}+h}$ , therefore for ${\displaystyle x_{1}=0.2}$  , ${\displaystyle x_{2}=0.4}$  , ${\displaystyle x_{3}=0.6}$  , ${\displaystyle x_{4}=0.8}$  , ${\displaystyle x_{5}=1}$  , ${\displaystyle x_{6}=b=1.2}$  the corresponding values are ${\displaystyle f(x_{1})=0.7784}$  , ${\displaystyle f(x_{2})=1.58721}$  , ${\displaystyle f(x_{3})=1.6740}$  , ${\displaystyle f(x_{4})=2.1891}$  , ${\displaystyle f(x_{5})=2.6458}$  , ${\displaystyle f(x_{6})=3.0053}$

Incomplete ... Completed soon

## Simpson's 3/8

The numerical integration technique known as "Simpson's 3/8 rule" is credited to the mathematician Thomas Simpson (1710-1761) of Leicestershire, England. His also worked in the areas of numerical interpolation and probability theory.

Theorem (Simpson's 3/8 Rule) Consider over , where , , and . Simpson's 3/8 rule is

.

This is an numerical approximation to the integral of over and we have the expression

.

The remainder term for Simpson's 3/8 rule is , where lies somewhere between , and have the equality

.

Proof Simpson's 3/8 Rule Simpson's 3/8 Rule

Composite Simpson's 3/8 Rule

Our next method of finding the area under a curve  is by approximating that curve with a series of cubic segments that lie above the intervals  .  When several cubics are used, we call it the composite Simpson's 3/8 rule.

Theorem (Composite Simpson's 3/8 Rule) Consider over . Suppose that the interval is subdivided into subintervals of equal width by using the equally spaced sample points for . The composite Simpson's 3/8 rule for subintervals is

.

This is an numerical approximation to the integral of over and we write

.

Proof Simpson's 3/8 Rule Simpson's 3/8 Rule

Remainder term for the Composite Simpson's 3/8 Rule

Corollary (Simpson's 3/8 Rule: Remainder term) Suppose that is subdivided into subintervals of width . The composite Simpson's 3/8 rule

.

is an numerical approximation to the integral, and

.

Furthermore, if , then there exists a value with so that the error term has the form

.

This is expressed using the "big " notation .

Remark. When the step size is reduced by a factor of the remainder term should be reduced by approximately .

Algorithm Composite Simpson's 3/8 Rule. To approximate the integral

,

by sampling at the equally spaced sample points for , where . Notice that and .

Animations (Simpson's 3/8 Rule Simpson's 3/8 Rule). Internet hyperlinks to animations.

Computer Programs Simpson's 3/8 Rule Simpson's 3/8 Rule

Mathematica Subroutine (Simpson's 3/8 Rule). Object oriented programming.

Example 1. Numerically approximate the integral by using Simpson's 3/8 rule with m = 1, 2, 4. Solution 1.

Example 2. Numerically approximate the integral by using Simpson's 3/8 rule with m = 10, 20, 40, 80, and 160. Solution 2.

Example 3. Find the analytic value of the integral (i.e. find the "true value"). Solution 3.

Example 4. Use the "true value" in example 3 and find the error for the Simpson' 3/8 rule approximations in example 2. Solution 4.

Example 5. When the step size is reduced by a factor of the error term should be reduced by approximately . Explore this phenomenon. Solution 5.

Example 6. Numerically approximate the integral by using Simpson's 3/8 rule with m = 1, 2, 4. Solution 6.

Example 7. Numerically approximate the integral by using Simpson's 3/8 rule with m = 10, 20, 40, 80, and 160. Solution 7.

Example 8. Find the analytic value of the integral (i.e. find the "true value"). Solution 8.

Example 9. Use the "true value" in example 8 and find the error for the Simpson's 3/8 rule approximations in example 7. Solution 9.

Example 10. When the step size is reduced by a factor of the error term should be reduced by approximately . Explore this phenomenon. Solution 10.

Various Scenarios and Animations for Simpson's 3/8 Rule.

Example 11. Let over . Use Simpson's 3/8 rule to approximate the value of the integral. Solution 11.

Animations (Simpson's 3/8 Rule Simpson's 3/8 Rule). Internet hyperlinks to animations.