/* relax.c Simple-minded solution of Laplace's equation using relaxation method, with overrelaxation w. The program is simple rather than elegant, and has no error checking at all. It should work with nearly any C compiler, and probably even with most C++ compilers. Output is written into a file called TEMP.$$$, with no formatting except for a carriage return at the end of each row. To write output to console, just replace the "fprintf(wfile,..." statements with "printf(...". Some additional formatting would be nice if the output is to be read by humans, rather than spreadsheets or a graphics program. Last modified 10/06/09 by E**3. */ #include #include #define VSIZE 20 #define HSIZE 20 float phi[VSIZE][HSIZE]; /* Could be double if more precision needed */ main() { double vtop,vbot,vleft,vright,maxdiff,newval,w,tol; int i,j,last_j,iter; FILE *wfile; printf("Enter V(top): "); scanf("%lf",&vtop); printf("Enter V(bottom): "); scanf("%lf",&vbot); printf("Enter V(left): "); scanf("%lf",&vleft); printf("Enter V(right): "); scanf("%lf",&vright); printf("Enter tolerance: "); scanf("%lf",&tol); printf("Enter overrelaxation (1-2): "); scanf("%lf",&w); /* Set the entire square array to zero initially */ for (i=0; itol; iter++) { /* Do an iteration. Be careful not to overwrite the boundaries! */ maxdiff=0; for (i=1; i maxdiff) // Adjust maximum difference if needed maxdiff = fabs(newval-phi[i][j]); phi[i][j] = newval; } } } /*Print the results */ printf("Maximum difference is %le\n",maxdiff); printf("Number of iterations is %d\n",iter); printf("Printing output to file \"temp.$$$\"\n"); wfile = fopen("temp.$$$","w"); for (i=0; i