The output needs to look like the box (picture below) Thanks 1. In this problem
ID: 3751177 • Letter: T
Question
The output needs to look like the box (picture below) Thanks 1. In this problem we explore a Monte Carlo approach to approximating the value of pi. The area of a circle of radius r 1 is Adrie-Tr2- whereas the area of the square inscribing it is Aaquare-( 2-4. We then have Acirde Anquare4 Now, consider the following process: * Generate a random x-coordinate between-1 and i Generate a random y-coordinate between -1 and 1 Calculate the length of vector from (0,0) to the random point (,) Use a Boolean expresion to create a logical variable that stores whether or not the random point is inside the circle of radius 1 centered at the origin . Add the resulting logical variable to a running sum variable A Monte Carlo approach would perform the above process for thousands of trials. We can approximate Aralio by mumber of times random point lies inside the circle number of trials Write a script that performs this Monte Carlo approach to approximating pi but with only 5 trials. Feel free to write some code and just repeat it 5 times in your script. Sample output:Explanation / Answer
% Matlab code
% Comment if u have any doubt
% like the answer,if u liked it ;-)
min = -1;
max = 1;
Inside_Circle = 0;
for i = 0:4
x = min + ((max-min).*rand(1));
y = min + ((max-min).*rand(1));
d = sqrt((x - 0)^2 + (y - 0)^2);
if((x < 1 && x > -1) && (y < 1 && y > -1))
inside = 1;
else
inside = 0;
end
Inside_Circle = Inside_Circle + inside;
fprintf("Trial %d : x = %.2f, y = %.2f, length = %.2f, This point in circle ? %d, Total in circle = %d ",(i+1),x,y,d,inside,Inside_Circle);
end
ratio = Inside_Circle/(i+1);
pi_approx = 4 * ratio;
fprintf("An approximation of pi is %.4f ",pi_approx);
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.