Numerical integration by Monte Carlo method.
Performs a numerical integration of function of single variable by using Monte Carlo method.
Example 1
Evaluate fuction Sin(x) on interval [0,PI] by using Monte Carlo algorithm. // 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);
end;
// Integrate
procedure DoIntegrate;
var area: TSample;
begin
area := MonteCarlo(IntFunc,0,PI,16,[],[],65536); // 2^16 random points in [0,PI] interval
end;
#include "MtxVecCpp.h"
#include "Math387.hpp"
#include "MtxIntDiff.hpp"
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);
}
void __fastcall Example();
{
double area = MonteCarlo(IntFun,0.0,PI,NULL,NULL,65536); // 2^16 random points in [0,PI] interval
}
private double IntFun(TVec x, double[] c, object[] o)
{
double x = x[0];
return System.Math.Sin(x);
}
private void Example()
{
TIntStopReason sr;
double area = MtxIntDif.MonteCarlo(IntFun,0.0,System.Math.PI,null,null,65536);
}