Gauss-Legendre Quadrature of order 10

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Software Libraries (https://www.hpmuseum.org/forum/forum-10.html)
+--- Forum: HP Prime Software Library (https://www.hpmuseum.org/forum/forum-15.html)
+--- Thread: Gauss-Legendre Quadrature of order 10 (/thread-24074.html)



Gauss-Legendre Quadrature of order 10 - Namir - 2025-09-20

I used Claude AI to generate the code for the HP-Prime to perform Gauss-Legendre Quadrature of order 10. I then edited the code to allow you to apply the algorithm multiple times in a given integration range in order to increase the accuracy of the result.

The function MYFX defines the integrated  function. 

The function GaussQuad10 defines the integration function. It has the following parameters:
1. Parameter a is the lower integration range.
2. Parameter b is the upper integration range.
3. Parameter h is the increment value. When h is equal to b - a, the function performs the integration on the entire range (a, b). When h is smaller than (b - a), the function calculates multiple sub-integrals to yield a more accurate result.

Code:
// Local function to integrate
MYFX(x)
BEGIN
  RETURN 1/x;
END;
 
EXPORT GaussQuad10(a,b,h)
BEGIN
  // Gauss-Legendre 10th Order Quadrature
  // HP Prime Calculator Program
 
  LOCAL  I, S, C, D;
  LOCAL Xn, W, T, b1, Stot;
 
  IF h <= 0 OR h > (b - a) THEN
    h = b - a;
  END;
 
  // Initialize arrays for 10 points
  Xn := MAKELIST(0, I, 1, 10);
  W := MAKELIST(0, I, 1, 10);
  T := MAKELIST(0, I, 1, 10);
 

  // Standard Gauss-Legendre nodes on [-1,1]
  T(1) := -0.9739065285171717;
  T(2) := -0.8650633666889845;
  T(3) := -0.6794095682990244;
  T(4) := -0.4333953941292472;
  T(5) := -0.1488743389816312;
  T(6) := 0.1488743389816312;
  T(7) := 0.4333953941292472;
  T(8) := 0.6794095682990244;
  T(9) := 0.8650633666889845;
  T(10) := 0.9739065285171717;
 
  // Gauss-Legendre weights
  W(1) := 0.0666713443086881;
  W(2) := 0.1494513491505806;
  W(3) := 0.2190863625159820;
  W(4) := 0.2692667193099963;
  W(5) := 0.2955242247147529;
  W(6) := 0.2955242247147529;
  W(7) := 0.2692667193099963;
  W(8) := 0.2190863625159820;
  W(9) := 0.1494513491505806;
  W(10) := 0.0666713443086881;
  Stot := 0;
 
  WHILE a < b DO
    b1 := a + h;
    // Transform nodes from [-1,1] to [a,b1]
    C := (b1 - a) / 2;
    D := (a + b1) / 2;
 
    FOR I FROM 1 TO 10 DO
      Xn(I) := C * T(I) + D;
    END;
 
    // Compute integral using Gauss-Legendre quadrature
    S := 0;
    FOR I FROM 1 TO 10 DO
      S := S + W(I) * MYFX(Xn(I));
    END;
    Stot := Stot +  C * S;
    a := a + h
  END;
 
  RETURN Stot;
END;

Example:

GaussQuad10(1,100,0.01) returns 4.605170186
compared to LN(100) = 4.60517018599

GaussQuad10(1,2,1) returns 0.69314718056
compared to LN(2) = 0.69314718056