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

MATLAB The USF math department has forgotten the value of pi and they want you t

ID: 3676356 • Letter: M

Question

MATLAB

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 pr2. If r = 1, the area is p and the area of a quarter circle is p/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).

1

Explanation / Answer

clear;

x1=1;

x2=-1;

r=1;

disp('Enter range ');

N= input(prompt);

hits= 0;

miss= 0;

area= 3.14 * r^2;

qarea= area/4;

disp("Area of Quater Circle is ')

disp(qarea)

for k = 1:N

a = x1 + (x2-x1).*rand(N,1);

b = x1 + (x2-x1).*rand(N,1);

% Count it if it is in/on the circle

radii = sqrt(a.^2+b.^2);

if radii <=1;

hits = hits + 1;

else

miss = miss + 1;

end

end