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

****MATLAB*** Estimating p. As discussed previously ,one way to estimate the out

ID: 3770081 • Letter: #

Question

****MATLAB***

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

**MATLAB**

Explanation / Answer

Below is the MATLAB code with comments

% Generate random vectors
x1 = -1 + 2.*rand(10000,1);
y1 = -1 + 2.*rand(10000,1);
x2 = -1 + 2.*rand(1000000,1);
y2 = -1 + 2.*rand(1000000,1);

% fetching two distinct lengths.
la = length(x1)
lb = length(x2)

% Traversing through the vector & checking for x2+y2<=1 condition
for i=1:la
if x1(i)*x1(i) + y1(i)*y1(i) <= 1
fprintf("%f %f", x1(i), y1(i))
end
end
  
for i=1:lb
if x2(i)*x2(i) + y2(i)*y2(i) <= 1
fprintf("%f %f", x2(i), y2(i))
end
end