NMath User's Guide

TOC |  Previous |  Next |  Index

32.2 Integrating Functions of Two Variables (.NET, C#, CSharp, Visual Basic, VB.NET)

The Integrate() method on TwoVariableIntegrator integrates a given two-variable function over a given region. For example, to compute the double integral:


First write the function:

private double F( DoubleVector v )
{
  return 1.0 / ( 1.0 - ( v[0] * v[0] * v[1] * v[1] ) );
}

Then encapsulate the function as a MultiVariableFunction:

MultiVariableFunction function = new MultiVariableFunction(
  new NMathFunctions.DoubleVectorDoubleFunction( F ) );

Finally, compute the integral:

TwoVariableIntegrator integrator = new TwoVariableIntegrator();
double xLower = 0;
double xUpper = 1;
double yLower = 0;
double yUpper = 1;
double integral =
  integrator.Integrate( function, xLower, xUpper, yLower, yUpper );

The code above explicitly sets the x and y bounds. You can also set the y lower bound, y upper bound, or both, as a function of x. For example, to compute this double integral:


First define the function:

private double F( DoubleVector v )
{
  return ( 9.0 * v[0] * v[0] ) - ( 3.0 * v[1] );
}

Then encapsulate the function as a MultiVariableFunction:

MultiVariableFunction function = new MultiVariableFunction(
  new NMathFunctions.DoubleVectorDoubleFunction( F ) );

Then define the y bounding functions and encapsulate them as OneVariableFunction objects:

private double YUpperF( double x )
{
  return Math.Sqrt( 9.0 - ( x * x ) );
}

private double YLowerF( double x )
{
  return -YUpperF( x );
}

OneVariableFunction yLowerFunction = new OneVariableFunction(
  new NMathFunctions.DoubleUnaryFunction( YLowerF ) );
OneVariableFunction yUpperFunction = new OneVariableFunction(
  new NMathFunctions.DoubleUnaryFunction( YUpperF ) );

Finally, compute the integral:

TwoVariableIntegrator integrator = new TwoVariableIntegrator();
double xLower = -3;
double xUpper = 3;
double integral = integrator.Integrate( function, xLower, xUpper,
  yLowerFunction, yUpperFunction );

TOC |  Previous |  Next |  Index