## Visualising logistic map chaos

The other day, I had a fascinating conversation with my friend and colleague, Paul Tobin. I had been thinking about using some kind of chaotic oscillator in a music synthesizer toy I’ve been playing around with and, since Paul is a gifted musician with a long-standing academic interest in chaotic systems, I was picking his brain about chaos. In about 30 seconds flat, he was able to jot down the bare essentials of a really simple system I could try out myself – the logistic map (you can read more about it here).

Here’s the basic formula:

$x_{n+1} = r x_n \left( 1 - x_n \right)$

where:

• r is a positive number
• For r<4, x_n remains in the interval (0,1) but jumps around with each iteration of the formula – the pattern of jumps depends on the specific value of r.

The basic idea is that you pick and r value, then iterate the formula many times to crank out a sequence of x_n values. Then you repeat the process for many values of r.

The code below generates a graph of the logistic map over a range of r values.

//
// Logistic map chaos graph program
// written by Ted Burke - 13-12-2012
//

#include <stdio.h>

// Allocate a pixel array to store generated image
int p[800][800];

int main()
{
// Algorithm parameters
int w=640, h=480;
int m, n, M=10000;
double r, r_min=3, r_max=4;
double x, x0=0.5;

// Set every pixel to zero initially
printf("Blanking canvas\n");
for (n=0 ; n<h ; ++n) for (m=0 ; m<w ; ++m) p[n][m] = 0;

// Calculate points and add to image.
// Each new point inrements a single pixel's value.
// Pixels saturate at 255.
printf("Calculating and plotting points\n");
for (n=0 ; n<w ; ++n)
{
// Calculate points for current r value
r = r_min + (n/(double)w)*(r_max - r_min);
printf("r = %lf\n", r);
x = x0;
for (m=0 ; m<M ; ++m)
{
x = r * x * (1 - x);
if (p[(int)(h*x)][n] < 255) p[(int)(h*x)][n]++;
}
}

// Write image to PGM file
printf("Writing PGM file\n");
FILE *f = fopen("graph.pgm", "w");
fprintf(f, "P2\n# comment\n%d %d\n255\n", w, h);
for (n=0 ; n<h ; ++n)
{
for (m=0 ; m<w ; ++m) fprintf(f, "%d ", p[n][m]);
fprintf(f, "\n");
}
fclose(f);

return 0;
}


The above code is not very pretty, but it actually produces a half-decent (certainly recognisable) graph of the chaotic behaviour of the logistic map. The horizontal axis is the r axis. The vertical axis is the x_n axis. Basically, the program above generates a sequence of 10,000 x_n values for each r value. These 10,000 values are used to generate a single column of pixels in the image.

I tried it first over the range 0 <= r <= 4 and got the following:

The vertical axis limits are 0 < x_n < 1. The horizontal axis limits for the graph above are 0 <= r < 4.

I then focused the graph in on the heart of the action by constraining r to the range 3 <= r <= 4. Here's what I got:

This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.

### 5 Responses to Visualising logistic map chaos

1. biosp says:

How was the result of the music synthesizer? Have you posted about it? Did you try some other chaotic maps as well/ Couldn’t find…
Anyway nice post. I’ve been trying to create the same bifurcation diagram as you did above but couldn’t show the fine details you have shown in the chaotic are (3.47-4) I got just a fully covered area. Did it in matlab though.
Anyway, you are now featuring in my blog🙂

• batchloaf says:

Hi biosp,

Ha ha, thanks for asking – I’m afraid I didn’t produce anything very musical yet with my chaotic oscillators, but I’m still thinking about it!

By the way, is it possible that you’re getting a solidly filled in area in MATLAB because it’s using lines to join the dots in your graph?

Ted

• batchloaf says:

Sorry, I just read your post and I see what you mean – it’s not completely solid, just missing some of the finer detail. Each time I add a data point to my image, it increments the value of whatever pixel it is by one, so if the same pixel gets “hit” a lot of times, it gets brighter and brighter. In MATLAB, once a marker falls on a particular spot, I guess it just gets coloured in and thereafter it makes no difference if it gets hit again.

According to this page on the Mathworks web site, you could use the scatter command instead of plot. That way, I think you could set the alpha (transparency) of the data points so that the more data points fall on the same spot, the darker it would get.

In case, the above link isn’t visible:

Ted

• biosp says:

Hey,
Thanks for your time spent for this long comment! This was a very intriguing suggestion with the scatter function. So intriguing I found myself trying it at 4AM in the morning (ahm ahm…). Unfortunately no luck so far – will try this again when not falling asleep. Will report!