This project has moved and is read-only. For the latest updates, please go here.
The Integrate function does numerical integration a user-supplied function. The following C# code snippet illustrates its use.

// If the integrand is a defined method, you can simply name the method.
// Here we integrate over the "hump" of the cosine function.
double I1 = FunctionMath.Integrate(
  Interval.FromEndpoints(-Math.PI/2.0, Math.PI/2.0)

// You can also use anonymous delegate notation to define the integrand inline.
double I2 = FunctionMath.Integrate(
  x => Math.Sqrt(Math.Sin(x)),
  Interval.FromEndpoints(0.0, Math.PI)

// Integrals can have infinite endpoints.
double I3 = FunctionMath.Integrate(
  x => Math.Exp(-x * x / 2.0),
  Interval.FromEndpoints(Double.NegativeInfinity, Double.PositiveInfinity)

// Integrable endpoint singularities are also allowed. (Log goes to negative infinity at zero.)
double I4 = FunctionMath.Integrate(Math.Log, Interval.FromEndpoints(0.0, 1.0));

// By default, we target nearly full precision and allow many thousands of function evaluations to achieve it.
// If your accuracy requirements are lower, or you want to change the number of allowed function
// evaluations, you can do so using EvaluationSettings.
EvaluationSettings integrationSettings = new EvaluationSettings() {
  RelativePrecision = 1.0E-5,
  EvaluationBudget = 500
double I5 = FunctionMath.Integrate(
  Interval.FromEndpoints(0.0, 1.0),

Last edited Sep 22, 2012 at 1:57 AM by ichbin, version 4


cubbins Nov 25, 2016 at 12:05 AM 
Explicitly declare the namespace of Interval as "Meta.Numerics.Interval" to clear compiler errors,
so change Interval.FromEndpoints(0.0, 1.0) to Meta.Numerics.Interval.FromEndpoints(0.0, 1.0),
and do all cases when calling method 'Interval'.

cubbins Nov 24, 2016 at 11:58 PM 
Need to change the last line to ".Value", to obtain a double from FunctionMath.Integrate.

double I5 = FunctionMath.Integrate(
Meta.Numerics.Interval.FromEndpoints(0.0, 1.0),