5. The USF math department has forgotten the value of pi and they want you to ca
ID: 3565485 • Letter: 5
Question
5. The USF math department has forgotten the value of pi and they want you to calculate it for them. Assume you have a quarter circle inside a square with sides of 1x1 unit. Then the radius of the circle is 1. The area of a circle is Pir^2. If r = 1, the area is re and the area of a quarter circle is Pi/4. Use a for loop beginning at 1 and ending at a number input from the keyboard to add random points in the square (use the MATLAB function rand() to get the points). If a point lands inside the circle, it is a hit else it is a miss. The approximate area of the circle is the (hits)/(total points).Explanation / Answer
clear all
close all
ndarts=input('please enter the number ' );
for i=1:ndarts
xcoord(i)=rand-0.5;
ycoord(i)=rand-0.5;
end
distancesq=xcoord.^2+ycoord.^2;
nhits=0;
for i=1:ndarts
if distancesq(i)<=0.25
nhits=nhits+1;
end
end
estimate=4*nhits/ndarts
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.