# Numerical Analysis/Gaussian Quadrature

For the first example, I just want an example to show that the solution is exact for polynomials of degree $2n-1$, using the $n$th degree Legendre polynomial. I'm going to approximate $\int _{-1}^{1}x^{3}dx$.
${\begin{aligned}\int _{-1}^{1}x^{3}dx&=\left({\frac {1}{\sqrt {3}}}\right)^{3}+\left({\frac {-1}{\sqrt {3}}}\right)^{3}\\{\frac {1}{4}}1^{4}-{\frac {1}{4}}(-1)^{4}&=\left({\frac {1}{3{\sqrt {3}}}}\right)-\left({\frac {1}{3{\sqrt {3}}}}\right)\\{\frac {1}{4}}-{\frac {1}{4}}&=0\end{aligned}}$
This problem accurately illustrates the method for solving problems using the Gaussian Quadrature algorithm. Note that the zeros of the Legendre polynomials of degree $n$ are ${\frac {1}{\sqrt {3}}}$ and $-{\frac {1}{\sqrt {3}}}$.
We can see from the previous example that this method works quite well if we are integrating from $-1$ to $1$, but in application, we rarely want to integrate over such a simple region. So in our next example, we will show that this technique is also effective if we change the limits of integration, as seen on the Wikipedia page, then we will solve the example $\int _{4}^{14}x^{3}dx$
${\begin{aligned}\int _{4}^{14}x^{3}dx&={\frac {14-4}{2}}\sum _{i=1}^{2}f\left({\frac {14-4}{2}}x_{i}+{\frac {14+4}{2}}\right)\\{\frac {1}{4}}14^{4}-{\frac {1}{4}}4^{4}&=5\left(\left(5{\frac {1}{\sqrt {3}}}+9\right)^{3}+(\left(5{\frac {-1}{\sqrt {3}}}+9\right)^{3}\right)\\9540&=5\left({\frac {125}{3{\sqrt {3}}}}+{\frac {875}{3}}+{\frac {1215}{\sqrt {3}}}+729-{\frac {125}{3{\sqrt {3}}}}+{\frac {875}{3}}-{\frac {1215}{\sqrt {3}}}+729\right)\\9540&=5\left(1908\right)\\9540&=9540\end{aligned}}$
We will next show how to solve a problem that isn't a simple polynomial. We will approximate $\int _{-1}^{1}e^{x}dx$ using a two point Gaussian approximation, and discuss the error analysis.
${\begin{aligned}\int _{-1}^{1}e^{x}dx&\approx e^{\frac {1}{\sqrt {3}}}+e^{\frac {-1}{\sqrt {3}}}\\e-{\frac {1}{e}}&\approx e^{\frac {1}{\sqrt {3}}}+{\frac {1}{e^{\frac {1}{\sqrt {3}}}}}\end{aligned}}$
We will next analyze this error, looking at the actual error followed by finding the error bound. We denote the approximation by $f_{approx}$ and the exact solution by $f_{exact}$.
${\begin{aligned}\left|f_{exact}-f_{approx}\right|&=\left|e-{\frac {1}{e}}-e^{\frac {1}{\sqrt {3}}}-{\frac {1}{e^{\frac {1}{\sqrt {3}}}}}\right|\\&=0.00770629937787233598077\end{aligned}}$
The theoretical error bound when using the Legendre polynomial method is
${\begin{aligned}\left|f_{exact}-f_{approx}\right|&\leq {\frac {(b-a)^{2n+1}(n!)^{4}}{(2n+1)\left[(2n)!\right]^{3}}}f^{(2n)}\left(\xi \right),a<\xi <b\\&\leq {\frac {2^{5}2^{4}}{5*4!^{3}}}e\\&={\frac {2^{9}}{5*24^{3}}}e\\&=0.02013542095154848322489\end{aligned}}$
In our example, the actual error was well within the error bound. We also see that with only two calculations, this is a very good algorithm for approximating integrals quickly with relatively good accuracy.

## Exercises

1. Show that Gaussian Quadrature can solve exactly general cubic polynomials.
a) Set up the integral:
Solution:
$\int _{-1}^{1}f(x)dx=\int _{-1}^{1}\left(ax^{3}+bx^{2}+cx+d\right)dx$
b) Evaluate the integral:
Solution:
${\begin{aligned}\int _{-1}^{1}f(x)dx&={\frac {a}{4}}1^{4}-{\frac {a}{4}}(-1)^{4}+{\frac {b}{3}}1^{3}-{\frac {b}{3}}(-1)^{3}+{\frac {c}{2}}1^{2}-{\frac {c}{2}}(-1)^{2}+d-(-d)\\\int _{-1}^{1}f(x)dx&={\frac {2}{3}}b+2d\end{aligned}}$
c) Evaluate the approximation:
Solution:
${\begin{aligned}\sum _{i=1}^{2}f\left(x_{i}\right)&=a\left({\frac {1}{\sqrt {3}}}\right)^{3}+a\left({\frac {-1}{\sqrt {3}}}\right)^{3}+b\left({\frac {1}{\sqrt {3}}}\right)^{2}+b\left({\frac {-1}{\sqrt {3}}}\right)^{2}+c\left({\frac {1}{\sqrt {3}}}\right)+c\left({\frac {-1}{\sqrt {3}}}\right)+2d\\\sum _{i=1}^{2}f\left(x_{i}\right)&={\frac {2}{3}}b+2d\end{aligned}}$
d) Do parts b and c correspond?
Solution:
Yes
2. Approximate $\int _{-1}^{1}sin^{2}(x)cos(x)dx$ using Gaussian Quadrature
a) Evaluate the problem symbolically:
Solution:
${\begin{aligned}\int _{-1}^{1}sin^{2}(x)cos(x)dx&=\int _{-1}^{1}cosxdx+\int _{-1}^{1}cos^{3}xdx\\&={\frac {2sin^{3}(1)}{3}}\\&\approx 0.397215491060637\end{aligned}}$
b) Evaluate the approximation:
Solution:
${\begin{aligned}\sum _{i=1}^{2}f\left(x_{i}\right)&=sin^{2}\left({\frac {1}{\sqrt {3}}}\right)cos\left({\frac {1}{\sqrt {3}}}\right)+sin^{2}\left({\frac {-1}{\sqrt {3}}}\right)cos\left({\frac {-1}{\sqrt {3}}}\right)\\&\approx 0.49923418313484188\end{aligned}}$
c) Find the actual error
Solution:
$\left|f_{exact}-f_{approx}\right|\approx 0.102018692074204$
d) Find the error bound
Solution:
${\begin{aligned}\left|f_{exact}-f_{approx}\right|&\leq {\frac {(b-a)^{2n+1}(n!)^{4}}{(2n+1)\left[(2n)!\right]^{3}}}f^{(2n)}\left(\xi \right),a<\xi <b\\&\leq {\frac {2^{5}2^{4}}{5*4!^{3}}}f^{4}(\xi )\\&\leq {\frac {2^{9}}{5*24^{3}}}*20.1824\\&\leq 0.14949925\end{aligned}}$
d) Is the actual error less than the error bound?
Solution:
Yes
3. If $x_{1},x_{2},....,x_{n}$ are the roots of the $n$th Legendre Polynomial $P_{n}(x)$ and that for each $i=1,2,....,n$, the numbers $c_{i}$ are defined by
$c_{i}=\int _{-1}^{1}\prod _{j=1,j\neq i}^{n}{\frac {x-x_{j}}{x_{i}-x_{j}}}dx$
Prove that if $P(x)$ is any polynomial of degree less than $2n$, then
$\int _{-1}^{1}P(x)dx=\sum _{i=1}^{n}c_{i}P(x_{i}).$
Proof:
The set that is relevant to this proof is the set of Legendre Polynomials, a collection orthogonal polynomials ${P_{0}(x),P_{1}(x),...,P_{n}(x),...,}$ with properties:
* For each $n,P_{n}(x)$ is a monic polynomial of degree $n$.
* $\int _{-1}^{1}{P(x)}\times {P_{n}(x)}dx=0$ whenever $P(x)$ is a polynomial of degree less that $n$.
First we use polynomial division, to get $Q(x)={\frac {P(x)}{P_{n}(x)}}$ with a remainder term $R(x)$, so that $P(x)=Q(x)\times P_{n}(x)+R(x)$ and the two polynomials $Q(x)$ and $R(x)$ are of degree less than $n$ and $P_{n}(x)$ is of degree $n$. Then using this fact we can rewrite the original integral into the form
    $\int _{-1}^{1}P(x)dx=\int _{-1}^{1}{P_{n}(x)}\times {Q(x)}dx+\int _{-1}^{1}R(x)dx$.
