Bisection

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define F(x) x*x-4*x-10
int main ()
{
	float x,x1,x2,xm,fx1,fx2,fxm,e=0.0001,c=0,a=1.0;
   clrscr();
   printf("Enter x1 and x2: ");
   scanf("%f %f",&x1,&x2);
   //xm=(x1+x2)/2;
   while(a>e)
   {
		xm=(x1+x2)/2;
      x=x1;
      fx1=F(x);
      x=x2;
      fx2=F(x);
      x=xm;
      fxm=F(x);

      if ((fxm*fx1)<0)
      {
			fx2=fxm;
	 		x2=xm;
      }
      else
      {
			fx1=fxm;
	 		x1=xm;
      }
      a=fabs((x2-x1)/x2);
      c++;

   }
   printf("Itteration: %.0f times.\nAns: %f",c,xm);
   getch();
   return 0;
}

Related posts:

Leave a Reply

Your email address will not be published. Required fields are marked *