How to resolve the algorithm Numerical integration/Gauss-Legendre Quadrature step by step in the J programming language

Published on 12 May 2024 09:40 PM
#J

How to resolve the algorithm Numerical integration/Gauss-Legendre Quadrature step by step in the J programming language

Table of Contents

Problem Statement

For this, we first need to calculate the nodes and the weights, but after we have them, we can reuse them for numerious integral evaluations, which greatly speeds up the calculation compared to more simple numerical integration methods.

Task description Similar to the task Numerical Integration, the task here is to calculate the definite integral of a function

f ( x )

{\displaystyle f(x)}

, but by applying an n-point Gauss-Legendre quadrature rule, as described here, for example. The input values should be an function f to integrate, the bounds of the integration interval a and b, and the number of gaussian evaluation points n. An reference implementation in Common Lisp is provided for comparison. To demonstrate the calculation, compute the weights and nodes for an 5-point quadrature rule and then use them to compute:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Numerical integration/Gauss-Legendre Quadrature step by step in the J programming language

Source code in the j programming language

NB. returns coefficents for yth-order Legendre polynomial
getLegendreCoeffs=: verb define M.
  if. y<:1 do. 1 {.~ - y+1 return. end.
  (%~ <:@(,~ +:) -/@:* (0;'') ,&> [: getLegendreCoeffs&.> -&1 2) y
)

getPolyRoots=: 1 {:: p.                                NB. returns the roots of a polynomial
getGaussLegendreWeights=: 2 % -.@*:@[ * (*:@p.~ p..)   NB. form: roots getGaussLegendreWeights coeffs
getGaussLegendrePoints=: (getPolyRoots ([ ,: getGaussLegendreWeights) ])@getLegendreCoeffs

NB.*integrateGaussLegendre a Integrates a function u with a n-point Gauss-Legendre quadrature rule over the interval [a,b]
NB. form: npoints function integrateGaussLegendre (a,b)
integrateGaussLegendre=: adverb define
:
  'nodes wgts'=. getGaussLegendrePoints x
  -: (-~/ y) * wgts +/@:* u -: nodes p.~ (+/ , -~/) y
)


   5 ^ integrateGaussLegendre _3 3
20.0356
   -~/ ^ _3 3   NB. true value
20.0357


  

You may also check:How to resolve the algorithm One-dimensional cellular automata step by step in the ALGOL W programming language
You may also check:How to resolve the algorithm Particle fountain step by step in the EasyLang programming language
You may also check:How to resolve the algorithm Ackermann function step by step in the AutoIt programming language
You may also check:How to resolve the algorithm Constrained random points on a circle step by step in the C# programming language
You may also check:How to resolve the algorithm Minkowski question-mark function step by step in the FreeBASIC programming language