Dew MtxVec NET
MonteCarlo Routine
Summary
Numerical integration by Monte Carlo method.

Unit
MtxIntDiff

Declaration
Function MonteCarlo(Fun: TRealFunction; lb, ub: TSample; const Constants: array of TSample; const ObjConst: array of TObject; N: Integer = 65536): TSample;
 Parameter  Description 
Fun Integrating function. 
lb Lower bound. 
ub Upper bound. 
Number of random points in [lb,ub] interval (see comments above). 

Description
Performs a numerical integration of function of single variable by using Monte Carlo method.
 See Also 
QuadGauss 

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); }

Copyright 2008 Dew Research
http://www.dewresearch.com