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

using MatLab 2. Use the Monte Carlo method to approximate . You can do this by g

ID: 2250307 • Letter: U

Question

using MatLab

2. Use the Monte Carlo method to approximate . You can do this by generating random points inside of a box, and determine if those points are inside or outside of a quarter circle with a radius of the side of your box (as shown in the figure below). You can use a similar procedure as the example in class to determine the area of the quarter of this box (the ratio of points inside the circle divided by the number of samples times the area of the box). Then, as area-r and you just calculated ¼ of the area, you can use your calculated value, and your approximations of pi for 10, 100, 1000, 100000, and 10000000 samples area r2 to approximate . Show 0 0

Explanation / Answer


Hello,
          Please find the answer attached as under. Please give a thumbs up rating if you find the answer useful! Have a rocking day ahead!


****************** Matlab Code *******************

r = 1;              % radius of the circle = side of the square
box_area = r*r;     % area of box

n1 = 10;            % n = no. of samples
n2 = 100;
n3 = 1000;
n4 = 10000;
n5 = 10000000;

x1 = rand(1,n1);    % random number generator for x and y co-ordinate
y1 = rand(1,n1);
x2 = rand(1,n2);
y2 = rand(1,n2);
x3 = rand(1,n3);
y3 = rand(1,n3);
x4 = rand(1,n4);
y4 = rand(1,n4);
x5 = rand(1,n5);
y5 = rand(1,n5);

d1 = sqrt(x1.*x1 + y1.*y1);   % distance of each point (x,y) in the x and y array from origin
d2 = sqrt(x2.*x2 + y2.*y2);
d3 = sqrt(x3.*x3 + y3.*y3);
d4 = sqrt(x4.*x4 + y4.*y4);
d5 = sqrt(x5.*x5 + y5.*y5);

in_circle1 = d1 < r;          % find out points lying within the circle
area1 = (sum(in_circle1)/n1) * box_area;   % find out area of quarter
pi_approx1 = 4*area1/(r*r);                   % approximate pi

in_circle2 = d2 < r;          % find out points lying within the circle
area2 = (sum(in_circle2)/n2) * box_area;   % find out area of quarter
pi_approx2 = 4*area2/(r*r);                   % approximate pi

in_circle3 = d3 < r;          % find out points lying within the circle
area3 = (sum(in_circle3)/n3) * box_area;   % find out area of quarter
pi_approx3 = 4 * area3/(r*r);                   % approximate pi

in_circle4 = d4 < r;          % find out points lying within the circle
area4 = (sum(in_circle4)/n4) * box_area;   % find out area of quarter
pi_approx4 = 4*area4/(r*r);                   % approximate pi

in_circle5 = d5 < r;          % find out points lying within the circle
area5 = (sum(in_circle5)/n5) * box_area;   % find out area of quarter
pi_approx5 = 4*area5/(r*r);                   % approximate pi

****************** End of code ********************

Please run the above code and see the values of the variables pi_approx1 till pi_approx5. These will be the approximated values of pi. All comments have been added to the code. The following are the approximated values of pi:

pi_approx1 =

    3.2000


pi_approx2 =

    3.2400


pi_approx3 =

    3.1960


pi_approx4 =

    3.1276


pi_approx5 =

    3.1406