Monday, July 08, 2013

Lorezn in Living Color - Part 2: Solving the System

So where did we leave off....?  Oh yeah, the Lorenz system.

The Lorenz system is a three dimensional system that has this intriguing property in that as the system evolves over time, the solution never repeats yet tends to follow a semi-regular pattern.  My goal is to map the three co-ordinates of the solution onto a red, green, blue (RGB) color space so that I can see the system in another way.

The first challenge to overcome: since I was planning on implementing this project on an Arduino, I needed to solve the system of differential equations numerically.  I knew that solving differential equations this way was common (almost every computing device solves in this manner) but having never done it myself, I was a bit intimidated.  I knew from one of my recent math classes that the most common way of doing this was using an algorithm called Runge-Kutta, the fourth order variant being the most common:

(From Wikipedia)

(In the world of math and science, the most fame you can reasonably hope for is getting something you've done named after you.  This carries very little value outside the math and science circles, though.  Can you name more than ten famous mathematicians or scientists?  Come to think of it, if you're reading this blog you probably can. But I bet your next-door neighbor can't.)

Even though I working on my PhD in electrical engineering, I am not used to looking at numerical algorithms described in equations and  implementing them for, say, the Lorenz system.  I decided rather than start by writing code directly on the Arduino, I was going to use tool of every of every rookie to the field of numerical analysis: Microsoft Excel.  When you're just starting, Excel makes things easier by explicitly showing the  intermediate values for every step of the process.  Each intermediate value in the process has its own column with each step in time (n from the equations above) getting its own row.

After messing around in Excel for a while I had figured out how to interpret the equations for the algorithm and seemed to have produced results that were consistent with what I understood about the system.  I could see the x, y, and z values oscillating over time, never running away to infinite or getting stuck at any particular value and always circling back and forth.  Always moving, never repeating.

But these were just numbers and its hard to get a feel for how they would look if represented as RGB values. Engineering/Artistic epiphany: I could use Processing (a close cousin of the Arduino development system) to visualize the changing state of the system.  Processing was designed for real-time animation and visualization and I was sure there would be a way to take the values from my Excel spreadsheet and use it to draw a string of circles filled with the colors produced by the calculations.

And it was.


(The full animation has 50 circles; you're just seeing a portion of it here.)

I noticed that the colors that were being produced by these solutions were not that, uhmm, colorful.  I was already scaling the values from the Lorenz solutions to make them fit the way the RGB color definition used by processing (0-255 for each color channel); a little bit more tweaking probably wouldn't hurt anything. It took a few guess-and-check attempts but I eventually found a simple equation that changed numerical solutions from the Lorenz system into color values I liked.  





Using Processing to check my work was a great idea and it gave me confidence I was on the right track.I could see the results of the Runge-Kutta algorithm, see the progression of the colors, and verify that, for my purposes, the system was producing useful results.

Next up: moving all of this work I had done in Excel onto the Arduino.



No comments:

Post a Comment