Gauss Legendre

Integration

Your Progress in this Chapter

0%

3 sections

Gauss Legendre

Section 7.2 of 723 min7 code examples

Gauss-Legendre Quadrature is a numerical integration technique designed to yield maximum algebraic precision with a minimal number of function evaluations. Unlike standard Newton-Cotes methods (such as the Trapezoidal or Simpson's rules) that use fixed, equally spaced evaluation points, Gauss-Legendre quadrature treats both the evaluation locations (nodes) and their corresponding multipliers (weights) as free variables.

Mathematical Foundation To evaluate a definite integral over a standardized interval [−1,1][-1, 1]:

∫−11f(x),dx≈∑i=1nwif(xi)\int_{-1}^{1} f(x) , dx \approx \sum_{i=1}^{n} w_i f(x_i)

The nodes xix_i are selected as the roots of the n-th degree Legendre polynomial Pn(x)P_n(x). The weights wiw_i are calculated by integrating the Lagrange interpolating polynomials over the interval:

wi=2(1−xi2)[Pn′(xi)]2w_i = \frac{2}{(1 - x_i^2) [P_n'(x_i)]^2}

An n-point Gauss-Legendre rule integrates polynomials of degree up to 2n - 1 exactly.

Domain Transformation (Mapping) For integrals over an arbitrary real interval [a, b], a linear change of variables maps the domain to [-1, 1]:

x(t)=b−a2t+a+b2x(t) = \frac{b - a}{2} t + \frac{a + b}{2}

dx=b−a2dtdx = \frac{b - a}{2} dt

The transformation yields:

∫abf(x),dx=b−a2∑i=1nwif(b−a2xi+a+b2)\int_{a}^{b} f(x) , dx = \frac{b - a}{2} \sum_{i=1}^{n} w_i f\left( \frac{b - a}{2} x_i + \frac{a + b}{2} \right)

Applied Examples (Solved via Simple Solver)

.. Admonition:: Example 1 : Exact Polynomial Integration (2-Point Rule)

Evaluate

∫02(x3−3x+2),dx\int_{0}^{2} (x^3 - 3x + 2) , dx

Example 1C#

1
2
Code is ready to run
OutputFrom the book
I = 2.0000

Exact Analytical Result:

[x44−3x22+2x]02=(4−6+4)−0=2\left[ \frac{x^4}{4} - \frac{3x^2}{2} + 2x \right]_0^2 = (4 - 6 + 4) - 0 = 2

.. Admonition:: Example 2 : Transcendental Function

∫0πsin⁡(x),dx\int_{0}^{\pi} \sin(x) , dx

Example 2C#

1
2
Code is ready to run
OutputFrom the book
I = 2.0000

Exact Analytical Result:

[−cos⁡(x)]0π=1−(−1)=2[-\cos(x)]_0^\pi = 1 - (-1) = 2

.. Admonition:: Example 3 : Infinite bound

Evaluate

∫0∞e−x2(ln⁡(x))2,dx\int_{0}^{\infty} e^{-x^2}(\ln(x))^2 , dx

Example 3C#

1
2
Code is ready to run
OutputFrom the book
I = 1.94752

Exact Analytical Result:

∫0∞e−x2(ln⁡(x))2,dx=π16(π2+2(γ+ln⁡(4))2)=1.94752\int_{0}^{\infty} e^{-x^2}(\ln(x))^2 , dx = \frac{\sqrt{\pi}}{16}\left(\pi^2 + 2(\gamma + \ln(4))^2\right) = 1.94752

Multiple Integral

SepalSolver can handle multiple integral via IntegralN. IntegralN is built on Integral running over multiply direction recursively. It is abole to compute upto 4 dimensional integrals efficiently.

.. Admonition:: Example 4 : Hypercube domain in 3 dimensions

Integrate the function f(x,y,z)=xyzf(x, y, z) = xyz over the region where xx ranges from 00 to 11, yy ranges from 11 to 22, and zz ranges from 22 to 33, which can be expressed as:

∫01∫12∫23xyz dz dy dx\int_{0}^{1} \int_{1}^{2} \int_{2}^{3} x y z \, dz \, dy \, dx

Example 4C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Code is ready to run
OutputFrom the book
The triple integral of x*y*z is approximately: 1.8750

.. Admonition:: Example 5 :

Integrate the function f(x,y)=xyf(x, y) = xy over the region where xx ranges from 00 to 11, yy ranges from x2x^2 to 22, which can be expressed as:

∫01∫x22xy dy dx\int_{0}^{1} \int_{x^2}^{2} x y \, dy \, dx

Example 5C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Code is ready to run
OutputFrom the book
The triple integral of x*y*z is approximately: 0.9167

.. Admonition:: Example 6 : Functional boundary

Integrate the function f(x,y,z)=xyzf(x, y, z) = xyz over the region where xx ranges from 00 to 11, yy ranges from x2x^2 to x\sqrt{x}, and zz ranges from 22 to 33, which can be expressed as:

∫01∫x2x∫23xyz dz dy dx\int_{0}^{1} \int_{x^2}^{\sqrt{x}} \int_{2}^{3} x y z \, dz \, dy \, dx

Example 6C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Code is ready to run
OutputFrom the book
The triple integral of x*y*z is approximately: 0.2083

.. Admonition:: Example 7 : Functional boundary

Integrate the function f(x,y,z)=xyzf(x, y, z) = xyz over the region where xx ranges from 00 to 11, yy ranges from x2x^2 to x\sqrt{x}, and zz ranges from xyxy to x+yx+y, which can be expressed as:

∫x1x2∫x2x∫xyx+yxyz dz dy dx\int_{x_1}^{x_2} \int_{x^2}^{\sqrt{x}} \int_{xy}^{x+y} x y z \, dz \, dy \, dx

Example 7C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Code is ready to run
OutputFrom the book
The triple integral of x*y*z is approximately: 0.0641

How many points do you need?

Integrate e^(−x²) from 0 to 3. It has no closed form, so every answer is an approximation — the only question is how good, and at what cost.

By handThe trapezoidal rule with a fixed width. Add more points, get more digits.
1Func<double, double> f = x => Exp(-x * x);2double a = 0, b = 3;34foreach (int n in new[] { 10, 100, 1000, 10000 })5{6 double h = (b - a) / n, sum = 0;7 for (int i = 1; i < n; i++) sum += f(a + i * h);8 double area = (h / 2) * (f(a) + 2 * sum + f(b));9 Console.WriteLine($"n = {n,-6} area = {area:F12}");10}

Output

n = 10     area = 0.886202033637
n = 100    area = 0.886207292750
n = 1000   area = 0.886207347704
n = 10000  area = 0.886207348254
With Reineur SolverGauss-Legendre picks its own points, and picks them well.
1double area = Integral(x => Exp(-x * x), 0, 3);2Console.WriteLine($"Integral area = {area:F12}");

Output

Integral   area = 0.886207348260

Ten thousand evaluations buys ten correct digits. Integral gets twelve in one call. The trapezoidal error falls with the square of the spacing, so each extra digit costs ten times the work — reaching full double precision by hand would need something like a hundred million points.

Try both in the terminal
Takes a free account.