//This is a code for solving the Poisson equation with //homogeneous boundary conditions // A subroutine called proc_cg.c needs to be used (a CG code) // A description of this code can be found in a PDF file (in Chinese) // titled multigrid.pdf #include "stdio.h" #include "stdlib.h" #include "math.h" #include "time.h" #define cengshu 7 //最细的网格的是第cengshu 层 #define N 128 //最细的网格剖分是N*N #define minN 16 //最粗的网格剖分是minN*minN double newz[(N+1)*(N+1)],newg[(N+1)*(N+1)]; double error[(N+1)*(N+1)],exact[(N+1)*(N+1)]; #include "proc_cg.c" void main() { int i,j; double h,maxerror; double sum; int k,cishu; clock_t st,ct; extern void cg(); void gauss(); void limit(); void extend(); void MG(); h=1.0/N; j=0; for(i=0;i<(N+1)*(N+1);i++) {newz[i]=0; if((i%(N+1))==0&&i!=0) j++; newg[i]=-2*(j*h)*(j*h-1)-2*(i%(N+1)*h)*(i%(N+1)*h-1); //给出右端项 } cishu=0; st=clock(); do {sum=0; MG(cengshu,newz,newg); //一次v-cyle for(i=1;i1.0e-10); ct=clock(); printf("%e\n",(double) (ct-st) / CLOCKS_PER_SEC); j=0; for(i=0;i<(N+1)*(N+1);i++) {if((i%(N+1))==0&&i!=0) j++; exact[i]=(i%(N+1)*h)*(j*h)*(j*h-1)*(i%(N+1)*h-1); error[i]=fabs(exact[i]-newz[i]); } maxerror=0; for(i=0;i<(N+1)*(N+1);i++) {if(maxerror