Search & Find out

Thursday, December 3, 2009

Newton Raphson Method

#include "stdio.h"
#include "conio.h"
#include "math.h"


void main()
{
int power,i,m,l,n,n1;
double c[100],x1,fx1,x2,fx2,x,n2;
clrscr();

printf("Welcome to Newton Raphson Method\n\n");
printf("Enter the highest power  ");
scanf("%d",&power);

       for(i=power;i>=0;i--)
       {
       if(i==0)
    {
     printf("Enter the constant term  ");

    }
    else
    {
    printf("Enter the coefficient  of the %d term  ",i);
    }
    scanf("%lf",&c[i]);
       }

       printf("\n\n\tyour equation is\n\n");

    for(i=power;i>=0;i--)
     {
       if(i==0)
       {
    printf("(%lf)  = 0\n",c[i]);
       }
       else
       {
    printf("(%lf) x^%d + ",c[i],i);
       }
     }
     printf("\n do you want to perform function till \n\n1)No. of ittrations\t2)Accuracy\n\n");
       scanf("%d",&n);

     if(n==1)
     {
      printf("\nSo, enter the no. of ittrations\t");
      scanf("%d",&n1);
     }
     else if(n==2)
     {
      printf("\nSo, enter the Accuracy till\t");
      scanf("%lf",&n2);
     }


printf("\n\n Enter the initial value for x1");
scanf("%lf",&x1);



      for(  l=1,x2=1;  (n==1?l<=n1:fabs(x2)>=n2);   l++  )
      {
     for(fx1=0,i=power;i>=0;i--)
        {
          fx1+=(c[i]*pow(x1,i));

        }
        m=power-1;
      for(fx2=0,i=power;i>0;i--)
      {
       fx2+=((i*c[i])*pow(x1,m));
       m--;

      }

      x2=(x1-(fx1/fx2));

       if(l==1)
       {
        printf("\n\n\tItt. \tx1   \tfx1    \t\tx2    \tf'x2 \n\n");
       }
     printf("\t%d    %lf    %lf    %lf    %lf \n",l,x1,fx1,x2,fx2);

      x1=x2;
     }

getch();
}



Download the executable (.exe)  file for this program..
download

0 comments:

Post a Comment

 

My Blog List

Term of Use