Example 3: Integral of the entire real line

The integral \int_{-\infty}^\infty \frac{x} {4e^x + 9e^{-x}} \, dx is computed and compared to its expected value. This integral is evaluated in Gradshteyn and Ryzhik (equation 3.417.1).

import com.imsl.math.*;

public class QuadratureEx3 {

    public static void main(String args[]) {
        Quadrature.Function fcn = new Quadrature.Function() {
            public double f(double x) {
                return x / (4 * Math.exp(x) + 9 * Math.exp(-x));
            }
        };

        Quadrature q = new Quadrature();
        double result = q.eval(fcn, Double.NEGATIVE_INFINITY,
                Double.POSITIVE_INFINITY);

        double expect = Math.PI * Math.log(1.5) / 12.;
        System.out.println("result = " + result);
        System.out.println("expect = " + expect);
    }
}

Output

result = 0.10615051707662819
expect = 0.10615051707663337
Link to Java source.

Reference

Gradshteyn, I. S. and I. M. Ryzhik (1965), Table of Integrals, Series, and Products , Academic Press, New York.