Looking at Chaos
Written by Mike James   
Saturday, 05 September 2009
Article Index
Looking at Chaos
Scrolling to plot progress

 

The only problem with this sort of display is that it doesn’t really show you what the iteration is doing as the bifurcation proceeds. To do this we need to add another picturebox to plot the progress of the iteration for each value of a. Make the picturebox that you add as wide as you can.

To make the plot look like a strip chart or oscilloscope display a Scroll extension method is added to the PictureBox class. See Stripchart Scrolling to find out how this all works. Add the following static class to the program:

public static class MyExtensions
{
[StructLayout(LayoutKind.Sequential)]
public class RECT
{
public Int32 left;
public Int32 top;
public Int32 right;
public Int32 bottom;
}
[DllImport("user32.dll")]
private static extern int
ScrollWindowEx(
System.IntPtr hWnd,
int dx,
int dy,
[MarshalAs(UnmanagedType.LPStruct)]
RECT prcScroll,
[MarshalAs(UnmanagedType.LPStruct)]
RECT prcClip,
System.IntPtr hrgnUpdate,
[MarshalAs(UnmanagedType.LPStruct)]
RECT prcUpdate,
System.UInt32 flags
);
public static void Scroll(
this PictureBox BP, int dx, int dy)
{
ScrollWindowEx(BP.Handle,
dx, dy, null, null,
IntPtr.Zero, null, 2);
}
}

and

using System.Runtime.InteropServices;

The complete new version of the Click event handler is:

 private void button1_Click(
object sender, EventArgs e)
{
Bitmap  BImage1 = new Bitmap(
pictureBox1.ClientSize.Width,
pictureBox1.ClientSize.Height);
pictureBox1.BackgroundImage = BImage1;

Double a;
Graphics G =
pictureBox2.CreateGraphics();
for (int y = 0; y < BImage1.Height; y++)
{
a = 2.95 + (4 - 2.95) *
y / BImage1.Height;
Double x = 0.2;
for(int i = 1;i<500;i++)
{
x = a * x * (1 - x);
}

for ( int i= 1; i < 500; i++)
{
x = a * x * (1 - x);
BImage1.SetPixel((int)(
x * BImage1.Width), y,Color.Green);
int y1 =(int) (x * pictureBox2.Height);
G.DrawLine(Pens.Red, 0, y1, 0, y1 + 1);
pictureBox2.Scroll(1, 0);
Application.DoEvents();
}
pictureBox1.Refresh();
}
G.Dispose();
}
}

This simply plots the data as the bifurcation diagram is plotted. On most machines this works so quickly that it looks like a display on an oscilloscope.

chaos2

Bifurcation diagram with stripchart plot

When you run the program notice the way that the population graph changes as additional stable points enter the bifurcation diagram. Also notice the way that the “signal” changes from periodic to what looks like random as the bifurcation diagram changes to chaos. You will need to watch the entire performance more than once because there is a lot to look at and take in. If you want to zoom in on a vertical section of the bifurcation diagram then change the 2.9 and 4 in the first instruction in the for loop.

If you would like the code for this project then register and click on CodeBin.

<ASIN:0143113453>

<ASIN:0192853783>

<ASIN:0786887214>

 



Last Updated ( Saturday, 05 September 2009 )