Chapter 7
This presentation is the property of its rightful owner.
Sponsored Links
1 / 5

Chapter 7 PowerPoint PPT Presentation


  • 38 Views
  • Uploaded on
  • Presentation posted in: General

Chapter 7. Monte Carlo Methods to Compute Pi. Circle formed within a square, with unit radius so that square has sides 2*2. Ratio of the area of the circle to the square given by Points within square chosen randomly Score kept of how many points happen to lie within circle

Download Presentation

Chapter 7

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Chapter 7

Chapter 7


Monte carlo methods to compute pi

Monte Carlo Methods to Compute Pi

  • Circle formed within a square, with unit radius so that square has sides 2*2. Ratio of the area of the circle to the square given by

  • Points within square chosen randomly

  • Score kept of how many points happen to lie within circle

  • Fraction of points within the circle will be pi/4, given a sufficient number of randomly selected samples


Monte carlo methods to compute pi1

Monte Carlo Methods to Compute Pi


Monte carlo methods to compute pi2

Monte Carlo Methods to Compute Pi

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

int main(int argc, char* argv[])

{ double niter = 100000000;

double x,y;

int i;

int count=0;

double z;

double pi;

srand(time(NULL));

for (i=0; i<niter; ++i)

{ //get random points

x = (double)random()/RAND_MAX;

y = (double)random()/RAND_MAX;

z = sqrt((x*x)+(y*y));

if (z<=1)

{

++count;

}

}

pi = ((double)count/(double)niter)*4.0;

printf("Pi: %f\n", pi);

}


Monte carlo methods to compute pi3

Monte Carlo Methods to Compute Pi

[email protected]:~/csc229> ./pi

Pi: 3.141370


  • Login