Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Estimating pi. As discussed previously, one way to estimate the outcome of for a

ID: 3009493 • Letter: E

Question



Estimating pi. As discussed previously, one way to estimate the outcome of for a problem is to use a Monte Carlo simulation which uses a large number of random numbers and there compares the results of these numbers. For estimating n. we can visualize a circle of radius 1 inside a square with a side of 2, both centered on zero. The area of the square is 4 and the area of the circle is pi. The ratio of the area of the circle to the area of the square is pi/4. Consequently, if a large number of points (N) with x and y values varying between 1 and -1 is generated, the number of points falling inside the circle would be Points in circle = N* pi/4 The number of points falling inside the circle can be determined by the condition x^2 + y^2 I. Generate row vectors of random numbers containing 10000 x-values and 10000 y-values and 1000000 x-values and 1000000-y values. Use the built-in functions of length and find (chapter S) to determine the number of points falling in the circle for the row vectors of 10000 elements and the row vectors containing 1000000 elements. Then use these values to calculate the estimate of pi. (Do not use any built-in functions other than Find, length and rand.) The script file should generate row vectors of random numbers You should have 2 scenarios: 10000 x-values and 10000 y-values and 1000000 x-values and 1000000-y values. Add comments to the end of this problem stating why you did not get the same value for pi for both scenarios.

Explanation / Answer

//case 1:
var x = 10000;
var y = 10000;
var numberOFPointsInCircle =0;
for(var i =0; i<x; i++){
var X = Math.floor((Math.random() * 2) - 1);
var Y = Math.floor((Math.random() * 2) - 1);
if(X*X+Y*Y<=1){
numberOFPointsInCircle++;
}
}
//now we know the number of points inside the circle we can calculate the value of pi
var pi = 4*numberOFPointsInCircle/x;

RESULT: 2.9852

//case 2
var x = 1000000;
var y = 1000000;
var numberOFPointsInCircle =0;
for(var i =0; i<x; i++){
var X = Math.floor((Math.random() * 2) - 1);
var Y = Math.floor((Math.random() * 2) - 1);
if(X*X+Y*Y<=1){
numberOFPointsInCircle++;
}
}
//now we know the number of points inside the circle we can calculate the value of pi
var pi = 4*numberOFPointsInCircle/x;

RESULT: 3.000996

This mehtod only calculate the approximate value. If you increase the number of iterations you will get the results closer to the actual value. Each time you run the script results are different this is because of the random number generator.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote