Numerical integration by using recursive Romberg algorithm.
Example 1
Evaluate fuction Sin(x)*Exp(-x^2) on interval [0,PI/2] by using Romberg algorithm. Uses Math387, MtxIntDiff;
// Integrating function
function IntFunc(Parameters: TVec; Const Constants: Array of TSample; Const ObjConst: Array of TObject): TSample;
var x: TSample;
begin
x := Parameters[0];
Result := Sin(x)*Exp(-x*x);
end;
// Integrate
procedure DoIntegrate;
var area: TSample;
sr: TIntStopReason;
begin
area := Romberg(IntFunc,0,0.5*PI,sr);
end;
#include "MtxVecCpp.h"
#include "Math387.hpp"
#include "MtxIntDiff.hpp"
// Integrating function
double __fastcall IntFun(TVec * Parameters, const double * Constants, const int Constants_Size,
System::TObject* const * ObjConst, const int ObjConst_Size)
{
double x = (*Parameters)[0];
return Math.Sin(x)*Math.Exp(-x*x);
}
void __fastcall Example();
{
TIntStopReason sr;
double area = Romberg(IntFun,0.0,5*PI,NULL,NULL,sr);
}
private double IntFun(TVec x, double[] c, object[] o)
{
double x = x[0];
return System.Math.Sin(x)*System.Math.Exp(-x*x);
}
private void Example()
{
TIntStopReason sr;
double area = MtxIntDif.Romberg(IntFun,0.0,5*System.Math.PI,out sr, 1.0e-4, 100);
}