Академический Документы
Профессиональный Документы
Культура Документы
Monte Carlo
Integration via Monte Carlo
A function can have a complicated domain D.
Define a simple domain D’.
Choose D’ such that it contains D.
D’ is a superset of D.
Integral of domain D’ is easy.
Integral of domain D is calculated as product of:
fraction of area (or volume) of D’ taken by D
Integral of domain D’.
Fraction can be obtained via Monte Carlo methods.
Throw a point randomly within D’.
Check if the point is also in D.
Repeat many times:
Fraction ~ (points thrown in D) / (all points).
2
Example: Integrate f(x)=x2
Need a definite integral
Use b 1
a
f ( x)dx x 2 dx
0
Take our superdomain D’ to be:
0<x<1 ; i.e. limits of integration (a,b)
y_min< y <y_max such that:
y_min = min(y(x)) for 0<x<1
3
Do the Monte Carlo…
This is the key to the idea:
Throw pairs of uniform random numbers
Each pair represents a point in our 2-D space
2 points for x-y plane
4
In code, with ROOT: User defined
functions
5
Using TF1 with a User-Defined
function:
double function(double* x,double* par) {
return x[0]*x[0];
}
void userDefined() {
// Illustration of a user defined 1-D function
TF1* theFunc = new TF1("theFunc",function,A,B,0);
//To evaluate the user defined function above,
//use the Eval method:
cout << “f(x) for x=0.5 is “ << theFunc->Eval(0.5) <<
endl;
}
6
Generating Random Numbers:
Use TRandom3:
Do NOT use TRandom
TRandom3 is the recommended generator.
void exampleMonteCarloIntegration() {
gStyle->SetOptStat(0);
double a, b, result;
int numberOfRealizations;
cout << "Input interval limits A and B, and numberOfRealizations:" << endl;
cin >> a >> b >> numberOfRealizations;
cout.precision(10);
result = monteCarloIntegral(a,b,numberOfRealizations);
cout << "Integral is " << result << ", error " << result-(1./3.) << endl;
return;
}
9
Resulting Histograms:
Black Points:
All generated points
Intervals:
0<x<1
0<y<1
Red Points:
Generated points with:
y<f(x)=x2
11
Homework, Part 2:
Calculate the overlap area between
circles… and spheres.
Write a ROOT macro that calculates the overlap area
between circles of radius 7.5 fm whose centers are
separated by 4 fm. (50 points)
R
ad
iu
s
=
7.
5
fm
b=4 fm
13