/* julia -- compute basin of attraction for Newton's method Written September 19, 2012 for Math 701 by Eric Olson */ #include #include #include #include #include #define FE_BAD (FE_INVALID|FE_DIVBYZERO|FE_OVERFLOW|FE_UNDERFLOW) #define RES 512 #define ITER 10 #define IMAX 100 static double xmin=-1.0, xmax=1.0; static double ymin=-1.0, ymax=1.0; inline complex f(complex z){ return z*z*z*z*z+1; } inline complex df(complex z){ return 5*z*z*z*z; } complex phi(complex z){ complex zdelta=f(z)/df(z); if(fetestexcept(FE_BAD)){ feclearexcept(FE_BAD); return z; } return z-zdelta; } complex newton(complex z){ complex w[IMAX+1]; w[0]=z; int i,j; for(i=1,j=0;i<=IMAX;i++,j+=i%2){ w[i]=phi(w[i-1]); if(w[i]==w[j]) return w[i]; } return w[IMAX]; } static int M,N; void writejulia(int n, complex *z){ char fname[64]; sprintf(fname,"julia-%03d.ppm",n); ztoppm6(z,M,N,fname); printf("Wrote Newton iterate %d to %s.\n",n,fname); } int main(){ printf("julia -- compute basin of attraction for Newton's method\n" "Written September 19, 2012 for Math 701 by Eric Olson\n\n"); double xdelta=xmax-xmin; double ydelta=ymax-ymin; if(xdelta>ydelta){ M=RES; N=RES*ydelta/xdelta+0.5; } else { N=RES; M=RES*xdelta/ydelta+0.5; } complex *z=malloc(M*N*sizeof(complex)); if(!z){ fprintf(stderr,"Out of memory: RES=%d too large!\n",RES); exit(1); } xdelta/=M-1; ydelta/=N-1; printf("Working on a %d x %d grid.\n\n",M,N); int m,n; for(n=0;n=ITER) break; for(i=0;i