执行多个for循环时出错

Error in implementation of multiple for loops

在这个程序中,我必须使用 Newton-Raphson 方法找到函数的根。对于 r 的每个值,我需要找到 Rf0,然后找到根。

之后我想增加 r 的值并再次找到根直到 r<=10。我能够找到 "r=1" 初始值的根,但我无法增加 r 的值来找到 r 直到 10 的其他值的根。循环在第一步后退出。

#include<stdio.h>
#include<math.h>
#define w 0.10655
#define z -9.208
#define allerr 0.000001
#define maxiter 100

float f(float f,float r,float R)
{
    return w*f-z*(r-1)+r*log(pow(r,1.5)*(r-f*f));
}
float df (float f,float r,float R)
{
    return w-(2*r*f/(r-f*f));
}
int main()
{
    int itr;
    float h, f0, f1,r,R;

    for(r=1; r<=10; r+=0.1)
    {
        R=pow(r,-2.5)*exp(z*((r-1)/r));
        f0=w*R+(sqrt(w*w*R*R+(2*r*(r-R))*(2*r+R*w*w/r)))/((2*r+R*w*w)/r);
        for (itr=1; itr<=maxiter; itr++)
        {
            h=f(f0,r,R)/df(f0,r,R);
            x1=f0-h;
            printf(" At Iteration no. %3d, x = %9.6f\n", itr, x1);
            if (fabs(h) < allerr)
            {
                printf("After %3d iterations, root = %8.6f\n", itr, x1);
                return 0;
            }
            f0=x1;
        }

        printf(" The required solution does not converge or iterations are insufficient\n");
        return 1;
    }

先打扫卫生。

1) 您发布的代码缺少右括号。未定义变量 x1

2) 没有理由使用 float 变量。使用 double 以获得更好的精度。

3) 要很好地退出内部循环,请不要使用 return,只需使用 break 将其中断即可。控制权将交给外循环。也不要在循环外使用 return

4) 区分通过break(找到解决方案时)离开内循环和达到最大迭代次数的情况。 (这可以通过 sfound 标志来完成。)

5) 无需在打印输出中添加大量中间步骤。

6) 改进打印输出以包含 r 值。

7) 注意:您的数值 Newton-Raphson 方法对于您的函数来说不稳定。

下面的代码解决了报告的不执行 r 循环的问题。

#include<stdio.h>
#include<math.h>

#define w            0.10655
#define z             -9.208
#define allerr      0.000001
#define maxiter         1000

double f(double f, double r, double R)
{
    return w*f-z*(r-1)+r*log(pow(r,1.5)*(r-f*f));
}

double df (double f, double r, double R)
{
    return w-(2.0*r*f/(r-f*f));
}

int main(void)
{
    int itr;
    double h, f0, f1, r, R, x1;
    int sfound = 0;

    for(r=1.0; r<=10.0; r+=0.1)
    {
        sfound = 0;  

        R = pow(r,-2.5)*exp(z*((r-1)/r));

        f0 = w*R+(sqrt(w*w*R*R+(2*r*(r-R))*(2*r+R*w*w/r)))/((2*r+R*w*w)/r);

        for (itr=1; itr <= maxiter; itr++)
        {
            h = f(f0,r,R)/df(f0,r,R);

            x1 = f0 - h;

            // printf(" r=%.2f at Iteration no. %3d, x = %9.6f\n", r, itr, x1);

            if (fabs(h) < allerr)
            {
                printf("For r=%.2f after %3d iterations, root = %8.6f\n", r,  itr, x1);
                sfound = 1;
                break; 
            }

            f0 = x1;
        }
        if(sfound == 0)
            printf("For r=%.2f the required solution does not converge or iterations are insufficient\n",r);
    }

    return 0;
}

输出:

