' ******************************************************************
' This program computes an integral by the Gauss-Legendre method.
' The result is compared with the analytical solution.
'
' The example function is:
'
' F(x) = x * exp(-x)
'
' The analytical solution is:
'
'        (x
' G(x) = |  F(t) dt = 1 - (x + 1) * exp(-x)
'        )0
' ******************************************************************

#INCLUDE "fbmath.bi"

FUNCTION F(X AS DOUBLE) AS DOUBLE
' Function to integrate

  F = X * EXP(-X)
END FUNCTION

FUNCTION G(X AS DOUBLE) AS DOUBLE
' Integral

  G = 1 - (X + 1) * EXP(-X)
END FUNCTION

CONST N = 10

DIM AS DOUBLE X(0 TO N), Y(0 TO N)

DIM AS INTEGER I

X(0) = 0
Y(0) = 0

FOR I = 1 TO N
  X(I) = I
  Y(I) = GausLeg0(@F, X(I))  ' or Y(I) = GausLeg(@F, 0, X(I))
NEXT I

PRINT "     X       GausLeg     Exact"
PRINT "------------------------------"

FOR I = 0 TO N
  PRINT USING "#####.####"; X(I); Y(I); G(X(I))
NEXT I

SLEEP
