Saturday, 28 September 2013

wap in c to find the real root of the equation xlog(x)-1.2=0 by using BISECTION METHOD.

#include<stdio.h>
#include<math.h>
#include<conio.h>
#include<process.h>
#include<string.h>
#define EPS 0.00000005
#define f(x) (x)*log10(x)-1.2
void bisect();
int count=1,n;
float root=1;
void main()
{
clrscr();
 printf("\n solution by BISECTION METHOD \n");
 printf("\n equation is ");
 printf("\n \t\t\t x*log(x)-1.2=0\n\n");
 printf("enter the number of iterations:");
 scanf("%d",&n);
 bisect();
getch();
}
void bisect()
{
 float x0,x1,x2,f0,f1,f2;
 int i=0;
 for(x2=1;;x2++)
 {
  f2=f(x2);
  if(f2>0)
  {
   break;
  }
 }
 for(x1=x2-1;;x2--)
 {
  f1=f(x1);
  if(f1<0)
  {
   break;
  }
 }
 for(;count<=n;count++)
 {
  x0=(x1+x2)/2.0;
  f0=f(x0);
  if(f0==0)
  {
   root=x0;
  }
  if(f0*f1<0)
  {
   x2=x0;
  }
  else
  {
   x1=x0;
   f1=f0;
  }
  printf("\n\t\t iteration %d",count);
  printf("\t  :\t  %f",x0);
  if(fabs((x1-x2)/x1)<EPS)
  {
   printf("\n\t\t--------------------------------------------");
   printf("\n\t\t      root=%f",x0);
   printf("\n\t\t   iteration=%d\n",count);
   printf("\t\t----------------------------------------------");
   exit(0);
  }
 }
 printf("\n\t\t---------------------------------------------------");
 printf("\n\t\t\t root=%7.4f",x0);
 printf("\n\t\t\t iteration = %d\n",count-1);
 printf("\t\t------------------------------------------------------------------------------------------------------------");
 }

No comments:

Post a Comment