Computational Lab in Physics: Monte Carlo Integration.

Slides:



Advertisements
Similar presentations
Prepared 7/28/2011 by T. O’Neil for 3460:677, Fall 2011, The University of Akron.
Advertisements

Introduction to Programming (in C++) Numerical methods I Jordi Cortadella Dept. of Computer Science, UPC.
Bayesian Reasoning: Markov Chain Monte Carlo
Introduction to Functions Programming. COMP102 Prog Fundamentals I: Introduction to Functions /Slide 2 Introduction to Functions l A complex problem is.
Writing and Testing Programs Drivers and Stubs Supplement to text.
Monday, 9/23/02, Slide #1 CS 106 Intro to CS 1 Monday, 9/23/02  QUESTIONS??  Today:  Discuss Lab 3  Do Exercises  Introduction to functions  Reading:
Evaluating Hypotheses
CPSC230 Computers & Programming I Lecture Notes 20 Function 5 Dr. Ming Zhang.
Chapter 6: User-Defined Functions I
CS 1400 Chap 6 Functions. Library routines are functions! root = sqrt (a); power = pow (b, c); function name argument arguments.
Lecture II-2: Probability Review
Basic Elements of C++ Chapter 2.
Mathematics for Business (Finance)
Space-Filling DOEs Design of experiments (DOE) for noisy data tend to place points on the boundary of the domain. When the error in the surrogate is due.
1 CE 530 Molecular Simulation Lecture 7 David A. Kofke Department of Chemical Engineering SUNY Buffalo
1 Statistical Mechanics and Multi- Scale Simulation Methods ChBE Prof. C. Heath Turner Lecture 11 Some materials adapted from Prof. Keith E. Gubbins:
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Chapter 13 Multiple Integrals by Zhian Liang.
1 Lesson 3: Choosing from distributions Theory: LLN and Central Limit Theorem Theory: LLN and Central Limit Theorem Choosing from distributions Choosing.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Approximate computations Jordi Cortadella Department of Computer Science.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
1 Advanced Issues on Classes Part 3 Reference variables (Tapestry pp.581, Horton 176 – 178) Const-reference variables (Horton 176 – 178) object sharing:
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
1 Three C++ Looping Statements Chapter 7 CSIS 10A.
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
Introduction to Integration
Overview Go over parts of quiz? Another iteration structure for loop.
Computer simulation Sep. 9, QUIZ 2 Determine whether the following experiments have discrete or continuous out comes A fair die is tossed and the.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Week 61 Introduction to Programming Ms. Knudtzon C Period Tuesday October 12.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Section 16.1 Definite Integral of a Function of Two Variables.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
A Sample Program #include using namespace std; int main(void) { cout
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Functions and Libraries. The idea of a function Functions in programming A function is a block of code that has been given a name. To invoke that code.
T/F  The following code will compile without error. int x = 3, y = 4, z = 5; double k = 3.4; cout
Chapter 9: Value-Returning Functions
Monte Carlo Methods Some example applications in C++
Chapter Topics The Basics of a C++ Program Data Types
Approximate computations
Chapter 6 CS 3370 – C++ Functions.
Ex1: Event Generation (Binomial Distribution)
ROOT: Functions & Fitting
iSTEP 2016 Tsinghua University, Beijing July 10-20, 2016
Basic Elements of C++.
New Structure Recall “average.cpp” program
Approximate computations
CS149D Elements of Computer Science
User-Defined Functions
Rectangular Coordinates in 3-Space
Basic Elements of C++ Chapter 2.
Computational Lab in Physics: Numerical Integration
Arrays & Functions Lesson xx
More U-Substitution: The “Double-U” Substitution with ArcTan(u)
Chapter 10 Algorithms.
MATLAB Tutorial ECE 002 Professor S. Ahmadi.
CS150 Introduction to Computer Science 1
Monte Carlo Methods A so-called “embarrassingly parallel” computation as it decomposes into obviously independent tasks that can be done in parallel without.
Chapter 6: User-Defined Functions I
CS 101 First Exam Review.
Introduction to Functions
Presentation transcript:

Computational Lab in Physics: Monte Carlo Integration.

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).

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.

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

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

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;

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.

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

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

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

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.

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?

Dimensions: Radius = 7.5 fm b=4 fm