Develop a Monte Carlo simulation by writing an algorithm that generates a sequen
ID: 3838246 • Letter: D
Question
Develop a Monte Carlo simulation by writing an algorithm that generates a sequence of uniform random numbers (0, 1) for values of x and y on the OABC region, and calculate the corresponding z distance. Compute the integrated values for T T and simulated values of pi. Using MATLAB. Generate a 2 - dimensional plot of x - y shot values for simulation trials of n = 10, 100, and 1000. This is essentially reproducing the OABC region with shot values as in the first figure. Generate a 2 - dimensional plot of your simulated pi value as a function of trials for 100000 trials. Restrict the y - axis range from 3 to 33. This should resemble the second figure. Determine how many trials are necessary in order to obtain an estimate of that is accurate to 3, 4, and digits 99.95% (3.14), 99.99% (3, 141), and 99.9999% (3.1415).Explanation / Answer
Following are the results from one sample run.
89 trials for 3 digit accuracy
233 trials for 4 digit accuracy
1053 trials for 5 digit accuracy
import random
import math
count_inside = 0
total_is = 100000000;
to_find_accuracy_3 = True;
to_find_accuracy_4 = True;
for count in range(0, total_is+1):
d = math.hypot(random.random(), random.random())
if d < 1:
count_inside += 1
value_of_pi = (4.0*count_inside)/(count+1);
if to_find_accuracy_3 and (int(value_of_pi*100) == 314):
print(count+1,' trials for 3 digit accuracy ');
to_find_accuracy_3 = False;
if to_find_accuracy_4 and (int(value_of_pi*1000) == 3141):
print(count+1,' trials for 4 digit accuracy ');
to_find_accuracy_4 = False;
if int(value_of_pi*10000) == 31415:
print(count+1,' trials for 5 digit accuracy ');
break;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.