Since the degree of $P(x)$ is less than twice the degree of the w: Legendre polynomial, then the degree of $Q(x)$ is less than the degree of $P_{n}(x)$. So (by Legendre property 2 started above), $\int _{-1}^{1}Q(x)\times P_{n}(x)dx=0$. Thus we see that $\int _{-1}^{1}P(x)dx=\int _{-1}^{1}R(x)dx$. We do not know what $R(x)$ is, but we know it has degree less than $n$ so it equals the polynomial that interpolates it at the values $x_{1},x_{2},....,x_{n}$. We will use the w: Lagrange polynomial form. Since $x_{i}$ is a roots of $P_{n}$ for each $i=1,2,...,n$, we have
    $P(x_{i})=Q(x_{i})\times P_{n}(x_{i})+R(x_{i})=Q(x_{i})\times 0+R(x_{i})=R(x_{i})$
Since $R(x)$ is a polynomial of degree less than $n$, then Lagrange polynomial for $R(x)$ is
    $R(x)=\sum _{i=1}^{n}\left(P(x_{i})\prod _{j=1,j\neq i}^{n}{\frac {x-x_{j}}{x_{i}-x_{j}}}\right)$.
We next integrate this polynomial by
    $\int _{-1}^{1}P(x)dx=\int _{-1}^{1}R(x)dx=\int _{-1}^{1}\left(\sum _{i=1}^{n}\left(P(x_{i})\prod _{j=1,j\neq i}^{n}{\frac {x-x_{j}}{x_{i}-x_{j}}}\right)\right)dx$.
