Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

matlab 5. The USF math department has forgotten the value of pi and they want yo

ID: 3564994 • Letter: M

Question

matlab

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 pi 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 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