C program of Newton Raphson Method

C program of Newton Raphson Method



#include
#include
#include
#include

int bigval,i=0,cnt=0,flag=0;
int coef[10]={0};
float x1=0,x2=0,t=0;
float fx1=0,fdx1=0;

int main()
{

   
    printf("\n\n\t C PROGRAM FOR THE NEWTON RAPHSON METHOD");
    printf("\n\n\n\t PLEASE ENTER THE MAXIMUM POWER OF X = ");
    scanf("%d",&bigval);

    for(i=0;i<=bigval;i++)
    {
        printf("\n\t x^%d = ",i);
        scanf("%d",&coef[i]);
    }

    printf("\n");

    printf("\n\t So, THE POLYNOMIAL IS "= ");
    for(i=bigval;i>=0;i--)/*printing coefficients*/
    {
        printf(" %dx^%d",coef[i],i);
    }

    printf("\n\n\tFirst approximation x1 ----> ");
    scanf("%f",&x1);

 
     printf("\n ITERATION \t x1 \t F(x1) \t \tF'(x1)  ");


    do
    {
            cnt++;
            fx1=fdx1=0;
            for(i=bigval;i>=1;i--)
            {
                fx1+=coef[i] * (pow(x1,i)) ;
            }
            fx1+=coef[0];
            for(i=bigval;i>=0;i--)
            {
                fdx1+=coef[i]* (i*pow(x1,(i-1)));
            }
            t=x2;
            x2=(x1-(fx1/fdx1));

            x1=x2;

            printf("\n\t %d \t%.3f \t %.3f\t\t%.3f ",cnt,x2,fx1,fdx1);

    }
 while((fabs(t - x1))>=0.0001);
    printf("\n\n\n\t AND.... THE ROOT OF EQUATION IS = %f",x2);
    getch();
}



No comments:

Post a Comment