Example 4: Integral of an oscillatory function

The integral of cos(ax ) for a = 10^4 is computed and compared to its expected value. Because the function is highly oscillatory, the quadrature rule is set to 6. The relative error tolerance is also set.
import com.imsl.math.*;

public class QuadratureEx4 {
    public static void main(String args[]) {
        final double a = 1.0e4;
        
        Quadrature.Function fcn = new Quadrature.Function() {
            public double f(double x) {
                return Math.cos(a*x);
            }
        };
        
        Quadrature q = new Quadrature();
        q.setRule(6);
        q.setRelativeError(1.e-10);
        double result = q.eval(fcn, 0.0, 1.0);
        
        double expect = Math.sin(a)/a;
        System.out.println("result = "+result);
        System.out.println("expect = "+expect);
        System.out.println("relative error = "+(expect-result)/expect);
        System.out.println("relative error estimate = "+q.getErrorEstimate());
    }
}

Output

result = -3.05614388902526E-5
expect = -3.056143888882521E-5
relative error = -4.670545934003717E-11
relative error estimate = 1.0488375541870691E-8
Link to Java source.