Difference between revisions of "Calculate Pi with Monte-Carlo-method"

From JSXGraph Wiki
Jump to: navigation, search
 
(4 intermediate revisions by 2 users not shown)
Line 4: Line 4:
 
This can be used to calculate <math>\pi</math> using statistics. After each update we count the number of points inside the circle (with midpoint [0,0] and radius 1) and outside the circle. The points are restricted to the square with midpoint [0,0] and edges of length 2.
 
This can be used to calculate <math>\pi</math> using statistics. After each update we count the number of points inside the circle (with midpoint [0,0] and radius 1) and outside the circle. The points are restricted to the square with midpoint [0,0] and edges of length 2.
  
 +
<html><div id="resulttext"></div></html>
 
Please, move the mouse pointer over this area:
 
Please, move the mouse pointer over this area:
<html>
+
<jsxgraph box="jxgbox" width="400" height="400">
<link rel="stylesheet" type="text/css" href="http://jsxgraph.uni-bayreuth.de/distrib/jsxgraph.css" />
 
<script type="text/javascript" src="http://jsxgraph.uni-bayreuth.de/distrib/prototype.js"></script>
 
<script type="text/javascript" src="http://jsxgraph.uni-bayreuth.de/distrib/jsxgraphcore.js"></script>
 
<div id="resulttext"></div>
 
<div id="jxgbox" class="jxgbox" style="width:400px; height:400px;" onmousemove="board2.update()"></div>
 
<script language="JavaScript">
 
 
  board2 = JXG.JSXGraph.initBoard('jxgbox', {boundingbox: [-1, 1, 1, -1], axis:true});
 
  board2 = JXG.JSXGraph.initBoard('jxgbox', {boundingbox: [-1, 1, 1, -1], axis:true});
 
  c = board2.createElement('circle', [[0,0], 1]);
 
  c = board2.createElement('circle', [[0,0], 1]);
board2.suspendUpdate();
 
 
  var p2 = [];
 
  var p2 = [];
 
  for (var i=0;i<50;i++) {
 
  for (var i=0;i<50;i++) {
 
   p2[i] = board2.createElement('point',
 
   p2[i] = board2.createElement('point',
 
           [function(){return 2*Math.random()-1;},function(){ return 2*Math.random()-1;}],
 
           [function(){return 2*Math.random()-1;},function(){ return 2*Math.random()-1;}],
           {style:5,name:' '});
+
           {face:'o',size:3,name:' '});
 
  }
 
  }
board2.unsuspendUpdate();
 
  
 
  var tin = 0;
 
  var tin = 0;
 
  var tout = 0;
 
  var tout = 0;
 +
var count = 0;
  
 
  function updText() {
 
  function updText() {
 +
  count++;
 
   var text = '';
 
   var text = '';
  
Line 45: Line 40:
 
   tout += outp;
 
   tout += outp;
  
   text += '<b><u>Current</u></b><br/>In: ' + inp + ', out: ' + outp + ', total: ' + (inp+outp) + '; ratio: ' + (inp/(inp+outp)) + ', ratio*4: ' + (4*inp/(inp+outp)) + '.<br /><b><u>Total</u></b><br/>In: ' + tin + ', out: ' + tout + ', total: ' + (tin+tout) + '; ratio: ' + (tin/(tin+tout)) + ', ratio*4: ' + (4*tin/(tin+tout));
+
   text += '<b><u>Current</u></b><br/>In: ' + inp + ', out: ' + outp + ', total: ' + (inp+outp) + '; ratio: ' + (inp/(inp+outp)) + ', ratio*4: ' + (4*inp/(inp+outp)) + '.<br /><b><u>Total</u></b> (' + count + ' updates in total)<br/>In: ' + tin + ', out: ' + tout + ', total: ' + (tin+tout) + '; ratio: ' + (tin/(tin+tout)) + ', ratio*4: ' + (4*tin/(tin+tout));
   $('resulttext').innerHTML = text;
+
   document.getElementById('resulttext').innerHTML = text;
 
  }
 
  }
  
 
   var hookid = board2.addHook(updText);
 
   var hookid = board2.addHook(updText);
 +
  JXG.addEvent(document.getElementById('jxgbox'), 'mousemove', function () { this.update(); }, board2);
  
</script>
+
</jsxgraph>
</html>
 
  
<source lang="html4strict">
+
==Code==
<div id="jxgbox" class="jxgbox" style="width:400px; height:400px;" onmousemove="board2.update()"></div>
+
Addition to the usual JSXGraph container division we need another div tag to hold the text output seen above:
 +
<source lang="xml">
 +
<div id="resulttext"></div>
 
</source>
 
</source>
  
 +
The JavaScript code:
 
