Strange Attractor Series #2

This one is more or less a straight port from the BASIC code at the end of chapter one of the book.

[kml_flashembed movie=”http://www.bit-101.com/sprottbook/StrangeAttractors_02.swf” width=”550″ height=”400″/]

So, what’s this all about? From the book:

Figure 1-4 provides a good graphical illustration of the sensitivity to initial
conditions. The horizontal axis represents all possible initial conditions from zero to
one. The vertical axis shows the value from zero to one corresponding to each initial
condition after five iterations. It’s not hard to see that two nearby points on the
horizontal axis usually translate into two very different values along the vertical axis
after five iterations. Try using PREV% = 10, and convince yourself that information
about the initial condition is almost completely lost after ten iterations.

Basically, it’s using the same n1 = Rn(1-n) equation as before. R=4 and n=0.05. It iterates through plotting n compared to previous n’s. The PREV% variable mentioned is set by changing the text field in the top left. If PREV is 1, it plots n, n1. This shows that for a single iteration of the formula with R=4, the results are pretty predictable. Change PREV to 5 and you have what is described in the paragraph above. A small change in input conditions can result in a very large change in output. This is the butterfly effect – butterfly flapping its wings in location x can cause a tornado half way around the world. After 10 iterations (PREV = 10), it’s pretty much chaos. For two nearly identical inputs, there’s no way to predict the outcome.

One of my favorite analogies from the book is this:

Try laying two identical garden hoses side by side, and turn on the water in each one at the same time without holding the ends.

Here you have two nearly identical inputs – hoses laying side by side – but even the slight differences between them will result in utterly unpredictable results within seconds (a few iterations).

Oh, and here’s the code, in all it’s timeline ugliness:

[as]var bmpd:BitmapData;
var n:Number;
var nNew:Number;
var R:Number;
var ns:Array;
var p:int;
var tf:TextField;
var prev:int;

init();
setParams();

function init():void
{
bmpd = new BitmapData(550, 400, false);
addChild(new Bitmap(bmpd));

tf = new TextField();
tf.type = TextFieldType.INPUT;
tf.defaultTextFormat = new TextFormat(“_sans”, 12, 0);
tf.text = “1”;
tf.restrict = “123456790”;
tf.maxChars = 2;
tf.width = 25;
tf.height = 20;
tf.border = true;
tf.background = true;
addChild(tf);
tf.addEventListener(Event.CHANGE, onPrevChange);

addEventListener(Event.ENTER_FRAME, onEnterFrame);
}

function onPrevChange(event:Event):void
{
if(tf.text == “0”)
{
tf.text = “1”;
}
if(tf.text == “”) return;
bmpd.fillRect(bmpd.rect, 0xffffff);
setParams();
}

function setParams():void
{
ns = new Array();
p = 0;
prev = int(tf.text);
n = .05;
R = 4;
}

function iterate():void
{
nNew = R * n * (1 – n);
}

function displayResults():void
{
ns[p] = n;
p = (p + 1) % 500;
var i:int = (p + 500 – prev) % 500;
bmpd.setPixel(550 * ns[i], 400 – 400 * nNew, 0);
}

function testResults():void
{
n = nNew;
}

function onEnterFrame(event:Event):void
{
iterate();
displayResults();
testResults();
}[/as]

As the examples in the book get more useful, I’ll probably start making real classes with them. These first few are just explorations.

This entry was posted in Flash, General. Bookmark the permalink.

5 Responses to Strange Attractor Series #2

  1. uri says:

    Me baffled:

    var i:int = (p + 500 – prev) % 500;
    bmpd.setPixel(550 * ns[i], 400 – 400 * nNew, 0);

  2. kp says:

    hehe. Well, like I said, it’s pretty much a straight port of the BASIC code from the book. I probably could have cleaned it up more, but it’s just an exploration.

  3. kp says:

    also, the book explains the code a bit more. i didn’t want to clean it up to much as it’s mentioned that other programs build on the base.

  4. Ryan Taylor says:

    Pretty cool, Keith.

    Does that book get into Lissajous curves at all? I did some experimenting with those last year and came up with pretty interesting results:

    http://www.boostworthy.com/blog/?p=92

  5. kp says:

    No, I don’t think it gets into Lissajous curves at all.

Leave a Reply