/* To find the area of a unit circle by Monte Carlo random dot method */
#include<stdio.h>
#include<math.h>
int main()
{
float x, y, r, sqr, ratio;
float xmax=2, ymax=2, h=0.0005, k=0.0005, area, rad=1;
float cnt_tot=0, cnt_acc=0, cnt_rej=0;
for(x=0;x<=ymax;x+=h)
{
for(y=0;y<=ymax;y+=k)
{
sqr=(x-1)*(x-1)+(y-1)*(y-1);
r=sqrt(sqr);
if(r<=rad)
{
cnt_acc+=1;
}
else
{
cnt_rej+=1;
}
}
}
cnt_tot=cnt_acc+cnt_rej;
printf("Total random number of dots generated=%f", cnt_tot);
ratio=cnt_acc/cnt_tot;
printf("\nRatio=%f", ratio);
area=ratio*ymax*xmax;
printf("\nTotal area=%f", ymax*xmax);
printf("\nArea of the circle = (Ratio) X (Total area)");
printf("\nArea of the circle = %f",area);
return(0);
}
#include<stdio.h>
#include<math.h>
int main()
{
float x, y, r, sqr, ratio;
float xmax=2, ymax=2, h=0.0005, k=0.0005, area, rad=1;
float cnt_tot=0, cnt_acc=0, cnt_rej=0;
for(x=0;x<=ymax;x+=h)
{
for(y=0;y<=ymax;y+=k)
{
sqr=(x-1)*(x-1)+(y-1)*(y-1);
r=sqrt(sqr);
if(r<=rad)
{
cnt_acc+=1;
}
else
{
cnt_rej+=1;
}
}
}
cnt_tot=cnt_acc+cnt_rej;
printf("Total random number of dots generated=%f", cnt_tot);
ratio=cnt_acc/cnt_tot;
printf("\nRatio=%f", ratio);
area=ratio*ymax*xmax;
printf("\nTotal area=%f", ymax*xmax);
printf("\nArea of the circle = (Ratio) X (Total area)");
printf("\nArea of the circle = %f",area);
return(0);
}
Comments
Post a Comment