Random walks: Difference between revisions

From JSXGraph Wiki
No edit summary
No edit summary
Line 18: Line 18:
<option value="100">100</option>
<option value="100">100</option>
<option value="200">200</option>
<option value="200">200</option>
</select> where <i>stepsize=5</i> and <i>Number of steps per walk = 100</i>. Therefore, the expected
</select> <br />
squared distance from the starting point will be <math>100\cdot 5^2=2500</math>.<br />
<input type="button" value="run" onClick="run()">
<input type="button" value="run" onClick="run()">
<input type="button" value="clear" onClick="clearturtle()">
<input type="button" value="clear" onClick="clearturtle()">
<br />
<br />
</form></html>
We have set <i>stepsize=5</i> and <i>Number of steps per walk = 100</i>. Therefore, the expected
squared distance from the starting point will be <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">
Average square of the distance between starting point and endpoint of the walks: <input type="text" value="" id="output">
</form>
</form>

Revision as of 13:07, 27 May 2009

Number of random walks:

We have set stepsize=5 and Number of steps per walk = 100. Therefore, the expected squared distance from the starting point will be [math]\displaystyle{ 100\cdot 5^2=2500 }[/math].

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

Source code

<jsxgraph width="600" height="600">
var brd = JXG.JSXGraph.initBoard('jxgbox', {originX: 300, originY: 300, unitX: 3, unitY: 3});
var t = brd.createElement('turtle');

function run() {
  var i,j,dist,sumdist=0.0;
  var stepSize = 5; 
  brd.suspendUpdate();
  var nr = $('number').value*1;
  for (i=0;i<nr;i++) {
     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();
  }
  $('output').value = (sumdist/nr).toFixed(3);
  brd.unsuspendUpdate();
}
function clearturtle() {
  sumist = 0.0
  t.cs();
}
</jsxgraph>

External links