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 x\" series. As

ID: 3790357 • Letter: T

Question

This problem is another entry in our "numerical approximation of x" 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 z-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 root x^2_0 + y^2 _0 gives the distance between P and the origin O. 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 1000 counts and creating two real random numbers between 0 and 1 for x_0 and y_0 (b) 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. This problem is worth 10 points (5 points for each part)

Explanation / Answer

countC = 0
totalCount = 10000
for i = 1:totalCount
x = rand()
y = rand()
r = sqrt(x*x + y*y)
if r <= 1
countC = countC + 1
end
end

calc_pie1 = 4*(countC/totalCount)

calc_pie2 = 0
total_count = 0
count_c = 0
err = 0.000000009

while(abs(pi - calc_pie2) > err)
total_count = total_count + 1
x = rand()
y = rand()
r = sqrt(x*x + y*y)
if r <= 1
count_c = count_c + 1
end
calc_pie2 = 4*(count_c/total_count)
end

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