Solve the following problems using MATLAB. Create a single *.m script file that
ID: 3572158 • Letter: S
Question
Solve the following problems using MATLAB. Create a single *.m script file that contains the solutions for all three problems:
Problem 3: In this problem you will be estimating ?. 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 then compares the results of these numbers. For estimating ?, 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 ?. The ratio of the area of the circle to the area of the square is ?/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*?/4
The number of points falling inside the circle can be determined by the condition x2 + y2 <= 1. Generate row vectors of uniformly distributed 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 (Chapter 3) 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 ?. (Do not use any built-in functions other than find, length and rand for this problem.)
The script file should generate row vectors of random numbers You should have 2 scenarios: 10000 x-values and 10000 y-values plus 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.
-1Explanation / Answer
a = -1;
b = 1;
n1 = 10000;
X1 = (b-a).*rand(n1, 1) + a;
Y1 = (b-a).*rand(n1, 1) + a;
X1Y1 = X1.^2 + Y1.^2;
k = find(X1Y1<1);
len = length(k);
pi1 = 4.0*len/n1;
disp(pi1);
n2 = 1000000;
X2 = (b-a).*rand(n2, 1) + a;
Y2 = (b-a).*rand(n2, 1) + a;
X2Y2 = X2.^2 + Y2.^2;
k = find(X2Y2<=1);
len = length(k);
pi2 = 4.0*len/n2;
disp(pi2);
We did not get the same value because as the number of simulations increase we get closer to the correct answer.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.