Lorenz attractor animation (Plotly)

Once for a class assignment, we were asked to control the Lorenz system. The instructor recommended us to use MATLAB for assignments, but since I’m inexperienced in MATLAB, I decided to use R to do the assignments, and used the package plotly to make interactive 3D plots of phase portraits1 of the Lorenz system. Later, I made the iconic animation to demonstrate chaos – how minuscule differences in the initial condition of the system gets magnified exponentially, resulting into significantly different outcomes. Here, I’ll share how to make such an animation with plotly.

Where \(t\) denotes time, \(x\) fluid motion, \(y\) horizontal temperature, \(z\) vertical temperature, and \(\sigma, \rho,\) and \(\beta\) parameters that specifies the particulars of the system that are constant through time. For \(\sigma = 10, \rho = 28, \beta = 8/3\), this system demonstrates chaotic behavior. Its phase portraits is the iconic butterfly, as we shall soon see.

Let’s solve the system of equations numerically first. There’re a number of packages that can numerically solve differential equations in R. One of them is deSolve, which is the one I’m using here. Another one I have used is pracma, which deliberately imitates the syntax of MATLAB. I don’t know enough math to solve the equations analytically, so I resort to a numeric solution.

What I mean by “numeric” is that time is discretized (here into steps of 0.01 time units), and since we know from the equations themselves how \(x,y,z\) change through time, we can use a variety of methods to compute what the values of \(x,y,z\) in the next time step will be given their values at present, starting at the initial condition (when time is 0). Here I used ode45, which is a popular method.

Cool, it’s the iconic butterfly! We also see how the state of the system traverses between the two wings of the butterfly through time.

It’s actually not difficult to add an animated point traversing the phase space through time. I’m adding one point, that moves along the \(x,y,z\) as specified in the sol data frame through time (frame argument). In the function animation_opts, frame = 1 means 1 millisecond per frame. redraw = FALSE means you can’t toggle the camera perspective while the animation is playing; this improves performance. This may take a while to render.