Estimating p . One way to estimate the outcome of for a problem is to use a Mont
ID: 3552886 • Letter: E
Question
Estimating p. 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 example to estimate p, 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 p. The ratio of the area of the circle to the area of the square is p/4. Consequently, if a large number of points (N) with x and y values varying between 1 and -1 are generated, the number of points falling inside the circle would be
Points in circle = N*p/4
For this problem you are to estimate pi using two functions that you will create. Both functions will accept two row vectors, one for x values and one for y values, and will return (send back) to the function call an estimate of p. 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 p, 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 p. (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 p. (Do not use any built-in function other than length for this second function.)
The script file should generate row vectors of random numbers for x and y to test your functions. You should have 3 scenarios. In the first scenario, generate 10000 random numbers for the x-values and 10000 random numbers for the y-values. In the second scenario, generate 1000000 random numbers for the x-values and 1000000 random numbers for the y-values. Which one of these two scenarios produce a value of p closer to that stored in the built-in MATLAB value of p? In the third scenario, use row vectors for x and y that are different sizes to test your check for same size.
Explanation / Answer
function p = fun1(X, Y)
if length(X) == length(Y)
Z = zeros(1,length(X));
for i = 1:length(X)
Z(i) = X(i)^2 + Y(i)^2;
end;
temp = find(Z < 1);
count = length(temp)
p = count / length(Y);
end;
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.