Wednesday 23 April 2014

Program to find the roots of a polynomial equation using bisection method

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x) x*x*x-x-1
int main()
{
float x0,x1,e,y0,y1,x2,y2;
int i=0;
printf("enter the initial guesses\n");
scanf("%f%f",&x0,&x1);
printf("enter the prescribed precision value\n");
scanf("%f",&e);
y0=f(x0);
y1=f(x1);
if(y0*y1>0)
{
  printf("initial guesses are not suitable\n");
  return(0);
}
//printline();
printf("iter\tX0\tF(X0)\tX1\tF(X1)\tX2\tF(X2)\n");
//printline();
while (fabs((x1-x0)/x1)>=e)
{
  x2=(x0+x1)/2;
  y2=f(x2);
  i=i+1;
  printf("  %d\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\n",i,x0,y0,x1,y1,x2,y2);
  if(y0*y2>0)
  {
    x0=x2;
}
  else
  {
    x1=x2;
}
}

printf("converges to the root\n");
printf("number of iteration is %d\n",i);
printf("root is %.2f",x2);
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-2022