Skip to content
Advertisement

Implementing composite Gauss quadrature in Python

I want to implement the composite Gaussian quadrature in Python to evaluate the integral ∫01 ex2 dx. Evaluting this using Python’s quad command, I get ∫01 ex2 dx ≈ 1.46

Below is my attempt at implementing this in Python. What I expect is that as n gets larger, the closer the quadrature gets to the ‘real’ integral. However, as I vary n, the results gets smaller and smaller. What mistake am I making?

JavaScript

Advertisement

Answer

Here is the working code based on your attempt:

JavaScript

The problem was that the integral has to be rescaled to the domain of the Legendre polynomials you are using. Also, you have not used the quadrature rule correctly (it does not use the stepsize h that you have in your code.) The correct rule is:

ab f(x) dx ≈ (b-a)/2 ∑i=1nwi f((b-a)/2ξi + (a+b)/2).

With this the code above outputs:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement