Home > Blockchain >  Why is my for loop not running 5 times for h[i]?
Why is my for loop not running 5 times for h[i]?

Time:12-13

void LAPLACEWCG() {
    int i, j, m, n, cnt;
    double err, rx, ry, ave, a, b, hx, hy, tol, max1,err_metric;
    tol = 0.000000001;
    max1 = 100000000;
    double h[5] = {0.1, 0.05, 0.01, 0.005, 0.001};
    for(int loop = 0; loop < 5; loop  )
    {
    hx = h[loop];
    hy = h[loop];
    printf("hx = %lf\n",hx);
    a = 1;
    b = 1;
    n = (a / hy)   1;
    m = (b / hx)   1;

    double *X = (double *) malloc(m * sizeof(double));
    double *Y = (double *) malloc(n * sizeof(double));

    double **R = (double **) malloc(n * sizeof(double*));
    for (i = 0; i < n; i  )
        R[i] = (double *) malloc(m * sizeof(double));

    double **P = (double **) malloc(n * sizeof(double*));
    for (i = 0; i < n; i  )
        P[i] = (double *) malloc(m * sizeof(double));

    double **AP = (double **) malloc(n * sizeof(double*));
    for (i = 0; i < n; i  )
        AP[i] = (double *) malloc(m * sizeof(double));

    double **U = (double **) malloc(n * sizeof(double*));
    for (i = 0; i < n; i  )
        U[i] = (double *) malloc(m * sizeof(double));

    for (i = 0; i < n; i  ) {
        for (j = 0; j < m; j  ) {
            U[i][j] = 1;
        }
    }


    for (j = 0; j < m; j  ) {
        X[j] = j * hx;
    }


    for (j = 0; j < m; j  ) {

    }

    for (j = 0; j < n; j  ) {
        Y[j] = (b - (j * hy));
    }




    for (i = 0; i < n; i  ) {
        for (j = 0; j < m; j  ) {
            R[i][j] = 0.0;
            P[i][j] = 0.0;
            AP[i][j] = 0.0;
        }
    }

    rx = (1 / (hx * hx));
    ry = (1 / (hy * hy));

    ave = (a * (BDYVAL(1, 0)   BDYVAL(2, 0))   b * (BDYVAL(3, 0)   BDYVAL(4, 0))) / (2 * a   2 * b);

    for (i = 0; i < n; i  ) {
        for (j = 0; j < m; j  ) {
            U[i][j] = ave * U[i][j];
        }
    }



    for (i = 0; i < n; i  ) {
        U[i][0] = BDYVAL(3, Y[i]);
        U[i][m-1] = BDYVAL(4, Y[i]);
    }

    for (j = 0; j < m; j  ) {
        U[0][j] = BDYVAL(1, X[j]);
        U[n-1][j] = BDYVAL(2, X[j]);
    }


    U[0][0] = (U[0][1]   U[1][0]) / 2;
    U[0][m-1] = (U[0][m - 2]   U[1][m-1]) / 2;
    U[n-1][0] = (U[n - 2][0]   U[n-1][1]) / 2;
    U[n-1][m-1] = (U[n - 2][m-1]   U[n-1][m - 2]) / 2;


    for (j = 1; j < m-1; j  ) {
        for (i = 1; i < n-1; i  ) {
            R[i][j] = (rx * U[i][j   1]   rx * U[i][j - 1]   ry * U[i   1][j]   ry * U[i - 1][j]
                       - 2 * (rx   ry) * U[i][j]);
        }
    }

    for (i = 0; i < n; i  ) {
        for (j = 0; j < m; j  ) {
            P[i][j] = R[i][j];
        }
    }

    err = ERROR_METRIC(R, m * n, 3);

    while ((err > tol) && (cnt <= max1)) {
        for (j = 1; j < m-1; j  ) {
            for (i = 1; i < n-1; i  ) {
                if (j == 1) {
                    if (i == 1) {
                        AP[i][j] = -rx * P[i][j   1] - ry * P[i   1][j]   2 * (rx   ry) * P[i][j];

                    } else if (i == n - 2) {
                        AP[i][j] = -rx * P[i][j   1] - ry * P[i - 1][j]   2 * (rx   ry) * P[i][j];
                    } else {
                        AP[i][j] = -rx * P[i][j   1] - ry * P[i   1][j] - ry * P[i - 1][j]   2 * (rx   ry) * P[i][j];
                    }

                } else if (j == m - 2) {
                    if (i == 1) {
                        AP[i][j] = -rx * P[i][j - 1] - ry * P[i   1][j]   2 * (rx   ry) * P[i][j];
                    } else if (i == n - 2) {
                        AP[i][j] = -rx * P[i][j - 1] - ry * P[i - 1][j]   2 * (rx   ry) * P[i][j];
                    } else {
                        AP[i][j] = -rx * P[i][j - 1] - ry * P[i   1][j] - ry * P[i - 1][j]   2 * (rx   ry) * P[i][j];
                    }
                } else if (i == n - 2) {
                    AP[i][j] = -rx * P[i][j   1] - ry * P[i][j - 1] - ry * P[i - 1][j]   2 * (rx   ry) * P[i][j];
                } else if (i == 1) {
                    AP[i][j] = -rx * P[i][j   1] - ry * P[i][j - 1] - ry * P[i   1][j]   2 * (rx   ry) * P[i][j];
                } else {
                    AP[i][j] = -rx * P[i][j   1] - rx * P[i][j - 1] - ry * P[i   1][j] - ry * P[i - 1][j]   2 * (rx   ry) * P[i][j];
                }
            }
        }
        CGUPDATE(U, R, P, AP, n, m);
        err = ERROR_METRIC(R, m * n, 3);
        cnt = cnt   1;
    }

    if (cnt >= max1) {
        printf("Maximum number of iterations exceeded");
    }
     double **E = (double **) malloc(n * sizeof(double*));
    for (i = 0; i < n; i  )
        E[i] = (double *) malloc(m * sizeof(double));

         double **D = (double **) malloc(n * sizeof(double*));
    for (i = 0; i < n; i  )
        D[i] = (double *) malloc(m * sizeof(double));

    for (i = 0; i < n; i  ) {
        for (j = 0; j < m; j  ) {
        E[i][j] = exp(PI*j*hx)*cos((n-1-i) * hy * PI);
        }
    }
     for (i = 0; i < n; i  ) {
        for (j = 0; j < m; j  ) {
        printf("E[%d][%d]: %lf \n", i, j, E[i][j]);
        }
    }


     for (i = 0; i < n; i  ) {
        for (j = 0; j < m; j  ) {
        printf("U[%d][%d]: %lf \n", i, j, U[i][j]);
        }
    }
     for (i = 0; i < n; i  ) {
        for (j = 0; j < m; j  ) {
        D[i][j] = U[i][j] - E[i][j];
        }
    }
    for (i = 0; i < n; i  ) {
        for (j = 0; j < m; j  ) {
        printf("D[%d][%d]: %lf \n", i, j, D[i][j]);
        }
    }
    err_metric = ERROR_METRIC(D,m*n,1);
    printf ("h: %lf error metric: %lf\n",hx,err_metric);
}
}

The for loop for h[5] should be ruuning 5 times but it just executes once.

I need to iterate through the function 5 times with 5 different values of h, is there something else I can do?

It's not giving me any errors.

The rest of the function is working as it should.

I am closing the loop at the end of the function. Should I close it anywhere before that?

I changed the variable inside the for loop, it's still just running once.

CodePudding user response:

Within inner for loops you are changing the variable i declared in the outer for loop

for(int i=0; i<5;i  )
{
    // ...
    for (i = 0; i < n; i  )
    //...

Use some other identifier in the inner for loops.

  • Related