pwl_product_integral, a C code which calculates the exact value of the integral of the product of two piecewise linear functions F(X) and G(X).

The piecewise linear function F(X) is defined by

The piecewise linear function G(X) is defined similarly, and there is no requirement that G(X) uses the same nodes as F(X), or the same number of nodes.

The task, then, is to determine the value of

        Integral ( A <= X <= B ) F(X) * G(X) dx
This value can be determined exactly, by breaking the interval ot integration into subintervals over which F(X) and G(X) are simple linear functions.

In finite element programs over 1-dimensional geometries, integrals like this may occur when assembling the stiffness matrix, but these integrals are generally treated using quadrature.

A more appropriate use for this function occurs when coarsening a finite element solution, or constructing a piecewise linear least squares finite element approximant to data that is regarded as a piecewise linear function.

In case 1, we treat the original finite element solution as the function F(X), and in case 2, we treat the data as a finite element function whose mesh is implicit in the X coordinates of the given data.

Our G_X is, in case 1, the coarse mesh and in case 2 the given mesh. To determine the finite element coefficient U(I) on this mesh, we must integrate our data function F(X) against the I-th basis function, which in this case is simply the I-th "hat function. So to use our formula for G(X), we set G_V(1:G_NUM) to 0, except that G_V(I) = 1, and compute

        U(I) = Integral ( G_X(I-1) <= X <= G_X(I+1) ) F(X) * G(X) dx
Doing this for I from 1 to G_NUM allows us to compute the coefficients of the coarsened solution (case 1) or the least squares approximant to the piecewise linear function representing the data F.


The computer code and data files described and made available on this web page are distributed under the MIT license


pwl_product_integral is available in a C version and a C++ version and a FORTRAN90 version and a MATLAB version.

Related Data and Programs:

FEM, a data directory which contains a description and examples of files that describe a finite element model.

FEM1D, a C code which applies the finite element method to a 1D linear two point boundary value problem.



  1. Hans Rudolf Schwarz,
    Finite Element Methods,
    Academic Press, 1988,
    ISBN: 0126330107,
    LC: TA347.F5.S3313.
  2. Gilbert Strang, George Fix,
    An Analysis of the Finite Element Method,
    Cambridge, 1973,
    ISBN: 096140888X,
    LC: TA335.S77.
  3. Olgierd Zienkiewicz,
    The Finite Element Method,
    Sixth Edition,
    Butterworth-Heinemann, 2005,
    ISBN: 0750663200,

Source Code:

Last revised on 24 July 2019.