For r=1.00 after   5 iterations, root = 0.105951                                                                                               
For r=1.10 after   5 iterations, root = 0.868910                                                                                               
For r=1.20 the required solution does not converge or iterations are insufficient                                                              
For r=1.30 the required solution does not converge or iterations are insufficient                                                              
For r=1.40 the required solution does not converge or iterations are insufficient                                                              
For r=1.50 the required solution does not converge or iterations are insufficient                                                              
For r=1.60 the required solution does not converge or iterations are insufficient                                                              
For r=1.70 the required solution does not converge or iterations are insufficient                                                              
For r=1.80 the required solution does not converge or iterations are insufficient                                                              
For r=1.90 the required solution does not converge or iterations are insufficient                                                              
For r=2.00 the required solution does not converge or iterations are insufficient                                                              
For r=2.10 the required solution does not converge or iterations are insufficient                                                              
For r=2.20 the required solution does not converge or iterations are insufficient                                                              
For r=2.30 the required solution does not converge or iterations are insufficient                                                              
For r=2.40 the required solution does not converge or iterations are insufficient                                                              
For r=2.50 the required solution does not converge or iterations are insufficient                                                              
For r=2.60 the required solution does not converge or iterations are insufficient                                                              
For r=2.70 the required solution does not converge or iterations are insufficient                                                              
For r=2.80 the required solution does not converge or iterations are insufficient                                                              
For r=2.90 the required solution does not converge or iterations are insufficient                                                              
For r=3.00 the required solution does not converge or iterations are insufficient                                                              
For r=3.10 the required solution does not converge or iterations are insufficient                                                              
For r=3.20 the required solution does not converge or iterations are insufficient                                                              
For r=3.30 the required solution does not converge or iterations are insufficient                                                              
For r=3.40 the required solution does not converge or iterations are insufficient                                                              
For r=3.50 the required solution does not converge or iterations are insufficient                                                              
For r=3.60 the required solution does not converge or iterations are insufficient                                                              
For r=3.70 the required solution does not converge or iterations are insufficient                                                              
For r=3.80 the required solution does not converge or iterations are insufficient                                                              
For r=3.90 the required solution does not converge or iterations are insufficient                                                              
For r=4.00 the required solution does not converge or iterations are insufficient                                                              
For r=4.10 the required solution does not converge or iterations are insufficient                                                              
For r=4.20 the required solution does not converge or iterations are insufficient                                                              
For r=4.30 the required solution does not converge or iterations are insufficient                                                              
For r=4.40 the required solution does not converge or iterations are insufficient                                                              
For r=4.50 the required solution does not converge or iterations are insufficient                                                              
For r=4.60 the required solution does not converge or iterations are insufficient                                                              
For r=4.70 the required solution does not converge or iterations are insufficient                                                              
For r=4.80 the required solution does not converge or iterations are insufficient                                                              
For r=4.90 the required solution does not converge or iterations are insufficient                                                              
For r=5.00 the required solution does not converge or iterations are insufficient                                                              
For r=5.10 the required solution does not converge or iterations are insufficient                                                              
For r=5.20 the required solution does not converge or iterations are insufficient                                                              
For r=5.30 the required solution does not converge or iterations are insufficient                                                              
For r=5.40 the required solution does not converge or iterations are insufficient                                                              
For r=5.50 the required solution does not converge or iterations are insufficient                                                              
For r=5.60 the required solution does not converge or iterations are insufficient                                                              
For r=5.70 the required solution does not converge or iterations are insufficient                                                              
For r=5.80 the required solution does not converge or iterations are insufficient                                                              
For r=5.90 the required solution does not converge or iterations are insufficient                                                              
For r=6.00 the required solution does not converge or iterations are insufficient                                                              
For r=6.10 the required solution does not converge or iterations are insufficient                                                              
For r=6.20 the required solution does not converge or iterations are insufficient                                                              
For r=6.30 the required solution does not converge or iterations are insufficient                                                              
For r=6.40 the required solution does not converge or iterations are insufficient                                                              
For r=6.50 the required solution does not converge or iterations are insufficient                                                              
For r=6.60 the required solution does not converge or iterations are insufficient                                                              
For r=6.70 the required solution does not converge or iterations are insufficient                                                              
For r=6.80 the required solution does not converge or iterations are insufficient                                                              
For r=6.90 the required solution does not converge or iterations are insufficient                                                              
For r=7.00 the required solution does not converge or iterations are insufficient                                                              
For r=7.10 the required solution does not converge or iterations are insufficient                                                              
For r=7.20 the required solution does not converge or iterations are insufficient                                                              
For r=7.30 the required solution does not converge or iterations are insufficient   
For r=7.40 the required solution does not converge or iterations are insufficient                                                              
For r=7.50 the required solution does not converge or iterations are insufficient                                                              
For r=7.60 the required solution does not converge or iterations are insufficient                                                              
For r=7.70 the required solution does not converge or iterations are insufficient                                                              
For r=7.80 the required solution does not converge or iterations are insufficient                                                              
For r=7.90 the required solution does not converge or iterations are insufficient                                                              
For r=8.00 the required solution does not converge or iterations are insufficient                                                              
For r=8.10 the required solution does not converge or iterations are insufficient                                                              
For r=8.20 the required solution does not converge or iterations are insufficient                                                              
For r=8.30 the required solution does not converge or iterations are insufficient                                                              
For r=8.40 the required solution does not converge or iterations are insufficient                                                              
For r=8.50 the required solution does not converge or iterations are insufficient                                                              
For r=8.60 the required solution does not converge or iterations are insufficient                                                              
For r=8.70 the required solution does not converge or iterations are insufficient                                                              
For r=8.80 the required solution does not converge or iterations are insufficient                                                              
For r=8.90 the required solution does not converge or iterations are insufficient                                                              
For r=9.00 the required solution does not converge or iterations are insufficient                                                              
For r=9.10 the required solution does not converge or iterations are insufficient                                                              
For r=9.20 the required solution does not converge or iterations are insufficient                                                              
For r=9.30 the required solution does not converge or iterations are insufficient                                                              
For r=9.40 the required solution does not converge or iterations are insufficient                                                              
For r=9.50 the required solution does not converge or iterations are insufficient                                                              
For r=9.60 the required solution does not converge or iterations are insufficient                                                              
For r=9.70 the required solution does not converge or iterations are insufficient                                                              
For r=9.80 the required solution does not converge or iterations are insufficient                                                              
For r=9.90 the required solution does not converge or iterations are insufficient                                                              
For r=10.00 the required solution does not converge or iterations are insufficient