为什么我的for循环不是运行 5次h[i]?

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

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);
}
}

h[5] 的 for 循环应该运行 5 次,但它只执行了一次。

我需要使用 5 个不同的 h 值迭代函数 5 次,我还能做些什么吗?

它没有给我任何错误。

函数的其余部分正常工作。

我在函数结束时关闭循环。我应该在那之前的任何地方关闭它吗?

我更改了for循环中的变量,它仍然只是运行一次。

在内部 for 循环中,您正在更改在外部 for 循环中声明的变量 i

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

在内部 for 循环中使用一些其他标识符。