Approximation of the integral using Gauss-Legendre quadrature formula

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.
  • #1
mathmari
Gold Member
MHB
5,049
7
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:
Mathematics news on Phys.org
  • #2
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:
 
  • #3
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:
 
  • #4
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. 🤔
 
  • #5
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:
 
  • #6
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. 🤔
 
  • #7
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! 🤩
 

FAQ: Approximation of the integral using Gauss-Legendre quadrature formula

What is the Gauss-Legendre quadrature formula?

The Gauss-Legendre quadrature formula is a numerical method for approximating the value of a definite integral. It uses a specific set of points and weights to calculate the integral, resulting in a more accurate approximation than other numerical methods.

How does the Gauss-Legendre quadrature formula work?

The formula works by dividing the interval of integration into smaller subintervals and calculating the integral at each of these points using a weighted sum of function values. These points and weights are predetermined and optimized to provide a more accurate approximation of the integral.

What are the advantages of using the Gauss-Legendre quadrature formula?

Compared to other numerical methods, the Gauss-Legendre quadrature formula provides a more accurate approximation of the integral with fewer function evaluations. It is also more efficient and less prone to error, making it a popular choice for scientists and engineers.

What are the limitations of the Gauss-Legendre quadrature formula?

One limitation of the formula is that it is only applicable to integrals with finite bounds. It also requires the function to be evaluated at specific points, which may be time-consuming for complex functions. Additionally, the accuracy of the approximation may decrease for integrals with highly oscillatory or singular functions.

How can I implement the Gauss-Legendre quadrature formula?

The formula can be implemented using various programming languages, such as MATLAB or Python. There are also built-in functions and libraries available that can perform the calculations for you. It is important to understand the underlying principles and equations of the formula before attempting to implement it in any programming language.

Similar threads

Replies
4
Views
937
Replies
2
Views
816
Replies
4
Views
1K
Replies
11
Views
881
Replies
1
Views
1K
Back
Top