# Approximation of the integral using Gauss-Legendre quadrature formula

• MHB
• mathmari
In summary, the conversation discusses the use of a quadrature formula to approximate the integral of a function. It also explores the concept of orthogonal polynomials and their use in calculating the nodes and weights for the quadrature formula. There is a discrepancy in the calculations, which is later resolved by correcting a sign mistake.

#### mathmari

Gold Member
MHB
Hey! :giggle:

Let $\displaystyle{I_n(f)=\sum_{i=0}^na_if(x_i)}$ be a quadrature formula for the approximate calculation of the integral $I(f)=\int_a^bf(x)\, dx$.

Show that a polynomial $p$ of degree $2n+2$ exists such that $I_n(p)\neq I(p)$.

Calculate the approximation of the integral $$\int_0^{\pi}\sqrt{\sin (x)}\, dx$$ using Gauss-Legendre quadrature formula with $2$ nodes.
For the second part I have done the following :

We get the orthogonal polynomials by the recurive formula:
\begin{align*}&P_n(x)=(a_n+x)P_{n-1}(x)+c_nP_{n-2}(x) \\ &P_0=1, \ P_{-1}=0\end{align*} with \begin{equation*}a_n=-\frac{\langle x\cdot P_{n-1}, P_{n-1}\rangle_w}{\langle P_{n-1}, P_{n-1}\rangle_w}, \ \ \ , c_n=-\frac{\langle P_{n-1}, P_{n-1}\rangle_w}{\langle P_{n-2}, P_{n-2}\rangle_w}\end{equation*}
We have :
\begin{equation*}P_1(x)=(a_1+x)P_{0}(x)+c_1P_{-1}(x)=(a_1+x)\cdot 1+c_1\cdot 0=a_1+x \end{equation*}
We need to calculate $a_1$:
\begin{equation*}a_1=-\frac{\langle x\cdot P_{0}, P_{0}\rangle_w}{\langle P_{0}, P_{0}\rangle_w}=-\frac{\langle x, 1\rangle_w}{\langle 1, 1\rangle_w}\end{equation*}
We have that \begin{align*}&\langle x, 1\rangle_w=\int_0^{\pi}x\cdot 1\cdot w(x)\, dx=\int_0^{\pi} x\cdot 1\, dx =\frac{\pi^2}{2} \\ &\langle 1, 1\rangle_w=\int_0^{\pi}1\cdot 1\cdot 1\, dx=\int_0^{\pi} 1\, dx =\pi\end{align*}
So we have that $a_1=-\frac{\pi}{2}$. Therefore we get $P_1=-\frac{\pi}{2}+x$.

We have that
\begin{equation*}P_2(x)=(a_2+x)P_{1}(x)+c_2P_{0}(x)=(a_2+x)\cdot \left (-\frac{\pi}{2}+x\right )+c_2\end{equation*}
We need to calculate $a_2$ and $c_2$:
\begin{equation*}a_2=-\frac{\langle x\cdot P_{1}, P_{1}\rangle_w}{\langle P_{1}, P_{1}\rangle_w}=-\frac{\langle x\cdot \left (-\frac{\pi}{2}+x\right ), -\frac{\pi}{2}+x\rangle_w}{\langle -\frac{\pi}{2}+x, -\frac{\pi}{2}+x\rangle_w}=-\frac{\langle -\frac{\pi}{2}x+x^2, -\frac{\pi}{2}+x\rangle_w}{\langle -\frac{\pi}{2}+x, -\frac{\pi}{2}+x\rangle_w}\end{equation*}
We have that \begin{align*}&\langle -\frac{\pi}{2}x+x^2, -\frac{\pi}{2}+x\rangle_w=\int_0^{\pi}\left (-\frac{\pi}{2}x+x^2\right )\cdot \left (-\frac{\pi}{2}+x\right )\cdot 1\, dx=\frac{\pi^4}{24} \\ &\langle -\frac{\pi}{2}+x, -\frac{\pi}{2}+x\rangle_w=\int_0^{\pi}\left (-\frac{\pi}{2}+x\right )\cdot \left (-\frac{\pi}{2}+x\right )\cdot 1\, dx=\frac{\pi^3}{12}\end{align*}
So we have that $a_2=-\frac{\frac{\pi^4}{24}}{\frac{\pi^3}{12}}=-\frac{\pi}{2}$ and $c_2=-\frac{\langle P_{1}, P_{1}\rangle_w}{\langle P_{0}, P_{0}\rangle_w}=-\frac{\frac{\pi^3}{12}}{\pi}=-\frac{\pi^2}{12}$.
Therefore we get $P_2=(-\frac{\pi}{2}+x)\cdot \left (-\frac{\pi}{2}+x\right )-\frac{\pi^2}{12}$.

But this is not correct, is it? Do we not have to get $P_2(x)=\frac{1}{2}\left (3x^2-1\right )$ ? :unsure:

Last edited by a moderator:
Hey mathmari,

According to wiki, we should use orthogonal polynomials on $[-1,1]$ with the convention that $P_n(1)=1$, and apply a change of interval to map it to $[0,\pi]$.
Your expected $P_2(x)=\frac 12(3x^2-1)$ is based on polynomials on $[-1,1]$.
But your calculations are on $[0,\pi]$ and it is not clear to me, which convention applies in that case, nor whether we can use the recursive formulas as given. :unsure:

