为啥我的 for 循环没有为 h[i] 运行 5 次?

Posted

技术标签:

【中文标题】为啥我的 for 循环没有为 h[i] 运行 5 次?【英文标题】:Why is my for loop not running 5 times for h[i]?为什么我的 for 循环没有为 h[i] 运行 5 次? 【发布时间】:2022-01-16 12:42:18 【问题描述】:
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 循环中更改了变量,它仍然只运行一次。

【问题讨论】:

【参考方案1】:

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

for(int i=0; i<5;i++)

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

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

【讨论】:

以上是关于为啥我的 for 循环没有为 h[i] 运行 5 次?的主要内容,如果未能解决你的问题,请参考以下文章

为啥我的 for 循环在“继续”之后没有重新评估条件?

为啥这个for循环只输出一次?

通过for循环将值附加到向量不起作用

为啥在下面的 for 循环中没有任何条件检查?

FOR循环中的上界在循环中没有改变,为啥?

for循环是根据啥来判断循环次数的