Wednesday, 23 April 2014

Program to find the roots of a polynomial equation using newton raphson method

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x) x*x*x-5*x+3
#define df(x) 3*x*x-5

//int printline()
//{
//int i;
//for(i=1;i<=60;i++)
//printf("-");
//printf("\n");
//}

int main()
{
float x0,x1,y0,dy0,e,delta;
int i,n;
printf("enter the initial guess\n");
scanf("%f%f",&x0,&x1);
printf("enter the prescribed precision value\n");
scanf("%f",&e);
printf("enter the maximum number of iteration\n");
scanf("%d",&n);
printf("enter the minimum allowed value of slope\n");
scanf("%f",&delta);
//printline();
printf("iter\tX0\tF(X0)\tF'(x0)\tX1\n");
//printline();
for(i=1;i<=n;i++)
{
  y0 = f(x0);
  dy0= df(x0);
  if ((fabs(dy0)) < delta)
  {
    printf("slope is too small\n");
    return(0);
  }
  x1=x0-(y0/dy0);
  printf("  %d\t%.2f\t%.2f\t%.2f\t%.2f\n",i,x0,y0,dy0,x1);
  if ((fabs(x1-x0)/x1)<=e)
  {
    //printline();
    printf("converges to the root\n");
    printf("number of iteration is %d\n",i);
    printf("root is %.2f\n",x1);
    getch();
    return(0);
  }
  x0=x1;
}
//printline();
printf("does not converges to the root in %d iteration",n);
getch();
}

No comments:
Write comments

Featured post

List of Universities in Karnataka offering M.Sc Computer Science

The post-graduate programme in Computer Science (M.Sc Computer Science) contains two academic years duration and having a four semesters....

Popular Posts

Copyright @ 2011-2016