using System; using CenterSpace.NMath.Core; namespace CenterSpace.NMath.Examples.CSharp { /// <summary> /// A .NET example in C# showing how to create integrator objects for greater control /// over how numerical integration is performed. /// </summary> class IntegrationExample { static double MyFunction( double x ) { return Math.Sqrt( x ); } static void Main( string[] args ) { Console.WriteLine(); Console.WriteLine( "Known exact integral of f from 1 to 4 = 4.6666666...\n" ); // As shown in the OneVariableFunctionExample, the Integrate() method // on OneVariableFunction computes the integral of a function over a // given interval. var d = new Func<double, double>( MyFunction ); OneVariableFunction f = d; Console.WriteLine( "Estimate using default Romberg integrator = {0}", f.Integrate( 1, 4 ) ); // To perform integration, every OneVariableFunction has an IIntegrator // object associated with it. NMath Core integration classes such as // RombergIntegrator and GaussKronrodIntegrator implement the IIntegrator // interface. The default integrator for a OneVariableFunction is an instance // of RombergIntegrator. // Instances of class RombergIntegrator compute successive Romberg // approximations of increasing order until the estimated error in the // approximation is less than a specified error tolerance, or until the // maximum order is reached. To achieve greater control over how integration // is performed, you can instantiate your own RombergIntegrator. var romberg = new RombergIntegrator(); // The Tolerance property gets and sets the error tolerance used in computing // integrals. MaximumOrder gets and sets the maximum order. romberg.Tolerance = 1e-10; romberg.MaximumOrder = 20; // The Integrate() method on RombergIntegrator accepts a // OneVariableFunction and an interval over which to integrate. Console.WriteLine( "Estimate using customized Romberg integrator = {0}", romberg.Integrate( f, 1, 4 ) ); // After computing an estimate, a RombergIntegrator holds a record of // the iteration process. Read-only properties are provided for accessing // this information. Console.WriteLine( "Order of estimate = {0}", romberg.Order ); Console.WriteLine( "Error estimate = {0}", romberg.RombergErrorEstimate ); // ToleranceMet gets a boolean value indicating whether or not the error // estimate for the integral approximation is less than the tolerance. if ( romberg.ToleranceMet ) { Console.WriteLine( "The estimate is within the error tolerance.\n" ); } else { Console.WriteLine( "The estimate is NOT within the error tolerance.\n" ); } // Tableau gets the entire matrix of successive approximations // computed while computing a Romberg estimate. The rows are the order // of approximation. The columns are the level of approximation. // The first column contains the trapezoidal approximations, // the second column the Simpsons rule approximations, the third // column the Booles rule approximations, and so on, up to the Order // of the approximation just computed. Console.WriteLine( "Romberg Tableau:" ); Console.WriteLine( romberg.Tableau.ToTabDelimited( "F5" ) ); // The automatic GaussKronrodIntegrator class uses Gauss-Kronrod rules with // increasing number of points. Approximation ends when the estimated error // is less than a specified error tolerance, or when the maximum number of // points is reached. The Gauss-Kronrod method is especially suited for // non-singular oscillating integrands. var gk = new GaussKronrodIntegrator(); Console.WriteLine( "\nEstimate using Gauss-Kronrod automatic integrator = {0}", gk.Integrate( f, 1, 4 ) ); // NMath Core also includes Gauss-Kronrod classes for different numbers of // Kronrod points (2n+1, beginning with a Gauss 10-point rule). For instance, // GaussKronrod21Integrator approximates integrals using the Gauss 10-point // and the Kronrod 21-point rule. var gk21 = new GaussKronrod21Integrator(); Console.WriteLine( "Estimate using Gauss-Kronrod 21 integrator = {0}", gk21.Integrate( f, 1, 4 ) ); Console.WriteLine(); Console.WriteLine( "Press Enter Key" ); Console.Read(); } }// class }// namespace← All NMath Code Examples