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.

JuliaSet 032 0043i.jpg

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.

Tandy 1000 rl 1.jpg

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.

Barnsley fern plotted with VisSim.PNG

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.

5 thoughts on “Lorenz Attractor in Python

  1. Hi Jason.
    Nice post, I share many of your experiences and feelings about those years 🙂 I started on a Sinclair QL to later move to the Amiga 1000 and 3000. Mandelbrot, Julia and others have been in my radar for quite a good time 🙂

    Now I also use Python on Raspbian (Raspberry PI) , Debian Jessie and Windows. On Windows the easisest way to setup a free cool environment with lots of Python modules is Anaconda https://www.continuum.io/downloads

    I also suggest you try using jupyter notebooks (also available within Anaconda) http://jupyter.org/ they will give you lots of interactivity and notebooks are also easy to share on the web.

    Bye,
    Mario

  2. I was looking forward to playing around with this code. Sadly, I can’t seem to get the appropriate version of the gi module installed.

  3. ahh, i remember well the heady days of picking up the latest issue of Nibble from the computer store at the mall and spending afternoons deciphering and typing in programs into my Apple //e.

    All those hex code listings…

    good times those 🙂

Leave a Reply