Monte Carlo Pi Model

Monte Carlo Pi Model preview image

1 collaborator

Default-person Firat Soylu (Author)

Tags

monte carlo simulation 

Tagged by Firat Soylu about 11 years ago

pi 

Tagged by Firat Soylu about 11 years ago

Parent of 1 model: Child of Monte Carlo Pi Model
Visible to everyone | Changeable by the author
Model was written in NetLogo 5.0.2 • Viewed 678 times • Downloaded 46 times • Run 0 times
Download the 'Monte Carlo Pi Model' modelDownload this modelEmbed this model

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)


Comments and Questions

Please start the discussion about this model! (You'll first need to log in.)

Click to Run Model

globals [estimated-pi error-rate real-pi avg-pi circle-radius]

to setup 
  clear-all
  ;set real-pi 3.1415926535897
  set real-pi 4 * (count patches with [pcolor = yellow] / count patches)
  set circle-radius 200
  ask patches [
    if abs(pxcor) < circle-radius and abs(pycor) < circle-radius [set pcolor red]
    if distance patch 0 0 < circle-radius [set pcolor yellow]
  ]
  set sand-number 100
  reset-ticks
  set real-pi 4 * (count patches with [pcolor = yellow] / count patches)
end 

to incremental-go
  ifelse sand-number < max-sand-number [set sand-number sand-number + increment-by][stop]
  let i  0
  set avg-pi 0
  let accumulated-values 0
  while [i < sample-size] [
    ask turtles [die]
    throw-sand
    calculate-pi
    set accumulated-values accumulated-values + estimated-pi 
    set i (i + 1)
  ]
  set avg-pi  accumulated-values / sample-size
  show avg-pi
  tick
end 

to-report random-one ;reports 1 or -1
  report ifelse-value (random 2 = 0) [-1][1]
end 

to throw-sand
  set-default-shape turtles "circle"
  ifelse full-circle? = True 
  [
    ifelse grains-pile? = True
      [ create-turtles sand-number [set color black set size grain-size move-to one-of patches]]
      [create-turtles sand-number [set color black set size grain-size move-to one-of patches with [count turtles-here < 1]]]
  ]
  [
    ifelse grains-pile? = True
      [ create-turtles sand-number [set color black set size grain-size move-to one-of patches with [pxcor > 0 and pycor > 0]]]
      [create-turtles sand-number [set color black set size grain-size move-to one-of patches with [pxcor > 0 and pycor > 0 and count turtles-here < 1]]]
  ]   
end 

to calculate-pi
  let circle-turtles  turtle-set turtles with [[pcolor] of patch-here = yellow ]
  set estimated-pi 4 * count circle-turtles / count turtles
  set error-rate 100 * abs(estimated-pi - real-pi) / real-pi
end 

There is only one version of this model, created about 11 years ago by Firat Soylu.

Attached files

File Type Description Last updated
Monte Carlo Pi Model.png preview Preview for 'Monte Carlo Pi Model' about 11 years ago, by Firat Soylu Download

This model does not have any ancestors.

Children:

Graph of models related to 'Monte Carlo Pi Model'