Version 5 of Simple Chaos Theory with Tcl

Updated 2004-07-01 02:21:51 by WJR

Chapter 3 of James Gleick's book on chaos (Chaos - Making a New Science, 1987) discusses how very simple equations can exhibit chaotic behavior when particular parameters are manipulated. The example equation he provides is a model for population growth in a fish pond, which looks like this:

http://jrankin.ath.cx/tclerswiki/equation.png

Meaning that the population for the next iteration (year, month, day, whatever) equals the rate of population growth r multiplied by the current population x multiplied by a term that keeps the population within bounds (e.g. as the population increases, food becomes increasingly scarce and some of our fish die or move to less crowded ponds).

The parameter manipulated is the rate of growth r, and Mr. Gleick discusses in depth what happens to the population as r is increased. He also provides several graphs that show how the fish population oscillates in regular patterns over multiple iterations and eventually becomes chaotic, i.e., no regular pattern is readily apparent.

For a fun exercise, and to help explain these concepts to myself, I scripted this model in Tcl to see if I could get similar results. I also wanted to output the data in such a way that I could import it into Excel to create graphs.

Well, fortunately the math was within my reach so generating the data was easy, and instead of using Excel I was able to use the emu_graph package to generate the graph on the fly.

Here's the script:

 #!/usr/local/ActiveTcl/bin/wish

 # Rate of population growth
 # Nice values 2, 2.75, 3, 3.25, 3.5, 4 (chaos ensues!)
 set r 2.75

 # Starting population
 set x .4
 set init_x $x

 # Number of iterations
 set iterations 50


 for {set i 1} {$i <= $iterations} {incr i} {
     set x [expr $r * $x * [expr 1 - $x]]
     lappend data $i
     lappend data $x
 }

 package require emu_graph
 wm title . "Simple Chaos Theory (r=$r, x=$init_x, $iterations\
         iterations)"

 canvas .c -width 500 -height 300
 pack .c 

 emu_graph::emu_graph graph -canvas .c -width 400 -height 225
 graph data d2 -colour red -points 0 -lines 1 -coords $data

Setting r to 2.75 results in the following graph, where the population oscillates for a while before settling down to a steady state.

http://jrankin.ath.cx/tclerswiki/2.75.png

Setting r to 3.25 results in a regular pattern that oscillates between levels each iteration, known as Period 2.

http://jrankin.ath.cx/tclerswiki/3.25.png

Setting r to 3.5 results in more complex, yet still regular, behavior known as Period 4.

http://jrankin.ath.cx/tclerswiki/3.5.png

Finally, increasing r to 4 results in apparent chaos. However, as Gleick writes, regularities will still appear now and then, only to give way to more chaotic behavior. My impression from the book thus far is that these regularities would not necessarily be visualized in graphs like these, but would be apparent through other means of visualizations (fractals, perhaps?).

http://jrankin.ath.cx/tclerswiki/4.png

Notes:

On my workstation, setting r to anything above 4 results in this error:

 Error in startup script: floating-point value too large to represent
     while executing
 "expr $r * $x * [expr 1 - $x]"