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

Estimating pi using MATLAB. As discussed previously ,one way to estimate the out

ID: 3770777 • Letter: E

Question

Estimating pi using MATLAB. 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 pi, 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 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 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.

Explanation / Answer

Note: I have used sprintf and disp functions for printing

if you don't want you can delete them , and just remove semicolon in line 6 & 14 to print pi value

Code:

x = -1 + 2*rand(1,10000);
y = -1 + 2*rand(1,10000);
r = (x.*x) + (y.*y);
l = length(find(r<=1));
n=1000;
p = (l*4)/1000;
str = sprintf("pi value with 10000 samples = %f",p);
disp(str);
x = -1 + 2*rand(1,1000000);
y = -1 + 2*rand(1,1000000);
r = (x.*x) + (y.*y);
l = length(find(r<=1));
n=1000;
p = (l*4)/1000000;
str = sprintf("pi value with 1000000 samples = %f",p);
disp(str);

output:

pi value with 10000 samples = 31.192000

pi value with 1000000 samples = 3.140452

Reason for different values:

1) As the PI value we got is an approximate value , calculated using random numbers

any two calculations will not give the same value

Difference between approximation 1 & 2:

Since we have used more number of samples in approximation 2 , we will get more accuracy in the second approximation

pi value with 10000 samples = 31.192000   

pi value with 1000000 samples = 3.140452

actual value of Pi = 3.1415926535

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