write a code in matlab to calculate the value of Pi by estim
write a code in matlab to calculate the value of Pi by estimating the area of a 2x2 square and the inscribing circle of radius 1. (e.g. randomly throwing pebbles into a 2x2 square, and calcuate the pebble counts inside and outside the circle)
Solution
If the side of square 2*2 and Radius is r value=1,So Area of circle if pi*r*2 and area of square =(2r)^2
Ratio of area of circle and square is pi/4
If we pick n points at random inside the square then N*pi/4 of those points should fall inside the circle.
So we have to check inside the circle x^2+y^2<r^2 ,where x and y are the coordinates of points and r is radius.
So pi is then approximately
Pi=4*M(How many points picked so far)/N(How many of those points fell inside the circle )
fprintf(“Approximate Value Of PI”);
% initialize value for loop
Count_inside=0;
Count=0;
d=0;
While count<10000
d=sqrt(rand^2+rand^2)
if(d<1)
count_inside+=1;
count+=1;
end
x=4.0*count_inside/count;
fprintf(“Approximate Value is %.15g”,x);
