views:

122

answers:

2

I don't know from where should I start solving this problem.

It seems that the Math department at UGA once again dropped the ball , and forgot the value of pi. You are to write a function called mypi which consumes a number that specifies the required accuracy and approximates the value of pi to that accuracy. You are going to the following algorithm based on geometric probability.

Think about a quarter circle inside of a unite square (the quarter circle has area pi/4). You pick a random point inside the square. if it is in the quarter circle, you get a "hit" and if not, you get a "miss". The approximate area of the quarter circle will be giving by the number of hits divided by the number of points you chose.

Your function should repeat the process of counting hits and misses until at least 10,000 tries have been made, and successive estimates of pi are within the prescribed accuracy. it should return the estimated value of pi.

HINT: 1- Use the function rand(...) in this problem.

2- Think about the probability that a random point ends up in the quarter circle. You can simulate this probability by counting how many random points end up in the quarter circle. This means you must geometrically define where the quarter circle is and have a script that can determine if the random point in the square is inside the boundaries of the quarter circle.

+1  A: 

In case you don't understand the description - the unit square has area 1, and the quarter circle area pi/4. If you pick random points in the unit square, the probability of landing in the quarter circle is simply the area of the quarter circle divided by the area of the square.

Since this is a homework problem, I won't answer directly, but think about how you can tell if a point is inside the region defined by the quarter circle and the x and y axes (as in, using greater than and less than operators). A simple for loop will suffice for picking the random points.

irrelephant
+2  A: 

Try

n=1000; % number of samples
TestValues=rand(n,2); % random vectors

IsInsideCircle=0; % Empty scalar
radius=[]; % Empty scalar

%Test condition:
for i:0:n
   radius = sqrt(TestValues(i,1)^2+TestValues(i,1)^2);
   if radius < 1
       IsInsideCircle=IsInsideCircle+1;
   end
end

I forget my matlab syntax - hopefully its wrong so you can learn the syntax. Basically you create a bunch of random numbers (one x coordinate, one y coordinate) that are in the 1st quadrant. Then check to see if the radius of that coordinate is less than the circle radius (implicitly defined as 1). Does help?

Marm0t
Yes it is wrong (just a little bit).. hopefully enough to make him solve the problem by himself. :)
George B.

related questions