Since $P(x_{i})$ is a constant, we will pull that outside the integral, giving us
    $\int _{-1}^{1}P(x)dx=\left(\sum _{i=1}^{n}P(x_{i})\left(\int _{-1}^{1}\prod _{j=1,j\neq i}^{n}{\frac {x-x_{j}}{x_{i}-x_{j}}}dx\right)\right)$.
We then note that
    $c_{i}=\int _{-1}^{1}\prod _{j=1,j\neq i}^{n}{\frac {x-x_{j}}{x_{i}-x_{j}}}dx$,
so we will substitute $c_{i}$ into our integration, giving us
    $\int _{-1}^{1}P(x)dx=\sum _{i=1}^{n}c_{i}P(x_{i}).$
This completes the proof.

#### Gaussian Quadrature Example

I realized that there was insufficient information after the derived and solved sample on Gaussian Quadrature thus i took the pain to edit this wikiversity page by adding a solved example to the information already on there and below is what i factored in.
Find the constants C_0, C_1, and x_1 so that the quadrature formula
    $\int _{0}^{1}f(x)\,dx=C_{0}f(0)+C_{1}f(x_{1}).$
has the highest possible degree of precision.
Solution
Since there are three unknowns, C_0, C_1 and x_1, we will expect the formula to be exact for
    $f(x)=1,x,and\,\ x^{2}$
Thus
    $f(x)=1,\ \int _{0}^{1}f(x)\,dx=1=C_{0}+C_{1}\ \qquad \ Equation1,\qquad f(x)=x,\ \int _{0}^{1}f(x)\,dx={\frac {1}{2}}=C_{1}x_{1}\ \qquad \ Equation2.\qquad f(x)=x^{2},\ \int _{0}^{1}f(x)\,dx={\frac {1}{3}}=C_{1}x_{1}^{2}.$
Equation 2 and 3 will yield.
    ${\frac {c_{1}x_{1}}{c_{1}x_{1}^{2}}}=x_{1}={\frac {2}{3}}.\qquad C_{1}={\frac {3}{4}}.\qquad C_{0}={\frac {1}{4}}.$
Hence
    $\int _{0}^{1}f(x)\,dx={\frac {1}{4}}f(0)+{\frac {3}{4}}f({\frac {2}{3}})$
Now,
    $f(x)=x^{3}.\qquad \int _{0}^{1}x^{3}\,dx={\frac {1}{4}}$
And
    ${\frac {1}{4}}(0)^{3}+{\frac {3}{4}}({\frac {2}{3}})^{3}={\frac {2}{9}}.$
Thus the degree of the precision is 2