Easy Chaos with Pluto
Subtitle: Simply Making a Mess of Everything
# Getting Started with Pluto
Pluto is a notebook environment for Julia. Install Julia and start it to get a command window. The first step is to add some packages we’ll use for this project. Type “]” at the prompt (without the quotes) to enter the package manager. The packages you’ll need are:
It may take a few minutes as Julia checks and downloads all necessary dependencies. When everything has finished, type “Ctrl-C” to exit the package manager. In the Julia command window, type “Using Pluto” followed by “Pluto.run()”:
A new tab will open in your browser:
Download EasyChaos.jl from Github and then enter the path in Pluto below “Open from file:”. The notebook should look very much like the remainder of this post.
# Easy Chaos!
Pluto is a programming environment for Julia, designed to be interactive and helpful. Changes made anywhere in the notebook affect the entire notebook. Pluto is reactive so you don’t need to recalculate cells.
This is useful in studying chaos because small changes made to a function can have big effects. You can define a function, run it, and immediately see the effects.
# A Linear Equation
The equation of a line is where is the slope and is the intercept. The slope is how much changes for each unit change in . For any starting point if you move one step to the right to , the new value becomes . Subtract and all that’s left is .
A step by in the direction gives a change by in the direction, which is the slope of the line.
If , then so the intercept is . Since two points define a line, then start at on the axis, move to and add (or subtract if is negative) to get a second point. Draw your line through the two points.
The line can also be defined as a function,
Let’s start by plotting some lines.
We need a way to change the values of and . First, we’ll make a slider for the slope and set the initial value to .
Next, we’ll make a slider for and set it to . Both can be varied between and .
Notice that the origin of the plot is in the center. Move the sliders for and to get a new plot. Interesting, but not chaos.
# A Little Chaos
Chaos is doing nearly the same thing over and over and expecting wildly different results. Let’s start with the linear equation above, with and .
Starting at , the output of the function is . Using this new value as the input, . If you do this several times you get the uninteresting sequence
Instead of calculating and then and so on, we can do this in a function iter_f with inputs where is the starting point, and is the number of iterations.
We can try this function with the equation starting at and for iterations.
Taking the difference between the outputs gives an uninteresting answer:
Now, let’s change the equation slightly to , and use the same starting points as before.
The difference between these sequences is:
Multiply the differences by to make the changes more obvious:
This is a little bit surprising, but notice that each number is twice the previous number. It looks like the sequence and could be written as :
Let’s write out several iterations of in terms of the starting point .
The iterate in terms of is
What happens if we make a small change in ? If we change by a small amount, , then
and the difference between the two results after iterations is (the term with is the same for both)
So long as then we can make as big as we like by iterating enough times, no matter how small is. A small change to the initial value of produces as big a change as you like in the final value, .
There you have it. Chaos!
# Moar Chaos
With a slightly more complicated equation, chaos becomes even more interesting. Instead of iterating a linear equation, let’s use the logistic function (see The Growing Gap for an application of logistic functions)
For , this is an inverted parabola that has a maximum at . If you draw the line it intersects the parabola at and . This line is useful for following the iterations or orbits of the function.
Start with a point on the axis, say (point A). Find the value of . This point B is on the parabola above point A.
We want to iterate by using the coordinate of B as the next input. We could calculate and we’ll want to write a Julia function to do that, but it’s also useful to see how the orbit evolves.
Draw a horizontal line until you get to the line at point C. Because C is on the line, then so the coordinates are . The coordinate of C is the coordinate of B, so C becomes the next iterate. From C, draw a vertical line until you intersect the parabola at D which has coordinates .
This process can be repeated as often as you like and will show the trajectory of the function .
# An Iterator for the Logistic Function
Like the iter_f function above, we can write a Julia function to iterate the logistic function. The inputs will be the starting point, , the constant , and the number of iterations with a default value of .
This new function, iter_logistic will return the orbit, .
Let’s try an example with
Now we can plot the trajectory:
Try adjusting the value for . When the plot seems pretty chaotic. What happens if you change from to ? This should show that the trajectory is sensitive to the initial value of .
Next, try and set . Is the plot chaotic? Is it sensitive to initial conditions?
# Autocorrelation and Orbits
While the plot of the logistic function trajectory for appears chaotic, you might be able to convince yourself that there are repeating patterns. The repetitions are far from identical, but if you imagine making a copy of the plot and shifting it over a bit, it might line up.
This is what autocorrelation does. The amount of the shift is controlled by a lag . For each autocorrelation compares it to . The output of the autocorrelation function is a number between and , where
and is the average of all the ’s. If then for every , means there is no correlation, and says that .
A orbit is the plot of on the axis and on the axis.
Let’s start by plotting the autocorrelation of the iterated logistic function for different values of .
Rather than scrolling back up to set and , let’s just create two new variables and call them and .
Choose the autocorrelation step :
The autocorrelation plot seems about as chaotic as the logistic function, but when , the phase portrait is very smooth. In fact, the outline of the blue dots (y1) looks just like the logistic function!
This should be expected since for every .
Why choose ? That choice of plots the orbit of the function. The orange lines (y2) show which point follows the current location. On the left side, the path seems to be mostly upwards towards a nearby point. On the right, points are sent back to the left half except those near .
Try setting and you’ll see that the orbit becomes the line , because there’s perfect correlation between and . The plot doesn’t provide any insight. Try some other values of to see what happens.
With Pluto, you can change the input values to a function to immediately see the effects.