Distance-Velocity Lag

One simple method to incorporate delay into simulation of differential equations is to step through an array of values adding the new value to the head of the list while removing the value that has been stored for the desired time from the bottom of the list. However, this is clumsy because the array must be reshuffled each time a number is placed at the top, numbers are moved, and a number is taken away. In other words, the value in the top box moves to the second box, the value in the second box moves to the third, etc. Actually the changes can move up from the bottom to save a line of program code because moving downward would overwrite the next number before it could be moved.

A much more efficient method is to move the subscript of the array. When the subscript reaches the end, it goesback to the beginning. The number to be delayed is added at thevalue of the subscript just after the old, delayed value is extracted. Only one box of the array is manipulated, and this is a great savings when there are many boxes.

You may ask why it matters to save some execution time for the CPU when computers have fantastic speed now a days. Consider using many boxes in the array to get precision of the delayed value. We may wish to update several thousand times per second. That would mean many unneeded computations when using the array that must be reshuffled each time there is a new entry.

Javascript Tip

var stack = [];
stack.push(2);       // push adds 2 to end of stack
stack.push(5);       // push adds 5 to end of stack
var i = stack.pop(); // get top of stack
OR
var i = stack.pop(n); //get nth entry of big stack