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

I was working on this question an am having trouble figuring out the required fu

ID: 3530822 • Letter: I

Question

I was working on this question an am having trouble figuring out the required function file. Estimating pi. 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 You are to create two separate functions to calculate estimate pi. Both functions will accept two arrays and will return an estimate of pi. Both functions should check that the input arrays are of the same size. If the arrays are the same size, then calculate the estimate of pi, otherwise output a message that the function call must contain arrays of the same size. The number of points falling inside the circle can be determined by the condition x2 + y2 <= 1. The first function should use the built-in functions of length and find to determine the number of points falling in the circle and then use this value to calculate the estimate of pi. (Do not use any other built-in function for this first function.) The second function to should use a for loop and a counter to count how many points are inside the circle and then calculate the estimate of pi. (Do not use any built-in function other than length for this second function.) The script file should generate row vectors of random numbers to test your functions. You should have 3 scenarios: 10000 x-values and 10000 y-values, 1000000 x-values and 1000000-y values, and a different amount of x- and y- values.

Explanation / Answer

Using 14 pairs of random cooridante pairs you can get:3.1428571428571

Using 452 you can get as close to Pi as 3.141592920354

the script is:

x = 0

y=14

for i = 1, y

do

a=math.random()

b=math.random()

if (a^2+b^2<1 ) then x=x+1 end

end

print(4*x/y)


For better approximation:

Use:


S = 0

n=100

for i = 1, n

do

x=math.random()

y=(1-x^2)^0.5

S=S+y

end

T=4*S/n

print(T)

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