<source lang="javascript">
 
<source lang="javascript">
board2 = JXG.JSXGraph.initBoard('jxgbox', {originX: 10, originY:390 , unitX:380 , unitY: 380});
+
board2 = JXG.JSXGraph.initBoard('jxgbox', {boundingbox: [-1, 1, 1, -1], axis:true});
board2.suspendUpdate();
+
c = board2.createElement('circle', [[0,0], 1]);
for (var i=0;i<50;i++) {
+
var p2 = [];
  var p2 = board2.createElement('point',
+
for (var i=0;i<50;i++) {
          [function(){return Math.random();},function(){ return Math.random()}],
+
    p2[i] = board2.createElement('point',
          {style:5,name:' '});
+
        [function(){return 2*Math.random()-1;},function(){ return 2*Math.random()-1;}],
}
+
        {size:3,face:'o',name:' '});
board2.unsuspendUpdate();
+
}
 +
 
 +
var tin = 0;
 +
var tout = 0;
 +
var count = 0;
 +
 
 +
function updText() {
 +
    count++;
 +
    var text = '';
 +
 
 +
    var inp = 0;
 +
    var outp = 0;
 +
 
 +
    for(var i=0; i<p2.length;i++) {
 +
        var x = p2[i].X();
 +
        var y = p2[i].Y();
 +
 
 +
        if(x*x+y*y <= 1) {
 +
            inp++;
 +
        } else {
 +
            outp++;
 +
        }
 +
    }
 +
    tin += inp;
 +
    tout += outp;
 +
 
 +
    text += '<b><u>Current</u></b><br/>In: ' + inp + ', out: ' + outp + ', total: ' + (inp+outp) + '; ratio: ' + (inp/(inp+outp)) + ', ratio*4: ' + (4*inp/(inp+outp)) + '.<br /><b><u>Total</u></b> (' + count + ' updates in total)<br/>In: ' + tin + ', out: ' + tout + ', total: ' + (tin+tout) + '; ratio: ' + (tin/(tin+tout)) + ', ratio*4: ' + (4*tin/(tin+tout));
 +
    document.getElementById('resulttext').innerHTML = text;
 +
}
 +
 
 +
var hookid = board2.addHook(updText);
 +
JXG.addEvent(document.getElementById('jxgbox'), 'mousemove', function () { this.update(); }, board2);
 
</source>
 
</source>
  
 
[[Category:Examples]]
 
[[Category:Examples]]
 
[[Category:Statistics]]
 
[[Category:Statistics]]

Latest revision as of 13:18, 7 June 2011

Here, at construction time each point receives a function pair as coordinates. In each update these functions which return Math.random() are called. Thus in each update each point receives new random coordinates. The 50 points are updated on the onmousemove event.

This can be used to calculate [math]\pi[/math] using statistics. After each update we count the number of points inside the circle (with midpoint [0,0] and radius 1) and outside the circle. The points are restricted to the square with midpoint [0,0] and edges of length 2.

Please, move the mouse pointer over this area:

Code

Addition to the usual JSXGraph container division we need another div tag to hold the text output seen above:

<div id="resulttext"></div>

The JavaScript code:

board2 = JXG.JSXGraph.initBoard('jxgbox', {boundingbox: [-1, 1, 1, -1], axis:true});
c = board2.createElement('circle', [[0,0], 1]);
var p2 = [];
for (var i=0;i<50;i++) {
    p2[i] = board2.createElement('point',
        [function(){return 2*Math.random()-1;},function(){ return 2*Math.random()-1;}],
        {size:3,face:'o',name:' '});
}

var tin = 0;
var tout = 0;
var count = 0;

function updText() {
    count++;
    var text = '';

    var inp = 0;
    var outp = 0;

    for(var i=0; i<p2.length;i++) {
        var x = p2[i].X();
        var y = p2[i].Y();

        if(x*x+y*y <= 1) {
            inp++;
        } else {
            outp++;
        }
    }
    tin += inp;
    tout += outp;

    text += '<b><u>Current</u></b><br/>In: ' + inp + ', out: ' + outp + ', total: ' + (inp+outp) + '; ratio: ' + (inp/(inp+outp)) + ', ratio*4: ' + (4*inp/(inp+outp)) + '.<br /><b><u>Total</u></b> (' + count + ' updates in total)<br/>In: ' + tin + ', out: ' + tout + ', total: ' + (tin+tout) + '; ratio: ' + (tin/(tin+tout)) + ', ratio*4: ' + (4*tin/(tin+tout));
    document.getElementById('resulttext').innerHTML = text;
}

var hookid = board2.addHook(updText);
JXG.addEvent(document.getElementById('jxgbox'), 'mousemove', function () { this.update(); }, board2);