MATLAB HELP Given the MATLAB Code that uses the Monte Carlo method to estimate p
ID: 3889021 • Letter: M
Question
MATLAB HELP
Given the MATLAB Code that uses the Monte Carlo method to estimate pi and also determines the difference between estimate and true value:
a=10; % the experiment event number
r=1; %the circle radius
c=0; % sucessful event number
for i=1:a
s=-r+1*r*rand();
t=-r+1*r*rand();
pi_sim(i)=4*c/a
pi_diff(i)=pi-pi_sim(i)
if ((s.^2+t.^2)<=r^2)
c = c+1
end
end
Question:With the given code: using a "while" loop to compute pi to a fixed level of precision (i.e. 2 significant figures, 3 significant figures, etc). Note how many steps are required for each level of precision. Your determination of the level of precision should NOT include a reference to the true value of pi. Please appropriately comment on MATAB to be easily read.
Explanation / Answer
Edited code by adding while loop
a=0; % the experiment event number
c=0; % sucessful event number
r =1; % redius of the circle
min_tries = 100;
pi_prev = 1000;
precision = 0.001;
while (true)
s = 2 * r * (rand() - 0.5); %get a point between -1 to +1
t = 2 * r * (rand() - 0.5);
a = a+1;
pi_sim = 4 * c / a;
pi_diff = abs(pi_prev - pi_sim);
pi_prev = pi_sim;
if (pi_diff <= precision && a>min_tries)
break;
end
if ((s.^2+t.^2)<=r^2)
c = c+1;
end
end
fprintf("estimated value of pi is %f", pi_prev);
Please go through the code i have added the while loop as the requirement if any queries regarding code Please get back to me
Thank you
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.