39

Numerical Methods in C++ Part 2: Gauss-Legendre Integration

 5 years ago
source link: https://www.tuicool.com/articles/hit/mAnMzyZ
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Welcome back to a new post at thoughts-on-cpp.com. In this post, I would like to discuss the Gauss integration algorithm, more precisely the Gauss-Legendre integration algorithm. The Gauss-Legendre integration is the most known form of the Gauss integrations.Others are

  • Gauss-Tschebyschow
  • Gauss-Hermite
  • Gauss-Laguerre
  • Gauss-Lobatto
  • Gauss-Kronrod

The idea of the Gauss integration algorithm is to approximate, similar to theSimpson Rule, the function f(x) by

Jfi26nV.png!web

While w(x) is a weighting function, ye6NZrb.png!web is a polynomial function ( Legendre-Polynomials ) with defined nodes rIV3aye.png!web which can be exactly integrated. A general form for a range of a-b looks like the following.

MvYVNj3.png!web

The Legendre-Polynomials are defined by the general formula and its derivative

e2mMRvF.png!web

yaEVFzi.png!web

The following image is showing the 3rd until the 7th Legendre Polynomials, the 1st and 2nd polynomials are just 1 and x and therefore not necessary to show.

uu6ZNzR.png!web

Let’s have a closer look at the source code :

The integral is done by the gaussLegendreIntegral (line 69) function which is initializing the LegendrePolynomial class and afterward solving the integral (line 77 – 80). Something very interesting to note: We need to calculate the Legendre-Polynomials only once and can use them for any function of order  n in the range a-b . The Gauss-Legendre integration is therefore extremely fast for all subsequent integrations.

The method calculatePolynomialValueAndDerivative is calculating the value (line 50) at a certain node rIV3aye.png!web and its derivative (line 51). Both results are used at method calculateWeightAndRoot to calculate the the node rIV3aye.png!web by the Newton-Raphson method (line 33 – 37).

eM7zuqu.png!web

The weight w(x) will be calculated (line 40) by

v2UFbab.png!web

As we can see in the screen capture below, the resulting approximation of

UjYf2mJ.png!web

is very accurate. We end up with an error of only 6fEF3mn.png!web . Gauss-Legendre integration works very good for integrating smooth functions and result in higher accuracy with the same number of nodes compared to  Newton-Cotes Integration . A drawback of Gauss-Legendre integration might be the performance in case of dynamic integration where the number of nodes are changing.

RRrq2yn.gif

Did you like the post?

What are your thoughts?

Feel free to comment and share this post.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK