Michael Thomas Flanagan's Java Scientific Library

Integration Class:      Numerical Integrations

     

Last update: 22 September 2008                                                                                                                              PERMISSION TO COPY
Main Page of Michael Thomas Flanagan's Java Scientific Library

This class contains the methods for performing the following numerical integrations: The function to be integrated is supplied by means of an interface, IntegralFunction
import statement/s: import flanagan.integration.Integration;
import flanagan.integration.IntegralFunction;
Related classes

SUMMARY OF CONSTRUCTORS AND METHODS

Constructors public double Integration()
public double Integration(IntegralFunction intFunc)
public double Integration(IntegralFunction intFunc, double lowerLimit, double upperLimit)
Set the function to be integrated public void setIntegrationFunction(IntegralFunction intFunc)
Set the limits public void setLimits(double lowerLimit, double upperLimit)
public void setLowerLimit(double lowerLimit)
public void setUpperLimit(double upperLimit)
Gauss-Legendre
Quadrature
public double gaussQuad(int nPoints)
public static double gaussQuad(IntegralFunction intFunc, double lowerLimit, double upperLimit, int nPoints)
public static void gaussQuadCoeff(double[] gaussQuadDist, double[] gaussQuadWeight, int nPoints)
Trapezoidal Rule public double trapezium(int nIntervals)
public static double trapezium(IntegralFunction intFunc, double lowerLimit, double upperLimit, int nIntervals)
public double trapezium(double accuracy, int maxIntervals)
public static double trapezium(IntegralFunction intFunc, double lowerLimit, double upperLimit, double accuracy, int maxIntervals)
public double getTrapeziumAccuracy()
public static double getTrapAccuracy()
public int getTrapeziumIntervals()
public static int getTrapIntervals()
Backward Rectangular Rule public double backward(int nIntervals)
public static double backward(IntegralFunction intFunc, double lowerLimit, double upperLimit, int nIntervals)
Forward Rectangular Rule public double forward( int nIntervals)
public static double forward(IntegralFunction intFunc, double lowerLimit, double upperLimit, int nIntervals)




CONSTRUCTORS AND METHODS

CONSTRUCTORS
public double Integration()
public double Integration(IntegralFunction intFunc)
public double Integration(IntegralFunction intFunc, double lowerLimit, doubleupperLimit)
Usage:                      Integration intgn = Integration();
Creates an Integration object without providing the function to be integrated or setting the limits to the integration.

Usage:                      Integration intgn = Integration(intFunc);
Creates an Integration object for the integration of the function passed in the IntegralFunction intFunc. See below (Coding the function) for details on how to write the code for the function to be integrated. The limits to the integration are not set.

Usage:                      Integration intgn = Integration(intFunc, lowerLimt, upperLimit);
Creates an Integration object for the integration of the function passed in the IntegralFunction intFunc. See below (Coding the function) for details on how to write the code for the function to be integrated. The lower limit to the integration is passed as the double lowerLimit and the upper limit as the double upperLimit.



SET THE FUNCTION TO BE INTEGRATED
public void setIntegrationFunction(IntegralFunction intFunc)
Usage:                      intgn.setIntegrationFunction(intFunc);
Sets the function to be integrated which is passed in the IntegralFunction, intFunc. See below (Coding the function) for details on how to write the code for the function to be integrated.



SET THE LIMITS TO THE INTEGRATION
public void setLimits(double lowerLimit, double upperLimit)
public void setLowerLimit(double lowerLimit)
public void setUpperLimit(double upperLimit)
Usage:                      intgn.setLimits(lowerLimit, upperLimit);
Sets or resets the limits to the integration. The lower limit is passed as the double lowerLimit and the upper limit as the double upperLimit.

Usage:                      intgn.setlowerLimit(lowerLimit);
Sets or resets the lower limit to the integration. The lower limit is passed as the double lowerLimit.

Usage:                      intgn.setUpperLimit(upperLimit);
Sets or resets the upper limit to the integration. The upper limit is passed as the double upperLimit.



GAUSS-LEGENDRE QUADRATURE
Call a Gauss-Legendre quadrature
public double gaussQuad(int nPoints)
public static double gaussQuad(IntegralFunction intFunc, double lowerLimit, double upperLimit, int nPoints)
Both these methods return the integral of the function f(x, a, b, c . . .), i.e.
                    
using an n point (nPoints) Gauss-Legendre quadrature. These methods automatically transforms the variable and the limits to give the -1 to +1 limits required by the Gauss-Legendre procedure.
Usage:                      integralSum = intgn.gaussQuad(nPoints);
Returns the quadrature sum. The number of points to be used in the quadrature procedure are passed as the int nPoints. The function to be integrated and the limits must be previously set through the constructor and/or the set methods above.

Usage:                      integralSum = Integration.gaussQuad(func, lowerLimit, upperLimit, nPoints);
A static method that returns the quadrature sum. See below (Coding the function) for details on how to write the code for the function to be integrated, passed in the IntegralFunction func.

Calculate a set of Gauss-Legendre quadrature coefficients
public static void gaussQuadCoeff(double[] gaussQuadDist, double[] gaussQuadWeights, int nPoints)
Usage:                      Integration.gaussQuadCoeff(gaussQuadDist, gaussQuadWeight, nPoints);
Calculates and returns the Gauss-Legendre distances (gaussQuadDist), scaled to a lower limit of -1 and an upper limit of +1, and the corresponding Gauss-Legendre weights (gaussQuadWeights) for an nPoints Gauss-Legendre quadrature. This method does NOT need to be called when using gaussQuad (above). Both the instance and static gaussQuad methods automatically call gaussQuadCoeff.



TRAPEZOIDAL RULE
Call a trapezium rule integration
public double trapezium(int nIntervals)
public static double trapezium(IntegralFunction intFunc, double lowerLimit, double upperLimit, int nIntervals)
Both methods returns the integral of the function f(x, a, b, c . . .), i.e.
                    
using the trapezium rule with a preset number of intervals.
Usage:                      integralSum = intgn.trapezium(nIntervals);
Returns the integral using the trapezium rule with the number of intervals passed as the integer nIntervals. The function to be integrated and the limits must be previously set through the constructor and/or the set methods above.

Usage:                      integralSum = Integration.trapezium(func, lowerLimit, upperLimit, nIntervals);
A static method that returns the trapezium sum with a set number of intervals nIntervals. See below (Coding the function) for details on how to write the code for the function to be integrated, passed in the IntegralFunction func.

public double trapezium(double upperLimit, double accuracy, int maxIntervals)
public static double trapezium(IntegralFunction intFunc, double lowerLimit, double upperLimit, double accuracy, int maxIntervals)
Both these methods call trapezium(nIntervals) [immediately above] iteratively, increasing nIntervals by one on each successive call, starting at nIntervals = 1. They return, integralSum, the integral of the function f(x, a, b, c . . .), i.e.
                    
when the difference between two successive calculations is less than a user provided accuracy multiplied by the penultimate value of integralSum. The user also provides a maximum number of intervals allowed and if this maximum is reached, the last value of integralSum is returned and a message is printed to screen. Usage:                      integralSum = intgn.trapezium(accuracy, maxIntervals);
Returns the trapezium sum for an iterative incrementing of the number of intervals when either the accuracy, passed as the double accuracy and used as described above, is satisfied or the maximum number of intervals, passes as the int maxIntervals is reached. The number of intervals used may be obtained using the method getTrapeziumIntervals() [see below]. The accuracy attained may be obtained using the method getTrapeziumAccuracy() [see below]. The function to be integrated and the limits must be previously set through the constructor and/or the set methods above.

Usage:                      integralSum = Integration.trapezium(func, lowerLimit, upperLimit, accuracy, maxIntervals);
A static method that returns the trapezium sum for an iterative incrementing of the number of intervals when either the accuracy, passed as the double accuracy and used as described above, is satisfied or the maximum number of intervals, passes as the int maxIntervals is reached. The number of intervals used may be obtained using the method IntegrationgettrapIntervals() [see below]. The accuracy attained may be obtained using the method Integration.getTrapAccuracy() [see below]. See below (Coding the function) for details on how to write the code for the function to be integrated, passed in the IntegralFunction func

Get the accuracy of the iterative trapezium rule method
public double getTrapeziumAccuracy()
Usage:                      actualAccuracy = intgn.getTrapeziumAccuracy();
Returns the actual accuracy, |(final trapezium sum - penultimate trapezium sum)/penultimate trapezium sum|, acheived in the iterative trapezium procedure.

public static double getTrapAccuracy()
Usage:                      actualAccuracy = Integration.getTrapAccuracy();
Static method that returns the actual accuracy, |(final trapezium sum - penultimate trapezium sum)/penultimate trapezium sum|, acheived in the iterative trapezium procedure.

Get the number of intervals in the iterative trapezium rule method
public int getTrapeziumIntervals()
Usage:                      nInterv = intgn.getTrapeziumIntervals();
Returns the value of number of intervals used in the trapezium summation. This value is updated on each iterative call by trapezium incrementing the number of intervals by 1.

public static int getTrapIntervals()
Usage:                      nInterv = Integration.getTrapIntervals();
Static method that returns the value of number of intervals used in the trapezium summation. This value is updated on each iterative call by trapezium incrementing the number of intervals by 1.



BACKWARD RECTANGULAR RULE
public static double backward(int nIntervals)
public static double backward(IntegralFunction intFunc, double lowerLimit, double upperLimit, int nIntervals)
Both methods return the integral of the function f(x, a, b, c . . .), i.e.
                    
using the backward rectangular rule with a user supplied number of intervals.
Usage:                      integralSum = Intgn.backward(nIntervals);
Returns the bacward rectangular rule sum for the number of intervals passed as the int nIntervals. The function to be integrated and the limits must be previously set through the constructor and/or the set methods above.

Usage:                      integralSum = Integration.backward(func, lowerLimit, upperLimit, nIntervals);
Static method that returns the bacward rectangular rule sum for the number of intervals passed as the int nIntervals. See below (Coding the function) for details on how to write the code for the function to be integrated passed as the IntegralFunction, func.



FORWARD RECTANGULAR RULE
public static double forward(int nIntervals)
public static double forward(IntegralFunction intFunc, double lowerLimit, double upperLimit, int nIntervals)
Both methods return the integral of the function f(x, a, b, c . . .), i.e.
                    
using the forward rectangular rule with a user supplied number of intervals.
Usage:                      integralSum = Intgn.forward(nIntervals);
Returns the bacward rectangular rule sum for the number of intervals passed as the int nIntervals. The function to be integrated and the limits must be previously set through the constructor and/or the set methods above.

Usage:                      integralSum = Integration.forward(func, lowerLimit, upperLimit, nIntervals);
Static method that returns the bacward rectangular rule sum for the number of intervals passed as the int nIntervals. See below (Coding the function) for details on how to write the code for the function to be integrated passed as the IntegralFunction, func.



CODING THE FUNCTION TO BE INTEGRATED
The mathematicl function to be integrated, f(x, a, b, c . . .), must be coded as a method
                    public double function(double x)
The method, function, must have the above signature, i.e. name, type and argument list, and be a method in a class which must implement the interface, IntegralFunction. The name of this class is the user's choice. The arguments to the mathematical version of the function, F(x, a, b, c . . .) other than x, i.e. a, b, c . . . may not be included in the method function(x) argument list. They should be declared as public members of the class of which function(x) is a member. The function is passed to the integration method, gaussQuad(...) as an instance of the class containing the method function(x) [func in the above example but this instance may be named at the user's choice]. This instance should be created in the method that calls gaussQuad(. . .). This procedure can be seen more clearly in the Example program demonstrating the use of numerical integration.



EXAMPLE PROGAMS



OTHER CLASSES USED BY THIS CLASS

This class uses the following classes in this library:


PERMISSION TO COPY

Permission to use, copy and modify this software and its documentation for NON-COMMERCIAL purposes is granted, without fee, provided that an acknowledgement to the author, Dr Michael Thomas Flanagan at www.ee.ucl.ac.uk/~mflanaga, appears in all copies and associated documentation or publications. Dr Michael Thomas Flanagan makes no representations about the suitability or fitness of the software for any or for a particular purpose. Dr Michael Thomas Flanagan shall not be liable for any damages suffered as a result of using, modifying or distributing this software or its derivatives.

Redistributions of the source code of this class, or parts of the source codes, must retain the copyright notice, this list of conditions and the following disclaimer (all at the top of the source code) and requires written permission from the Michael Thomas Flanagan:

Redistribution in binary form of all or parts of this class must reproduce the copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution and requires written permission from the Michael Thomas Flanagan:
.



> This page was prepared by Dr Michael Thomas Flanagan