The wiki page gives $x_i=\pm\frac{1}{\sqrt 3}$ and $w_i=1$ for a quadrature with 2 nodes on the interval $[-1,1]$.
If I apply a change of interval I think we get $\tilde x_i=\frac\pi 2\left(1\pm\frac 1{\sqrt 3}\right)$ and $\tilde w_i=\frac\pi 2$. :unsure:

Klaas van Aarsen said:
The wiki page gives $x_i=\pm\frac{1}{\sqrt 3}$ and $w_i=1$ for a quadrature with 2 nodes on the interval $[-1,1]$.
If I apply a change of interval I think we get $\tilde x_i=\frac\pi 2\left(1\pm\frac 1{\sqrt 3}\right)$ and $\tilde w_i=\frac\pi 2$. :unsure:

So the polynomial I found must be correct, right?

The nodes are the roots of $P_2(x)=x^2-\pi x+\frac{\pi^2}{6}$, which are $x_1=\frac\pi 2\left(1-\frac 1{\sqrt 3}\right)$, $x_2=\frac\pi 2\left(1+\frac 1{\sqrt 3}\right)$.

The weights are:
\begin{align*}w_1&= \int_0^{\pi}w(x)\prod_{j=1, j\neq 1}^2\frac{x-x_j}{x_1-x_j}\, dx=\int_0^{\pi}w(x)\cdot \frac{x-x_2}{x_1-x_2}\, dx=\int_0^{\pi}1\cdot \frac{x-\frac\pi 2\left(1+\frac 1{\sqrt 3}\right)}{\frac\pi 2\left(1-\frac 1{\sqrt 3}\right)-\frac\pi 2\left(1+\frac 1{\sqrt 3}\right)}\, dx\\ & =\int_0^{\pi}\frac{x-\frac\pi 2\left(1+\frac 1{\sqrt 3}\right)}{-\frac{\pi}{\sqrt{3}}}\, dx=\frac{\pi}{2}\\ w_2&=\int_0^{\pi}w(x)\prod_{j=1, j\neq 2}^2\frac{x-x_j}{x_2-x_j}\, dx=\int_0^{\pi}w(x)\frac{x-x_1}{x_2-x_1}\, dx=\int_0^{\pi}1\cdot \frac{x-\frac\pi 2\left(1-\frac 1{\sqrt 3}\right)}{\frac\pi 2\left(1-\frac 1{\sqrt 3}\right)-\frac\pi 2\left(1+\frac 1{\sqrt 3}\right)}\, dx \\ & =\int_0^{\pi} \frac{x-\frac\pi 2\left(1-\frac 1{\sqrt 3}\right)}{-\frac{\pi}{\sqrt{3}}}\, dx=-\frac{\pi}{2}\end{align*}
Therefore the formula is \begin{align*}&\int_0^{\pi}f(x)\, dx\approx \sum_{i=1}^2f(x_i)\cdot w_i=f(x_1)\cdot w_1+f(x_2)\cdot w_2 \\ & \Rightarrow \int_0^{\pi}\sqrt{\sin (x)}\, dx=\sqrt{\sin \left (\frac\pi 2\left(1-\frac 1{\sqrt 3}\right)\right )}\cdot \frac{\pi}{2}+\sqrt{\sin \left (\frac\pi 2\left(1+\frac 1{\sqrt 3}\right)\right )}\cdot \left (-\frac{\pi}{2}\right )=0.7850\cdot 1.5708-0.7850\cdot 1.5708=0\end{align*}
But this is not correct, shouldn't the result be about $2.3963$ ? Have I done something wrong at the calculations or is the formula wrong? I don't really see a mistake. :unsure:

mathmari said:
But this is not correct, shouldn't the result be about $2.3963$ ? Have I done something wrong at the calculations or is the formula wrong? I don't really see a mistake.
I believe $w_2$ should be $+\frac\pi 2$. If we substitute that, we get indeed about $2.3963$.
There seems to be a sign mistake where you substituted into $x_2-x_1$ in the denominator.

Klaas van Aarsen said:
I believe $w_2$ should be $+\frac\pi 2$. If we substitute that, we get indeed about $2.3963$.
There seems to be a sign mistake where you substituted into $x_2-x_1$ in the denominator.

Ah yes, you are right!

So we have:
\begin{align*}&\int_0^{\pi}f(x)\, dx\approx \sum_{i=1}^2f(x_i)\cdot w_i=f(x_1)\cdot w_1+f(x_2)\cdot w_2 \\ & \Rightarrow \int_0^{\pi}\sqrt{\sin (x)}\, dx=\sqrt{\sin \left (\frac\pi 2\left(1-\frac 1{\sqrt 3}\right)\right )}\cdot \frac{\pi}{2}+\sqrt{\sin \left (\frac\pi 2\left(1+\frac 1{\sqrt 3}\right)\right )}\cdot \frac{\pi}{2}=0.7850\cdot 1.5708+0.7850\cdot 1.5708=2.4662\end{align*}
There is a difference to $2.3963$,but is this a mistake at the calculations or is this an error because of the roundings? :unsure:

mathmari said:
So we have:
\begin{align*}&\int_0^{\pi}f(x)\, dx\approx 2.4662\end{align*}
There is a difference to $2.3963$,but is this a mistake at the calculations or is this an error because of the roundings?
Neither. (Shake)

It's a quadrature approximation with only 2 nodes. So we won't find the exact value of the integral.
Apparently the method has an error of about $0.07$ in this case.

Klaas van Aarsen said:
Neither. (Shake)

It's a quadrature approximation with only 2 nodes. So we won't find the exact value of the integral.
Apparently the method has an error of about $0.07$ in this case.

Ah ok, I see! Thank you very much!