/* solving discrete poisson equation with Jacobi iteration */ #include #include #include #include #define nx 32 #define ny 32 int main() { int i, j, iter, itmax=1000; double a=1.0, b=1.0, tol=1.0e-6; double hx, hy, d, dx, dy, xi, yj, res, rtmp, err, tc; double (*u)[ny+1], (*ut)[ny+1], (*unew)[ny+1], (*f)[ny+1]; u = malloc((nx+1)*(ny+1)*sizeof(double)); ut = malloc((nx+1)*(ny+1)*sizeof(double)); unew = malloc((nx+1)*(ny+1)*sizeof(double)); f = malloc((nx+1)*(ny+1)*sizeof(double)); if (!u | !ut | !unew | !f) { printf("Out of memory!\n"); exit(1); } // initial data hx = a / nx; hy = b / ny; d = hx*hx * hy*hy / ( 2*(hx*hx + hy*hy) ); dx = d / (hx*hx); dy = d / (hy*hy); // true solution : u = - (x^2 + y^2)/4 for (i=0; i<=nx; i++) { for (j=0; j<=ny; j++) { xi = hx * i; yj = hy * j; ut[i][j] = -0.25*(xi*xi + yj*yj); } } // set initial guess and right hand side for (i=1; i