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 over 12 years ago

pi 

Tagged by Firat Soylu over 12 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 909 times • Downloaded 65 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.)


WHAT IS IT?

(a general understanding of what the model is trying to show or explain)

HOW IT WORKS

(what rules the agents use to create the overall behavior of the model)

HOW TO USE IT

(how to use the model, including a description of each of the items in the Interface tab)

THINGS TO NOTICE

(suggested things for the user to notice while running the model)

THINGS TO TRY

(suggested things for the user to try to do (move sliders, switches, etc.) with the model)

EXTENDING THE MODEL

(suggested things to add or change in the Code tab to make the model more complicated, detailed, accurate, etc.)

NETLOGO FEATURES

(interesting or unusual features of NetLogo that the model uses, particularly in the Code tab; or where workarounds were needed for missing features)

RELATED MODELS

(models in the NetLogo Models Library and elsewhere which are of related interest)

CREDITS AND REFERENCES

(a reference to the model's URL on the web if it has one, as well as any other necessary credits, citations, and links)

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 over 12 years ago by Firat Soylu.

Attached files

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

This model does not have any ancestors.

Children:

Graph of models related to 'Monte Carlo Pi Model'