Taylor series/Code/ExampleZ

From Citizendium
Jump to navigation Jump to search

// Code for the right hand side part of the figure TaylorExampleZ.jpg

// For compillation, this code needs also the graphical rootines
//  ContourPlot/code/ado.cin     (function that makes header of the eps file)
//  ContourPlot/code/conto.cin  (function which draws a level)


#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define DB double
#define DO(x,y) for(x=0;x<y;x++)
#include <complex.h>
#define z_type complex<double>
#define Re(x) x.real()
#define Im(x) x.imag()
#define I z_type(0.,1.)
z_type F4(z_type z1) { int K=66,k;
			z_type z=1.-z1;
			z_type s=0.;
			z_type t=1; 
			for(k=0;k<=21;k++) { s+=t; t*=z; }
			return s;

		}
#include "conto.cin"
main(){ int j,k,m,n; DB x,y, p,q, t; z_type z,c,d, cu,cd;
int M=250,M1=M+1;
int N=200,N1=N+1;
DB X[M1],Y[N1], g[M1*N1],f[M1*N1], w[M1*N1]; // w is working array.
char v[M1*N1]; // v is working array
FILE *o;o=fopen("TaylorExampleZ.eps","w");ado(o,0,0,504,504);
fprintf(o,"202 202 translate\n 100 100 scale\n");
DO(m,M1) X[m]=-2+.02*m;
DO(n,N1) Y[n]=-2+.02*n;
for(m=-2;m<3;m++) {	if(m==0){M(m,-2.1)L(m,2.1)}
			else	{M(m,-2)L(m,2)}	}
for(n=-2;n<3;n++) {M(  -2,n)L(2,n)} fprintf(o,".002 W 0 0 0 RGB S\n");
z_type F[M1*N1];
DO(m,M1)DO(n,N1){	g[m*N1+n]=9999;
			f[m*N1+n]=9999;	}
for(m=0;m<M1;m++){x=X[m];
		  DO(n,N1)	{	y=Y[n]; z=z_type(x,y);
			   		c=F4natu(z); F[m*N1+n]=c;
				}
		}
DO(m,M1)
DO(n,N1){	c=(F[m*N1+n]); p=Re(c); q=Im(c);
		if(p>-999 && p<999) g[m*N1+n]=p;
		if(q>-999 && q<999) f[m*N1+n]=q;
	}
p=4;q=4;
                  conto(o,f,w,v,X,Y,M,N, ( -3     ),-q,q); fprintf(o,".004 W 1 0 0 RGB S\n");
                  conto(o,f,w,v,X,Y,M,N, (-2.     ),-q,q); fprintf(o,".004 W 1 0 0 RGB S\n");
for(n=2;n<10;n+=2)conto(o,f,w,v,X,Y,M,N, (-2.+.1*n),-p,p); fprintf(o,".002 W 0 1 0 RGB S\n");
                  conto(o,f,w,v,X,Y,M,N, (-1.     ),-q,q); fprintf(o,".004 W 1 0 0 RGB S\n");
for(n=2;n<10;n+=2)conto(o,f,w,v,X,Y,M,N, (-1.+.1*n),-p,p); fprintf(o,".002 W 0 1 0 RGB S\n");
                  conto(o,f,w,v,X,Y,M,N, (0.      ),-q,q); fprintf(o,".004 W 0 0 0 RGB S\n");
for(n=2;n<10;n+=2)conto(o,f,w,v,X,Y,M,N, (    .1*n),-p,p); fprintf(o,".002 W 0 1 0 RGB S\n");
                  conto(o,f,w,v,X,Y,M,N, ( 1.     ),-q,q); fprintf(o,".004 W 0 0 1 RGB S\n");
for(n=2;n<10;n+=2)conto(o,f,w,v,X,Y,M,N, ( 1.+.1*n),-p,p); fprintf(o,".002 W 0 1 0 RGB S\n");
                  conto(o,f,w,v,X,Y,M,N, ( 2.     ),-q,q); fprintf(o,".004 W 0 0 1 RGB S\n");
                  conto(o,f,w,v,X,Y,M,N, ( 3      ),-q,q); fprintf(o,".004 W 0 0 1 RGB S\n");
                  conto(o,g,w,v,X,Y,M,N, (-3.     ),-p,p); fprintf(o,".004 W 0 0 0 RGB S\n");
                  conto(o,g,w,v,X,Y,M,N, (-2.     ),-p,p); fprintf(o,".004 W 0 0 0 RGB S\n");
for(n=2;n<10;n+=2)conto(o,g,w,v,X,Y,M,N, (-2.+.1*n),-p,p); fprintf(o,".002 W 1 0 0 RGB S\n");
                  conto(o,g,w,v,X,Y,M,N, (-1.     ),-q,q); fprintf(o,".004 W 0 0 0 RGB S\n");
for(n=2;n<10;n+=2)conto(o,g,w,v,X,Y,M,N, (-1.+.1*n),-p,p); fprintf(o,".002 W 1 0 0 RGB S\n");
                  conto(o,g,w,v,X,Y,M,N, (0.      ),-q,q); fprintf(o,".004 W 0 0 0 RGB S\n");
for(n=2;n<10;n+=2)conto(o,g,w,v,X,Y,M,N, (    .1*n),-p,p); fprintf(o,".002 W 0 0 1 RGB S\n");
                  conto(o,g,w,v,X,Y,M,N, ( 1.     ),-q,q); fprintf(o,".004 W 0 0 0 RGB S\n");
for(n=2;n<10;n+=2)conto(o,g,w,v,X,Y,M,N, ( 1.+.1*n),-p,p); fprintf(o,".002 W 0 0 1 RGB S\n");
                  conto(o,g,w,v,X,Y,M,N, ( 2.     ),-p,p); fprintf(o,".004 W 0 0 0 RGB S\n");
                  conto(o,g,w,v,X,Y,M,N, ( 3.     ),-p,p); fprintf(o,".004 W 0 0 0 RGB S\n");
fprintf(o,"showpage\n\%\%\%Trailer"); fclose(o);
//	  system( "ggv TaylorExampleZ.eps &");	//for linux
	  system("open TaylorExampleZ.eps");	//for macintosh
	system("ps2pdf TaylorExampleZ.eps");
	getchar(); system("killall Preview");	//for macintosh
}