Download presentation
Presentation is loading. Please wait.
Published byReynard West Modified over 6 years ago
1
Computational Lab in Physics: Monte Carlo Integration.
2
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).
3
Example: Integrate f(x)=x2
Need a definite integral Use 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 y_max = max(y(x)) for 0<x<1 What is the area of this superdomain? Square of unit sides, so area = 1.
4
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 3 points for x,y,z 3D 4 points for 4D… If the pair is within the area we are trying to calculate, count it Fraction = points within / all points For our example: obtain random number for x (in region 0-1) obtain random number for y (in region 0-1) if (y<f(x)) increment counter
5
In code, with ROOT: User defined functions
double function(double* x,double* par) { //the syntax above works for //functions of many variables //with many parameters. //This example is for a 1-D function //with no additional parameters. return x[0]*x[0]; }
6
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;
7
Generating Random Numbers:
Use TRandom3: Do NOT use TRandom TRandom3 is the recommended generator. //Argument to the constructor is the seed. TRandom3 rnd(1); //seed = 1; //Generate a uniform random number: rnd.Uniform(); //default, limits are 0 <x< 1 rnd.Uniform(a);//limits are 0 <x< a rnd.Uniform(a,b);//limits are a <x< b Other random numbers defined: -Exp(tau) -Integer(imax) -Gaus(mean,sigma) -Rndm() -Uniform(x1) -Landau(mpv,sigma) -Poisson(mean) -Binomial(ntot,prob) Note: Use seed=0 if you want a unique seed every time. See class reference for more info.
8
Putting it all together in code, Part 1
double function(double* x,double* par) { return x[0]*x[0]; } double monteCarloIntegral(double A, double B, int numberOfRealizations, int seed=1) { TF1* theFunc = new TF1("theFunc",function,A,B,0); double yLower = theFunc->Eval(A); // this assumes that double yUpper = theFunc->Eval(B); // f(A)<f(B) for A<B TH2D* allPoints = new TH2D("allPoints","All Points",1000,0,1,1000,0,1); TH2D* intPoints = new TH2D("intPoints","Integral Points",1000,0,1,1000,0,1); double regionArea = (yUpper-yLower) * (B-A); int sum = 0; // Generate points ramdomly within the rectangular region. If they fall below the function to be integrated sum is // incremented by one. TRandom3 rnd(seed); // Note: If seed==0, it will be picked by TUUID object for (int i=0; i<numberOfRealizations; ++i) { double xValue = rnd.Uniform(A,B); double yValue = rnd.Uniform(yLower,yUpper); allPoints->Fill(xValue,yValue); if (theFunc->Eval(xValue)> yValue) { sum++; intPoints->Fill(xValue,yValue); TCanvas* mcIntCnv = new TCanvas("mcIntCnv","Monte Carlo Integration",500,500); allPoints->Draw(); intPoints->SetMarkerColor(2); intPoints->Draw("same"); theFunc->Draw("same"); allPoints->SetXTitle("x"); allPoints->SetYTitle("f(x)"); // double fraction = sum/static_cast<double>(numberOfRealizations); return fraction*regionArea + yLower*(B-A);
9
Putting it all together in code, Part 2
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; }
10
Resulting Histograms:
Black Points: All generated points Intervals: 0<x<1 0<y<1 Red Points: Generated points with: y<f(x)=x2 Black line: y=x2. Fraction: # Red pts / # Black pts Integral: Area of Square x Fraction
11
Homework, Part 1: Area of Unit Circle
Problem 22.1 from Text (50 points) Calculate the area of a unit circle Equivalent to calculating p. Use Monte Carlo integration. Modify the program given to do the integral. Make also the corresponding histograms. Calculate the error for the given number of tries.
12
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) Extra Credit: The above is the step towards a calculation used in high-energy nuclear physics. Next step is to find the overlap volume of two spheres (e.g. Au nuclei, which to first order can be approximated as 2 hard spheres of radius 7.5 fm) For 10 extra points, write a program that does the calculation in 3-D instead of 2-D, i.e. Your program should print out a result that answers this question: What is the overlap volume in fm3 of two spheres of radius 7.5 fm whose centers are separated by 4 fm?
13
Dimensions: Radius = 7.5 fm b=4 fm
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.