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

Problem 5: Estimating p. As discussed previously ,one way to estimate the outcom

ID: 3770917 • Letter: P

Question

Problem 5: Estimating p. 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 p, 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 p. The ratio of the area of the circle to the area of the square is p/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*p/4 The number of points falling inside the circle can be determined by the condition x2 + y2 <= 1. 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 5) 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 p. (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.

Use matlab for this. I have a data file that works for this so if you could just figure out a solution that would be appricated.

Explanation / Answer

MonteCarlo.m

n1 = 10000;
n2 = 1000000;
count = 0;


for i=1:n1
x = 2*rand-1;
y = 2*rand-1;
  
if x^2 + y^2 <= 1
count = count + 1;
end
  
end

pi_approx = 4*(count/n1);
disp("For 10000 points, pi = ")
disp(pi_approx);

count = 0;
for i=1:n2
x = 2*rand-1;
y = 2*rand-1;
  
if x^2 + y^2 <= 1
count = count + 1;
end
  
end


pi_approx = 4*(count/n2);
disp("For 100000 points, pi = ")
disp(pi_approx);

% The reason the values are not same for both the scenarios is because the number of points we chose to % check if they lie in the circle ( sampling points) differ. According to central limit theorem, quadrupling the % number of sampled points halves the error. Our approximation increases with more the points we choose % to check (after randomly generating),