Example 4: Integral of an oscillatory function
The integral of cos(ax
) for a
= 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.
using System;
using Imsl.Math;
public class QuadratureEx4 : Quadrature.IFunction
{
private double a;
public QuadratureEx4(double a)
{
this.a = a;
}
public double F(double x)
{
return Math.Cos(a * x);
}
public static void Main(String[] args)
{
double a = 1.0e4;
Quadrature.IFunction fcn = new QuadratureEx4(a);
Quadrature q = new Quadrature();
q.Rule = 6;
q.RelativeError = 1e-10;
double result = q.Eval(fcn, 0.0, 1.0);
double expect = Math.Sin(a) / a;
Console.Out.WriteLine("result = " + result);
Console.Out.WriteLine("expect = " + expect);
Console.Out.WriteLine
("relative error = " + (expect - result) / expect);
Console.Out.WriteLine
("relative error estimate = " + q.ErrorEstimate);
}
}
Output
result = -3.05614388902539E-05
expect = -3.05614388888252E-05
relative error = -4.67475872674725E-11
relative error estimate = 1.04883755430041E-08
Link to C# source.