Saturday, September 7, 2013

MONTE CARLO SIMULATION TO FIND THE AREA OF A CIRCLE

Monte Carlo Simulation to find the Area of a Circle.  The radius of the circle is assumed to be a positive integer

Note:- Here the idea is to inscribe a circle of radius r within a square of side 2r. Then generate as many random points as possible, which will all fall within the square.  The ratio of the number of points falling within the circle to the total number of points generated will be equal to the ratio of the area of the circle to the area of the square.  As the number of random points increase, accuracy also increases..



#include < stdio.h >
#include < conio.h >
#include < math.h >
#include < stdlib.h >
void main()
{
  int c,k;
  clrscr();
  printf("Enter the radius of the circle(Enter a positive integer)\n");
  scanf("%d",&c);
  int x=c, y=c;
  k=2*c;
  int i,x1,y1;
  float n=0,r,area;
  clrscr();
  randomize();
  for(i=1;i<=10000;i++)
    {
   x1= rand()%k;
   y1= rand()%k;
   printf("\n%d,  %d\n  ", x1,y1);
   r=((x1-x)*(x1-x))+((y1-y)*(y1-y));
   r=sqrt(r);
   if (r<=c)
   n=n+1;
   printf("\n%f\n",r);
   }
   printf ("\n%f\n", n);
   area=n*k*k;
   printf("%f\n",area);
   area= area/10000;
   printf("%f",area);
   getch();
 }

No comments: