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

This problem is another entry in our \"numerical approximation of pi\" series. A

ID: 3790479 • Letter: T

Question

This problem is another entry in our "numerical approximation of pi" series. Assume that you have created x_0 and y_0, two real random numbers between 0 and 1. If x_0 and y_0 are coordinates of a single point P in the x-y plane, the random point P(x_0, y_0) is confined to a 1-by-1 square cornered on the origin (OABC in Figure 1). Recall that r_0 = squareroot x^2_0 + y^2_6 gives the distance between P and the origin O. We know that the probability of r_0 being less than 1 (i.e. the probability for point P to be confined to circular sector OAC) is equal to the area of the circular sector OAC over the area of the square OABC or pi/4. Write a program to calculate the value of pi from this method by using a for loop for 1000 counts and creating two real random numbers between 0 and 1 for x_0 and y_0. Repeat the problem once more except this time by using a while loop, find out how many times you need to repeat the loop to calculate pi with 10^-9 difference from the exact value defined by MATLAB variable pi.

Explanation / Answer

a) Matlab code : Estimated Pi value = 3.15600000000000

format long
count = 0;
for i =1:1000
x = rand;
y = rand;
if(sqrt((x*x) + (y*y)) < 1)
count = count +1
end
end
P = count/1000;
Pi_value = P*4;
Pi_value

b) Itearations Required = 2692675 and Final Pi_value = 3.141592654145042

format long
count = 0;
Itearations = 0;
i = 0;
while(true)
i = i+1;
x = rand;
y = rand;
if(sqrt((x*x) + (y*y)) < 1)
count = count +1;
end
P = count/i;
Pi_value = P*4;
if(abs(Pi_value- pi) < 10^-9)
Itearations = i
break;
end
end
Pi_value

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