How JustAnswer Works:
• Ask an Expert
• Get a Professional Answer
• 100% Satisfaction Guarantee
Ask Raj Your Own Question
Raj, Engineer
Category: Homework
Satisfied Customers: 1290
Experience:  4 + Experiance in IT Industry
16752232
Type Your Homework Question Here...
Raj is online now

# 2) Monte Carlo IntegrationWrite a C++ program to compute

This answer was rated:
2) Monte Carlo Integration
Write a C++ program to compute the area of a circle of radius ‘r’ using a numerical method called “Monte Carlo Integration”.
Imagine that the black square is a dartboard and that you are tossing darts at it randomly. With enough darts, the ratio of darts in the circle (Nc) to total darts thrown (Nt) is approximately equal the ratio between the area of the circle (circle_area = π(r)2) and the area of the square (square_area = (2r)2 ) which is easy to compute. We can simplify the math by only considering the first quadrant, calculating the ratio of the number of darts landed on the first quadrant over the number of darts landed on the top right square’s area.
Study the following link to learn how to assign a random number to a variable http://www.cplusplus.com/reference/cstdlib/rand/. Define variables to store the x and y coordinates of a particular dart throw. In other words a random number between [0, r] gets assigned to the variables x and y, each (simulating one dart throw). Now, place your x and y declarations in a loop to simulate N dart throws. Note that all of these darts land on the top right square.
Use the Euclidean distance formula, 𝑑=√𝑥2+𝑦2 to check if a dart is landed inside the first quadrant. Maintain account of the number of darts that end up in the first quadrant of the unit circle. Now use your loop to calculate the area of the circle by computing the ratio between Nt and Nc. Run your code for an arbitrary value of ‘r’ and compute the area of the circle based on the given values of Nt and fill out the following table.
NT Area of a Circle
100
10,000
1,000,000
5,000,000
How accurate is your Monte Carlo Integration? Write a short paragraph and explain your observation.
Hi there,

I will be helping you with the solution.
Let me know the deadline?

Thank you
Customer: replied 4 years ago.
Hey the deadline is Friday
Do you have any document like MS WORD file or PDF file containing tue requirement?

Thank you
Customer: replied 4 years ago.
Ya, how would you like me I send the assignment requirements
You can upload your file on www.wikisend.com and then provide me the FILE ID and DOWNLOAD LINK for the file here to download the file.
Customer: replied 4 years ago.
Are you just wanting to see how my teacher wants everything laid out and the format.
Yes.
Customer: replied 4 years ago.

hey Raj, here is the download link

http://wikisend.com/download/762774/Project Template.docx

Customer: replied 4 years ago.

Hey did you get the information from that downloadable link??

http://wikisend.com/download/762774/Project Template.docx

Ok Got the file.
Let me go through it.
Customer: replied 4 years ago.

hey Raj, thanks for helping me with the question that I have been having problems with, just wondering if you have a solution.

Thanks

I am facing issue with calculatiins.
Can you please explain with formulas the steps for calculations?

Thank you
Customer: replied 4 years ago.

Hello Raj, ya for sure.

- imagine you have a coordinate system with x=0..1 and y=0..1 (square) - r=1 (radius) - we consider a first quadrant of the coordinate system (see figure in your task) - the table shows different N (N throws of darts) - you need a loop with n-runs - each run calculates two random numbers x,y in the intervall 0..r (in our case 0..1,because r = 1) - then you have to check, if the point P(x,y) is located in the area of the circle - if yes -> increase a variable ("hit"-variable) - x^2 + y^2 < 1 -> point is located in the area of the circle with radius 1 - the area of the circle = a(N) = 4 * (hit-variable/N) - with radius of 1 you have probabilistic algorithm for the determination of pi - all details are without guarantee

Hope that helps, i will be on the computer all day so if you have any questions, just fire me a message. Thanks

You are welcome.
Based on description provided by you I have written the code.

Please download the Solution code file(Monte Carlo.cpp) from the link below:
http://www.mediafire.com/?njm859to4es3nme

If you face any difficulty than feel free to ask for help.

If the solution file is satisfactory than Please don't forget to RATE the Answer.
It will hardly take few seconds to RATE my Answer

Thank You and Have a Good Day.
Customer: replied 4 years ago.

Hello Raj, when i try to open the link it is not working.

thanks

what Compiler do you use for C++ Code?
Customer: replied 4 years ago.

I use a compiler called Xcode for Mac but any compiler should work.

Ok So you are using XCode.
give me 5 min and I will provide you the updated code for MAC.
Customer: replied 4 years ago.

okay thanks

Ok I have modifed the code based on code in XCode Compiler for MAC.

Please download the updated version from the link below:
http://www.mediafire.com/?7bcvr8i4f7jni8m

If you face any difficulty than feel free to ask for help.

If the solution file is satisfactory than Please don't forget to RATE the Answer.
It will hardly take few seconds to RATE my Answer

Thank You and Have a Good Day.
Customer: replied 4 years ago.

Hello Raj, one last thing, when i go to test the code and enter in the values for N above, , 10000,(NNN) NNN-NNNN(NNN) NNN-NNNN it doesn't even calculate it only calculates for 100.

For me it Does for all the Values.
You need to Enter Numbers without ,
Like for 10,000 you need to enter 10000

Like at present I am running the code and below is the outputs for the values:

For 100:
Enter Value for N : 100
Hit rate : 25
Area of Circle = 1

For 10000:
Enter Value for N : 10000
Hit rate : 2540
Area of Circle = 1.016

For(NNN) NNN-NNNN
Enter Value for N :(NNN) NNN-NNNNbr/>Hit rate : 250066
Area of Circle = 1.00026

For(NNN) NNN-NNNN
Enter Value for N :(NNN) NNN-NNNNbr/>Hit rate :(NNN) NNN-NNNNbr/>Area of Circle = 1.00015

If you face any difficulty than feel free to ask for help.

If the solution file is satisfactory than Please don't forget to RATE the Answer.
It will hardly take few seconds to RATE my Answer

Thank You and Have a Good Day.
Customer: replied 4 years ago.

okay thanks for your help.

THIS ANSWER IS LOCKED!

You need to spend \$3 to view this post. Add Funds to your account and buy credits.
Raj and 3 other Homework Specialists are ready to help you
Customer: replied 4 years ago.

Hey Raj, one last question, in general how accurate do you think the Monte Carlo Integration is ?

thanks

Based on Output the more the value of N (i.e. Number of Samples Collected) the better is the result.
So it seems effective for our code except for the part for 100 Samples which can predict in the worst case Area of Circle as 4 as well which is way to larger than the guess.

So Its effective as the number of samples(N) Increases.