Wednesday 23 April 2014

Program to find the roots of a polynomial equation using method of false position

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x) x*x*x-4*x+1

int main()
{
float x0,x1,e,y0,y1,x2,y2;
int i,n;
//clrscr();
printf("enter the initial guesses\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);
y0=f(x0);
y1=f(x1);
if(y0*y1 > 0)
{
  printf("initial guesses are not suitable\n");
  return (0);
  //exit(0);
}
//printline();
printf("iter\tX0\tF(X0)\tX1\tF(X1)\tX2\tF(X2)\n");
//printline();
//for(i=1;i<=n;i++)
{
  x2=(x0*y1-x1*y0)/(y1-y0);
  y2=f(x2);
  printf("  %d\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\n",i,x0,y0,x1,y1,x2,y2);
  if ((fabs(y2))<=e)
  {
    //printline();
    printf("converges to the root\n");
    printf("number of iteration is %d\n",i);
    printf("root is %.2f\n",x2);
    getch();
    return (0);
    //exit(0);
  }
  if (y0*y2>0)
  {
    x0=x2;
    y0=y2;
  }
  else
  {
    x1=x2;
    y1=y2;
  }
}
//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-2022