Coding

More Strange Attractors

As I mentioned toward the end of my post on the Python/GTK+ implementation of the Lorenz attractor, I ultimately wanted to add some of the other strange attractors to my program in order to make it a bit more interesting. So I did just that. It also gave me a good excuse to learn a bit more about how to use and layout widgets in a GTK+ application.

In addition to the Lorenz attractor, the program now will generate the Rössler Attractor and a plot of the behavior of Chua’s Circuit. The Rössler Attractor is another well-known system in the world of chaos theory, which you can learn more about by following the above link. Since I’m only plotting a 2D section of each attractor, I have to decide which view to display. In the case of the Rössler, I thought that the X-Y view was better than the X-Z view.

More interesting to me is the simulation of Chua’s Circuit, as this is based on an actual analog circuit you can build. The circuit is a chaotic oscillator that consists of the usual L-C elements (and a resistor plus limiting diodes) along with a nonlinear negative resistance circuit element. The negative resistance element is usually implemented with an active device such as an op-amp, although it has been reported that a memristor can also serve this function. The simulation is a system of three ordinary differential equations, much like the Lorenz or Rössler systems, but with a function in the first ODE to represent the behavior of the nonlinear negative resistance element. You can see in the code listing below that this was easy to implement in Python with a lambda function. It’s cool to see the pattern drawn on a display, but I think it would be much better to have an actual circuit render it on an analog oscilloscope. One day, I hope to do that, but in the mean time, enjoy these videos of the behavior of such a circuit.

As far as my additions to the Python code, I created a GTK DrawingArea for each attractor, then added them to a Stack, which allows them to be switched with the StackSwitcher widget at the bottom of the screen. For clarity, I also added a legend to each DrawingArea to display which axes are being rendered for each attractor, as the Rössler has a different view from the other two. This code is a bit longer than the initial iteration, but much of it is similar, since the calculation and plotting of each system is nearly the same (yes, I could have factored the code quite a bit, but this is just for fun). Another fun time with code was had!

Coding

Lorenz Attractor in Python

Back in the day, when I was a budding nerd in the late 80s/early 90s, I spent a lot of my free time down at the local public library looking for any books I could find regarding certain topics which captured my interest: programming, astronomy, electronics, radio, mathematics (especially the recreational mathematics books), and other topics in the realm of science.

One of my fondest recollections of that time was how accessible that home investigations into some of these topics became due to the advent of the personal computer. Of course, in those times, not every household had some kind of computing device like they do today, but PCs were affordable enough that even a lower middle class house like ours could scrape together enough for a computer with a bit of work.

We also didn’t have widespread household Internet, so your options for getting new programs to play with were limited to the relatively expensive services like Compuserve, trying to find warez on bulletin board systems, checking out books and magazines with program listings from the public library, or perhaps if you were lucky, being able to check out a stack of 3.5 in floppies from the library (how were they able to do that with commercial software?). Of course, given the previously mentioned socio-economic status, I was mostly limited to the public library option. Although perhaps some day I’ll tell the tale of how I talked my parents into letting me get Compuserve, and then proceeding to rack up a huge access bill on my parents’ credit. Oops.

Of particular interest to me was the relatively new field of chaos theory and fractals. These studies were conceived a bit earlier than this time period, but were popularized in the public eye during this time. I got hooked in by the photos published in the groundbreaking books The Fractal Geometry of NatureChaos: Making a New Science, and The Beauty of Fractals. There were also plenty of articles in publications like Scientific American and the books of authors like Clifford Pickover. The wonderful thing about these resources is that many of them not only showed you the pretty photos of fractals and chaotic systems, but actually described and illustrated the algorithms behind them, which allowed you to code your own implementations at home.

During my early work in trying to recreate these forms in my own computer, I had an Atari 800XL with a dodgy floppy disk drive, which made any kind of coding a bit of an adventure as it seemed like my mass storage old successfully worked about half of the time (on a side note, who remembers the even earlier days, where you would type in a machine language monitor program in BASIC, and then transcribe strings of ML from a listing in a magazine in order to get a new program…good times). Things really got serious when we ended up acquiring a Tandy 1000 (I believe the RL version, but I’m not 100% certain about that). Once we had that in the house, I spent many late weekend nights trying to write code to reproduce the fascinating patterns found in the pages of those books. You know you’re a true nerd when you get such an electric thrill from finally mastering the code to generate a Sierpinski triangle or Barnsley fern in glorious CGA on your own monitor.

So what’s the point of this overwrought bout of nostalgia? Well, I was recently pining for the old days when you could just sit down at the PC and implement a chaos system in one quick setting with minimal fuss. Compiled languages with arcane GUI frameworks were right out. Fortunately, we are blessed with quite a few good replacements for the old BASIC environment. My favorite is Python, and since I use Linux Mint as my primary desktop OS, the GTK+ 3 libraries for Python are already included by default, so it’s quite easy to get a rudimentary 2D graphics system up and running quickly.

For my first chaos system coding challenge, I decided to go with the great-granddaddy of chaos: the Lorenz attractor. It’s an easy system to compute, and it’s quite obvious if you get the implementation right or wrong. Once I got the hang of the GTK+ 3 library interface, it didn’t take that long to bang out an implementation of the Lorenz attractor in relatively few lines of Python. The simplicity is satisfying, and reminds me of the fabled old days of coding.

There’s the code if any of you would like to play around with it. It should be fairly easy to replicate if you are using any of the Debian-derived Linux distributions, and probably only marginally more difficult with other Linux distros. I have no idea what it would take to get running on Windows, so good luck if that’s your OS of choice.

Now I have a framework to build off if I get a further itch for similar experiments. I’m already working on an extension to this code that will render other attractors. It would be fun to find a 3D rendering library that would be easy to use so that I could plot in three dimensions, but that’s not hugely critical to me. This is just an exercise to have some fun, capture a bit of that nostalgia, and distract myself a bit during downtime. Hopefully a few of you kindred souls will have derived some enjoyment from this trip down memory lane.