#include "stdio.h" #include "stdlib.h" #include "math.h" #include "paulslib.h" #include "bitmaplib.h" int MAXITERATIONS = 100000; int NEXAMPLES = 10000; double *x,*y; void SaveAttractor(int,double *,double *,double,double,double,double); int main(int argc,char **argv) { double ax[6],ay[6]; int i,n; int drawit; long secs; double xmin=1e32,xmax=-1e32,ymin=1e32,ymax=-1e32; double d0,dd,dx,dy,lyapunov; double xe,ye,xenew,yenew; x = malloc(MAXITERATIONS*sizeof(double)); y = malloc(MAXITERATIONS*sizeof(double)); time(&secs); srand48(secs); for (n=0;n 1e10 || ymax > 1e10) { drawit = FALSE; printf("infinite attractor "); break; } /* Does the series tend to a point */ dx = x[i] - x[i-1]; dy = y[i] - y[i-1]; if (ABS(dx) < 1e-10 && ABS(dy) < 1e-10) { drawit = FALSE; printf("point attractor "); break; } /* Calculate the lyapunov exponents */ if (i > 1000) { dx = x[i] - xenew; dy = y[i] - yenew; dd = sqrt(dx * dx + dy * dy); lyapunov += log(fabs(dd / d0)); xe = x[i] + d0 * dx / dd; ye = y[i] + d0 * dy / dd; } } /* Classify the series according to lyapunov */ if (drawit) { if (ABS(lyapunov) < 10) { printf("neutrally stable "); drawit = FALSE; } else if (lyapunov < 0) { printf("periodic %g ",lyapunov); drawit = FALSE; } else { printf("chaotic %g ",lyapunov); } } /* Save the image */ if (drawit) SaveAttractor(n,ax,ay,xmin,xmax,ymin,ymax); printf("\n"); } exit(0); } void SaveAttractor(int n,double *a,double *b, double xmin,double xmax,double ymin,double ymax) { char fname[128]; FILE *fptr; int i,ix,iy; static int first = TRUE; BITMAP4 *image = NULL,white = {255,255,255},black = {0,0,0}; int width = 500, height = 500; /* Save the parameters */ sprintf(fname,"%05d.txt",n); if ((fptr = fopen(fname,"w")) == NULL) { fprintf(stderr,"Failed to open output file\n"); return; } fprintf(fptr,"%g %g %g %g\n",xmin,ymin,xmax,ymax); for (i=0;i<6;i++) fprintf(fptr,"%g %g\n",a[i],b[i]); fclose(fptr); /* Save the image */ sprintf(fname,"%05d.tga",n); if (first) { image = Create_Bitmap(width,height); first = TRUE; } Erase_Bitmap(image,width,height,white); if ((fptr = fopen(fname,"w")) == NULL) { fprintf(stderr,"Failed to open output file\n"); return; } for (i=0;i 100) Draw_Pixel(image,width,height,ix,iy,black); } Write_Bitmap(fptr,image,width,height,1); fclose(fptr); }