next up previous contents
Next: lorenz-ng.c: For solving and Up: Lorenz Attractor Source Code Previous: music2.c: For producing chaotic

lorenz.c: For solving and graphing the Lorenz attractor

This was my first attempt at exploring the Lorenz attractor. It simply plots the familiar x-z plane image of the attractor, along with each of the three variables on time axes.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <vga.h>

#define TIMESTEP 0.0001
#define VIDMODE  G640x480x16
#define XSCALE   4
#define YSCALE   4
#define ZSCALE   4
#define TSCALE   20

void main()
{
    double x1, y1, z1, x, y, z, t, dt;
    int   xp, yp, tp=0, gxp, gyp, gzp;
   
    FILE  *file;

    file=fopen("output", "w");
   
    x1=(y1=(z1=(t=0.0001)));
    dt=TIMESTEP;

    vga_setmode(VIDMODE);
    vga_setcolor(1);
    vga_drawline(320,0,320,479);
    vga_drawline(0,240,639,240);
    vga_drawline(0,300,639,300);
    vga_drawline(0,380,639,380);
    vga_drawline(0,460,639,460);
    vga_setcolor(15);

    while(tp<639)
    {
        x=x1 + ( -10.0*x1  + 10.0*y1    ) * dt;
        y=y1 + ( 28.0*x1   - y1 - x1*z1 ) * dt;
        z=z1 + ( -(8/3)*z1 + x1*y1      ) * dt;
        t=t  +                              dt;

        xp=320+(int)floor(XSCALE*x);
        yp=240-(int)floor(YSCALE*z);
        tp=(int)floor(TSCALE*t);
        if (tp>639)
          {
            tp=479;
            /*      t=0.0;*/
          }
        
        gxp=300-(int)floor(XSCALE*x/4);
        gyp=380-(int)floor(YSCALE*y/4);
        gzp=460-(int)floor(ZSCALE*z/4);

        /*      vga_setcolor(0);
        vga_drawline(tp, 241, tp, 479);

        vga_setcolor(1);
        vga_drawpixel(tp,300);
        vga_drawpixel(tp,380);
        vga_drawpixel(tp,460);
        if (tp==320) vga_drawline(320,0,320,479);*/

        vga_setcolor(15);
        vga_drawpixel(xp,yp);
        vga_drawpixel(tp,gxp);
        vga_drawpixel(tp,gyp);
        vga_drawpixel(tp,gzp);

        x1=x;
        y1=y;
        z1=z;
/*      fprintf(file, "%f %f %f %f\n", t, x, y, z);*/
    }
    getchar();
    fclose(file);
}



Mike Andrews
Wed Oct 23 01:18:29 EDT 1996