Difference between revisions of "Random walks"

From JSXGraph Wiki
Jump to navigationJump to search
 
(22 intermediate revisions by 2 users not shown)
Line 13: Line 13:
 
<option value="10">10</option>
 
<option value="10">10</option>
 
<option value="15">15</option>
 
<option value="15">15</option>
<option value="20">20</option>
+
<option value="20" selected>20</option>
 
<option value="30">30</option>
 
<option value="30">30</option>
 
<option value="50">50</option>
 
<option value="50">50</option>
 
<option value="100">100</option>
 
<option value="100">100</option>
 
<option value="200">200</option>
 
<option value="200">200</option>
</select> where <tt>stepsize=5</tt><br />
+
</select> <br />
<input type="button" value="run" onClick="run()">
+
<input type="button" value="run simulation" onClick="run()">
<input type="button" value="clear" onClick="clearturtle()">
+
<input type="button" value="clear screen" onClick="clearturtle()">
 
<br />
 
<br />
Average square length of the walks:<input type="text" value="" id="output">
+
</form></html>
 +
Fixed values in this simulation are:
 +
* stepsize <math>{}=5</math> and
 +
* Number of steps per walk <math> {}= 100</math>.
 +
Therefore, the expected
 +
squared distance from the starting point will be equal to
 +
* <math>100\cdot 5^2=2500</math>.
 +
<html>
 +
<form>
 +
Average square of the distance between starting point and endpoint of the walks: <input type="text" value="" id="output">
 
</form>
 
</form>
 
</html>
 
</html>
  
 
<jsxgraph width="600" height="600">
 
<jsxgraph width="600" height="600">
var brd = JXG.JSXGraph.initBoard('jxgbox', {originX: 300, originY: 300, unitX: 3, unitY: 3});
+
var brd = JXG.JSXGraph.initBoard('jxgbox', {boundingbox: [-100, 100, 100, -100]});
var t = brd.createElement('turtle');
+
var t = brd.create('turtle');
  
 
function run() {
 
function run() {
Line 35: Line 44:
 
   t.hideTurtle();
 
   t.hideTurtle();
 
   brd.suspendUpdate();
 
   brd.suspendUpdate();
   var nr = $('number').value*1;
+
   var nr = document.getElementById('number').value*1;
 
   for (i=0;i<nr;i++) {
 
   for (i=0;i<nr;i++) {
 +
    t.setPenColor(JXG.hsv2rgb(Math.round(Math.random()*255),Math.random(),Math.random()));
 
     for (j=0;j<100;j++) {
 
     for (j=0;j<100;j++) {
 
         var a = Math.floor(360*Math.random());  
 
         var a = Math.floor(360*Math.random());  
Line 46: Line 56:
 
     t.home();
 
     t.home();
 
   }
 
   }
   $('output').value = (sumdist/nr).toFixed(3);
+
   document.getElementById('output').value = (sumdist/nr).toFixed(3);
 
   brd.unsuspendUpdate();
 
   brd.unsuspendUpdate();
 
}
 
}
 
function clearturtle() {
 
function clearturtle() {
  sumist = 0.0
 
 
   t.cs();
 
   t.cs();
 
}
 
}
Line 56: Line 65:
  
 
===Source code===
 
===Source code===
<source lang="html4strict">
+
<source lang="javascript">
<jsxgraph width="600" height="600">
+
var brd = JXG.JSXGraph.initBoard('jxgbox', {boundingbox: [-100, 100, 100, -100]});
var brd = JXG.JSXGraph.initBoard('jxgbox', {originX: 300, originY: 300, unitX: 3, unitY: 3});
+
var t = brd.create('turtle');
var t = brd.createElement('turtle');
 
  
 
function run() {
 
function run() {
Line 65: Line 73:
 
   var stepSize = 5;  
 
   var stepSize = 5;  
 
   brd.suspendUpdate();
 
   brd.suspendUpdate();
   var nr = $('number').value*1;
+
   var nr = document.getElementById('number').value*1;
 
   for (i=0;i<nr;i++) {
 
   for (i=0;i<nr;i++) {
 +
    t.setPenColor(JXG.hsv2rgb(Math.round(Math.random()*255),Math.random(),Math.random()));
 
     for (j=0;j<100;j++) {
 
     for (j=0;j<100;j++) {
 
         var a = Math.floor(360*Math.random());  
 
         var a = Math.floor(360*Math.random());  
Line 76: Line 85:
 
     t.home();
 
     t.home();
 
   }
 
   }
   $('output').value = (sumdist/nr).toFixed(3);
+
   document.getElementById('output').value = (sumdist/nr).toFixed(3);
 
   brd.unsuspendUpdate();
 
   brd.unsuspendUpdate();
 
}
 
}
 
function clearturtle() {
 
function clearturtle() {
  sumist = 0.0
 
 
   t.cs();
 
   t.cs();
 
}
 
}
</jsxgraph>
 
 
</source>
 
</source>
  
 
===External links===
 
===External links===
 
* [http://en.wikipedia.org/wiki/Random_walk http://en.wikipedia.org/wiki/Random_walk]
 
* [http://en.wikipedia.org/wiki/Random_walk http://en.wikipedia.org/wiki/Random_walk]
 +
* [http://mathworld.wolfram.com/RandomWalk.html http://mathworld.wolfram.com/RandomWalk.html]
 +
* [http://www.bookrags.com/research/random-walks-and-brownian-motion-wop/ http://www.bookrags.com/research/random-walks-and-brownian-motion-wop/]
  
 
[[Category:Examples]]
 
[[Category:Examples]]
 
[[Category:Turtle Graphics]]
 
[[Category:Turtle Graphics]]
 +
[[Category:Statistics]]

Latest revision as of 13:19, 8 June 2011

Number of random walks:

Fixed values in this simulation are:

  • stepsize [math]{}=5[/math] and
  • Number of steps per walk [math] {}= 100[/math].

Therefore, the expected squared distance from the starting point will be equal to

  • [math]100\cdot 5^2=2500[/math].

Average square of the distance between starting point and endpoint of the walks:

Source code

var brd = JXG.JSXGraph.initBoard('jxgbox', {boundingbox: [-100, 100, 100, -100]});
var t = brd.create('turtle');

function run() {
  var i,j,dist,sumdist=0.0;
  var stepSize = 5; 
  brd.suspendUpdate();
  var nr = document.getElementById('number').value*1;
  for (i=0;i<nr;i++) {
     t.setPenColor(JXG.hsv2rgb(Math.round(Math.random()*255),Math.random(),Math.random()));
     for (j=0;j<100;j++) {
        var a = Math.floor(360*Math.random()); 
        t.right(a); 
        t.forward(stepSize);
     }
     dist = t.pos[0]*t.pos[0]+t.pos[1]*t.pos[1];
     sumdist += dist;
     t.home();
  }
  document.getElementById('output').value = (sumdist/nr).toFixed(3);
  brd.unsuspendUpdate();
}
function clearturtle() {
  t.cs();
}

External links