/* Example of the use of the class Integration demonstrating the use of several of the numerical integration methods in integrating one of two functions, y = a + x*x and y = b + c*x using the static methods. Michael Thomas Flanagan July 2006 */ import flanagan.integration.Integration; import flanagan.integration.IntegralFunction; // Class to evaluate the function y = a + x*x for a given value of x (function 1). class Funct1 implements IntegralFunction{ private double a = 0.0D; public double function(double x){ double y = a + x*x; return y; } public void setA(double a){ this.a = a; } } // Class to evaluate the function y = b + c*x for a given value of x (function 2). class Funct2 implements IntegralFunction{ private double b = 0.0D, c = 1.0D; public double function(double x){ double y = b + c*x; return y; } public void setB(double b){ this.b = b; } public void setC(double c){ this.c = c; } } // Class to demonstrate numerical integration methods in class Integration. public class IntegrationExample2{ public static void main(String[] arg){ // Create instances of the classes holding the function evaluation methods Funct1 f1 = new Funct1(); Funct2 f2 = new Funct2(); // Assign values to constants in the functions f1.setA(10.5); f2.setB(7.0); f2.setC(-2.3); // Variable declarations double lowerLimit = 1.0D; double upperLimit = 3.0D; int nPoints = 32; int nIntervals = 1; // Example of Gauss-Legendre quadrature on function 1 // Call Gauss-Legendre quadrature method to operate on function 1 double integralSum0 = Integration.gaussQuad(f1, lowerLimit, upperLimit, nPoints); // Output the result System.out.println("The " + nPoints + " point Gausian-Legendre integral of function 1 between the limits " + lowerLimit + " and " + upperLimit + " is " + integralSum0); // Call Gauss-Legendre quadrature method to operate on function 2 with the same limits and twice the number of points nPoints *= 2; integralSum0 = Integration.gaussQuad(f2, lowerLimit, upperLimit, nPoints); System.out.println("The " + nPoints + " point Gausian-Legendre integral of function 2 between the limits " + lowerLimit + " and " + upperLimit + " is " + integralSum0); // Example of Gauss-Legendre Quadrature and Trapeziodal rule applied to function 1 // Reset limits and number of points or intervals lowerLimit = 0.0D; upperLimit = 5.0D; nPoints = 16; nIntervals = 1000; // Call both methods to operate on function 1 double integralSum1 = Integration.gaussQuad(f1, lowerLimit, upperLimit, nPoints); double integralSum2 = Integration.trapezium(f1, lowerLimit, upperLimit, nIntervals); System.out.println("The " + nPoints + " point Gausian-Legendre integral of function 1 between the limits " + lowerLimit + " and " + upperLimit + " is " + integralSum1); System.out.println("The " + nIntervals + " interval trapezium integral of function 1 between the limits " + lowerLimit + " and " + upperLimit + " is " + integralSum2); // Example of Trapeziodal rule and backward rectangular rule applied to function 2 after function constants have been altered // Reset b and c f2.setB(3.1D); f2.setC(5.5D); // Reset limits and number of intervals lowerLimit = 0.0D; upperLimit = 10.0D; nIntervals = 5000; // Call both methods to operate on function 2 integralSum1 = Integration.trapezium(f2, lowerLimit, upperLimit, nIntervals); integralSum2 = Integration.backward(f2, lowerLimit, upperLimit, nIntervals); System.out.println("The " + nIntervals + " interval trapezium integral of function 2 between the limits " + lowerLimit + " and " + upperLimit + " is " + integralSum1); System.out.println("The " + nIntervals + " interval backward rectangular integral of function 2 between the limits " + lowerLimit + " and " + upperLimit + " is " + integralSum2); } }