Skip to content
/ NQL Public

Mathematical library collecting popular quadrature algorithms implemented in MATLAB for fast and accurate approximations of definite integrals

Notifications You must be signed in to change notification settings

papadeiv/NQL

Repository files navigation

NQL - Numerical Quadrature Library

This library implements an automatic numerical integration routine in MATLAB using Newton-Cotes and Gauss-Legendre formulae. In order to run, John D'Errico's HPF toolbox must be installed.

Main structure

The library consists of

  • main.m computes the necessary parameters according to the choice of formulae provided by the user (nodes, affine map coefficients, jacobian etc...);
  • legendre.m if Gauss-Legendre formulae is selected it computes the nodes of the N-th degree Legendre's polynomial while displaying the recursive polynomials used for its construction (optional) like so
  • quadrature.m defines the workflow for the computation of the weigths associated to the Lagrangian basis' functions;
  • lagrangian_weights.m computes the aforementioned weights;
  • lagrange.m computes iteratively the NxN coefficient matrix associated to the N Lagrange's polynomials of degree N-1 and plots them over the specified interval (optional) as shown
  • extract_basis.m for each node computes the coefficients of all monomials associated recursively to Lagrange's polynomial associated to that node; The simulation can be launched by script or by calling main.m direclty from the command window. In either cases the input parameters must be specified which are
    • interval of integration I = [a,b] specified through its endpoints a and b;
    • integrand, or target function f(x), specified as kernel;
    • number of discretisation nodes N in I for the numerical quadrature;
    • optional global variables (see below).

Options

The values of the global variables have to be specified either if the user does not intend to use such options. The latter are:

  • tabulated The library can optionally choose to compute numerically the roots of the Legendre's polynomial via linear interpolation (set flag variable to 0) or to reference them from tabulated values with 289 decimal digits provided by Pomax's repository (set flag to 1);
  • precision Numerical integration famously requires a high degree of precision in floating-point arithmetic in order to obtain sufficiently accurate results. Therefore the library allows the user to select the integer number of (decimal) digits with which extract the tabulated Legendre's nodes (e.g. precision=16 sets the library to run with MATLAB's default double precision as per the IEEE 754 standard);
  • print_polynomials To display Legendre's polynomials and Lagrange's basis functions depicted above set the flag value to 1 otherwise set it to 0; if Gauss-Legendre formula is used the Lagrangian basis function are depicted in [-1,1] instead of [a,b];
  • print_quadrature To display the Lagrange's interpolating polynomial overlayed with the target kernel of integration set the flag value to 1 otherwise set it to 0; if Gauss-Legendre is used both functions are plotted in [-1,1] instead of [a,b] (see Examples below).

Accessories

To support data management and visualisation the following functions have been provided:

  • tab_legendre.m contains tabulated values of the nodes of Legendre's ploynomial up to the 32nd degree;
  • cmap.m defines a colormap based on the interpolation of 8 colors and according to the number of Legendre's and Lagrange's functions are defined;
  • doubledot.m computes the inner product between two vectors but in ascending order of values so to avoid numerical cancellation;

Examples

The user can launch one of the scripts contained in the examples directory, specifically the purposely-defined test.m script, to get accustomed with the settings and outputs of the library. In there both Newton-Cotes and Gauss-Legendre formulae are used to integrate numerically the function in the interval . The Lagrange's interpolating polynomial is reported for both formulae

Newton-Cotes (N=4) Gauss-Legendre (N=4)
Newton-Cotes (N=8) Gauss-Legendre (N=8)

Suggested benchmarks

This suite was built for educational purposes and as a quick first reference for testing more performant softwares for numerical integration. As a benchmark for its performance against fairly demanding computations in the script benchmark.m the results displayed in Figure 2 from the paper Design of quadrature rules for Müntz and Müntz-logarithmic polynomials using monomial transformation are replicated. The kernel is a sequence of Müntz monomials of the form where lambda is a real-valued exponent ranging continuosly from 0 to 40; in the simulation such continuous interval is discretised in 1000 equispaced samples while using a precision of 16 (decimal) digits for the Gauss-Legendre quadrature. With N=16 nodes the errors are

while with N=24 nodes of interpolation the errors are

where, with an abuse of notation, the a-priori estimate is referred to the L1-norm of the difference between the exact definite integral and NQL's output. The tests, ran both on a i5-1035G1 quad-core processor with 8 GB of RAM, took approximately 4 hours to complete. Observe that the library's output does satisfy the predicted 2n-1 degree of precision of a Gauss-Legendre's formula in terms of asbolute error.

About

Mathematical library collecting popular quadrature algorithms implemented in MATLAB for fast and accurate approximations of definite integrals

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages