Iteration Method with C Programming Language

Numerical Analysis Iteration Method.

#include    //for basic I/O statements
#include    //for mathematical statements like fabs

int i=0;
float x_0,x,tol,temp=0;

float g(float x)
{
    float h;

    h=pow(x+1,1.0/3);
        return h;
}

float gp(float x)
{
    float k;
    k=pow(x+1,-2.0/3)/3;
        return k;
}

void main()
{
    printf(”Enter initial approximation:”); //Start with the initial approximation x_0
    scanf(”%f”,x_0);
    printf(”nEnter the tollerance:”);     //Enter a certain number of decimal places as precision strength
    scanf(”%f”,&tol);

        printf(”nnnIteration      Solution”);  //Set a table with appropriate headings

    if(fabs(gp(x_0))<1)                     // if |g’(x)|<1; then the function will converge to a root of f(x)=0
      ret:    i++;
        x=g(x_0);
                printf(”n%d    %7.4f”,i,x); //Display result
        if(fabs(x-temp)
             goto end;         //I don’t encourage anyone to use goto statements
        else                   //use if you have to, or have no better ideas at the moment
            {
            temp=x;        //Prepare the temporary variable for re-testing
            x_0=x;         //Assign the new value as the new approximation and continue
                        goto ret;
            }
end:
}

Enter initial approximation: 1.3

Enter the tollerance: 0.01

Iteration    Solution

1                1.0000

2                1.2599

3                1.3123

4                1.3224

5                1.3243

comments powered by Disqus
Loading