Numerical Integration Part 2
\[\int_a^b f(x)\;dx = \sum_{i=0}^n w_i f(x_i) + \text{ error}\]
Different choices of \(x_i\) and \(w_i\).
Closed:
Open:
with \(n+1\) points:
For n even: and \(f \in \mathcal{C}^{n+\color{green}{2}}([a,b])\):
\[\text{error} = f^{(n+2)}(\xi)\frac{h^{n+3}}{(n+2)!}\int_0^n t^2(t-1)(t-2)\cdot(t-n)\;dt\]
For n odd: and \(f \in \mathcal{C}^{n+\color{green}{1}}([a,b])\):
\[\text{error} = f^{(n+1)}(\xi)\frac{h^{n+2}}{(n+1)!}\int_0^n t(t-1)(t-2)\cdot(t-n)\;dt\]
with \(n+1\) points:
For n even: and \(f \in \mathcal{C}^{n+\color{green}{2}}([a,b])\):
\[\text{error} = f^{(n+2)}(\xi)\frac{h^{n+3}}{(n+2)!}\int_{-1}^{n+1} t^2(t-1)(t-2)\cdot(t-n)\;dt\]
For n odd: and \(f \in \mathcal{C}^{n+\color{green}{1}}([a,b])\):
\[\text{error} = f^{(n+1)}(\xi)\frac{h^{n+2}}{(n+1)!}\int_{-1}^{n+1} t(t-1)(t-2)\cdot(t-n)\;dt\]
The largest \(n\) such that the formula is exact for every polynomial of degree \(n\).
N-C formulas with even \(n\): degree of accuracy is \(n+1\).
N-C formulas with odd \(n\): degree of accuracy is \(n\).
Open, with \(n = 0\).
\(x_0 = \frac{a+b}{2}\)
\(h = \frac{b - a}{2}\)
\(w_0 = 2h\)
\(\text{error } = f''(\xi)\frac{h^3}{3}\)
Degree of accuracy: 1
Closed, with \(n = 1\).
\(x_0 = a\), \(x_1 = b\).
\(h = \frac{b - a}{1}\)
\(w_0 = w_1 = \frac{h}{2}\)
\(\text{error } = -f''(\xi)\frac{h^3}{12}\)
Degree of accuracy: 1
Closed, with \(n = 2\).
\(x_0 = a\), \(x_1 = \frac{a+b}{2}\), \(x_2 = b\).
\(h = \frac{b - a}{2}\)
\(w_0 = w_2 = \frac{h}{3}\), \(w_1 = \frac{4h}{3}\)
\(\text{error } = -f^{(4)}(\xi)\frac{h^5}{90}\)
Degree of accuracy: 3
Approximate \(\displaystyle \int_a^b f(x)\;dx\) by \(\displaystyle \sum_{i=1}^n w_i f(x_i)\) such that we get the maximal possible degree of accuracy.
We need to determine \(x_i\) and \(w_i\) for \(i = 1, 2, \dots, n\), or \(2n\) unknowns.
We need \(2n\) equations.
We will require that the rule is exact for \(f(x) = 1\), \(f(x) = x\), \(f(x) = x^2\), . . ., \(f(x) = x^{2n-1}\).
We get degree of accuracy \(2n-1\).
If \(b - a\) is large, we would need a large number of nodes to make \(h\) small enough. That would lead to complicated rules, and require highly differentiable functions.
Instead, we divide the interval \([a,b]\) to many small subintervals and apply one of the quadrature rules on each subinterval separately. Then we add the results.
Let \(f\in \mathcal{C}^4([a,b])\) and let \(n\) be even.
Let \(h = \frac{b-a}{n}\) and \(x_j = a + jh\) for \(j = 0, 1, \dots, n\).
Then
\[\int_a^b f(x)\;dx = \frac{h}{3}\left(f(a) + 2\sum_{j=1}^{\frac{n}{2}-1}f(x_{2j}) + 4\sum_{j=1}^{n/2} f(x_{2j-1}) + f(b)\right) + \text{ error}\]
where
\[\text{error } = - \frac{b-a}{180} h^4 f^{(4)}(\xi).\]
\[\int_a^b f(x)\;dx = \frac{h}{3}\left(f(a) + 2\sum_{j=1}^{\frac{n}{2}-1}f(x_{2j}) + 4\sum_{j=1}^{n/2} f(x_{2j-1}) + f(b)\right) + \text{ error}\]
Σ = sum
function compositeSimpson(f::Function, a, b, n)
if !iseven(n)
throw(DomainError(n, "In Simpson's method the number of intervals must be even."))
end
h = (b-a)/n
x = [a + i*h for i in 1:(n-1)]
total = f(a) + f(b)
total += 4*Σ(f(x[i]) for i in 1:2:(n-1))
if n > 2
total += 2*Σ(f(x[i]) for i in 2:2:(n-2))
end
return h/3*total
end