Calculate integral by Monte Carlo method

Calculate integral by Monte Carlo method preview image

1 collaborator

Default-person Do Trong Thanh (Author)

Tags

(This model has yet to be categorized with any tags)
Visible to everyone | Changeable by the author
Model was written in NetLogo 5.0.4 • Viewed 432 times • Downloaded 33 times • Run 0 times
Download the 'Calculate integral by Monte Carlo method' 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

;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; variable declarations ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;

globals 
[
  value-integral 
  approx-integral
  scale-factor
]

;;;;;;;;;;;;;;;;;;;;;;
;; setup procedures ;;
;;;;;;;;;;;;;;;;;;;;;;

to setup
  clear-all
  set scale-factor 25
  resize-world 0 scale-factor 0 scale-factor
  set-default-shape turtles "dot"
  reset-ticks
  if (function = "y = x^5")
  [
    set value-integral 1 / 6
  ]
  if (function = "y = (cos x)^2")
  [
    set value-integral 0.7273243567
  ]
  if (function = "y = (sin (1/x))^2")
  [
    set value-integral 0.6734567683
  ]
end 

to-report scale [ x ]
  report (x / scale-factor)
end 

to-report degree [ x ]
  report (x * 180 / pi)
end 

to-report value-function [ x ]
  if (function = "y = x^5")
  [
    report (scale x) * (scale x) * (scale x) * (scale x) * (scale x)
  ]
  if (function = "y = (cos x)^2")
  [
    report (cos (degree (scale x))) * (cos (degree (scale x)))
  ]
  if (function = "y = (sin (1/x))^2")
  [
    report (sin (degree (1 / (scale x)))) * (sin (degree (1 / (scale x))))
  ]
end 

;;;;;;;;;;;;;;;;;;;;;;;;
;; runtime procedures ;;
;;;;;;;;;;;;;;;;;;;;;;;;

to go
  create-turtles 10
  [
    set size 0.5
    setxy random-float scale-factor random-float scale-factor
    set color yellow
    if (ycor < scale-factor * (value-function xcor))
    [
      set color red
    ]
  ]
  set approx-integral (count turtles with [color = red]) / (count turtles)
  tick
end 

There are 3 versions of this model.

Uploaded by When Description Download
Do Trong Thanh about 11 years ago Add comment Download this version
Do Trong Thanh about 11 years ago Improve GUI Download this version
Do Trong Thanh about 11 years ago Initial upload Download this version

Attached files

File Type Description Last updated
Calculate integral by Monte Carlo method.png preview Preview about 11 years ago, by Do Trong Thanh Download

This model does not have any ancestors.

This model does not have any descendants.