Search code examples
coverflownumerical-methodsnewtons-method

Error in implementation of multiple for loops


In this program I have to find the root of the function using Newton-Raphson method. For every value of r I need to find R, f0 then I find the root.

After which I want to increment the value of r and find the root again till r<=10. I am able to find the root for initial value of "r=1" but I am not able to increment value of r to find roots for other values of r till 10. The loop is getting exited after first step.

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

Solution

  • A housekeeping first.

    1) Your posted code is missing closing bracket. The variable x1 is not defined.

    2) There is no reason to use float variables. Use double for better precision.

    3) To exit inner loop nicely do not use return just break it with break. The control will be given to the outer loop. Do not use return outside the loop either.

    4) Distinguish leaving the inner loop via break (when solution was found) from the case where the maximum number of iterations was reached. (This can be done via sfound flag.)

    5) No need to flood the printout with intermediate steps.

    6) Improve the printout to include r value.

    7) A note: your numerical Newton-Raphson method is not stable for your functions.

    The code below solves the reported problem of not executing the r loop.

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

    Output:

    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