# Calculate integral by Monte Carlo method

Model was written in NetLogo 5.0.4
•
Viewed 312 times
•
Downloaded 18 times
•
Run 0 times

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.

## Attached files

File | Type | Description | Last updated | |
---|---|---|---|---|

Calculate integral by Monte Carlo method.png | preview | Preview | over 7 years ago, by Do Trong Thanh | Download |

This model does not have any ancestors.

This model does not have any descendants.