' ******************************************************************
' This program computes the convolution product H of two functions
' F and G by the Gauss-Legendre method. The result is compared with
' the analytical solution.
'
' The example functions are:
'
' F(x) = x * exp(-x)
' G(x) = exp(-2 * x)
'
' The analytical solution is:
'
' H(x) = (F * G)(x) = (x - 1) * exp(-x) + exp(-2 * x)
' ******************************************************************

#INCLUDE "fbmath.bi"

FUNCTION F(X AS DOUBLE) AS DOUBLE
  F = X * EXP(-X)
END FUNCTION

FUNCTION G(X AS DOUBLE) AS DOUBLE
  G = EXP(-2 * X)
END FUNCTION

FUNCTION H(X AS DOUBLE) AS DOUBLE
  DIM E AS DOUBLE

  E = EXP(-X)
  H = ((X-1) + E) * E
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) = 0.1 * I
  Y(I) = Convol(@F, @G, X(I))
NEXT I

PRINT "     X        Convol     Exact"
PRINT "------------------------------"

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

SLEEP
