Sunday 1 April 2012

c Program to Implement BISECTION METHOD


/*
  Program to Implement BISECTION METHOD.
  EQN -> x*log10(x) - 1.2
*/

#include<stdio.h>
#include<conio.h>
#include<math.h>

#define F(x)  (x)*log10(x)-1.2
int n;
float root=1;

void bisect();

void main()
{
clrscr();
printf("\n Solution by BISECTION METHOD ");
printf("\n Equation is -> x*log(x) - 1.2 = 0\n");
printf("\n Enter the no. of iterations ");
scanf("%d",&n);
bisect();
getch();
}

void bisect()
 {
 int count;
 float x1,x2,x0;
 float f1,f2,f0;
 // Finding x2
 for(x2=1; ;x2++)
    {
    f2=F(x2);
    if(f2>0)
      {
      break;
      }
    }
 printf("\n x2 = %d ",x2);
 // Finding x1
 for(x1=x2-1; ;x1--)
    {
    f1=F(x1);
    if(f1<0)
      {
      break;
      }
    }
 printf("\n x2 = %d ",x2);
 printf("\n ----------------------------------------------------");
 printf("\n \tIteration No.    \t -        ROOTS ");
 printf("\n ----------------------------------------------------\n");
 for(count=1;count<=n;count++)
    {
    x0=(x1+x2)/2;
    f0=F(x0);
    if(f0==0)
      {
      root=x0;
      }
    if(f0*f1<0)
      {
      x2=x0;
      f2=f0;
      }
    else
      {
      x1=x0;
      f1=f0;
      }
    printf("\n \t ITERATION %d \t : \t %f",count,x0);
    if( fabs((x1-x2)/2) < 0.00000005)
      {
      printf("\n -----------------------------------------------");
      printf("\n \t \t \tROOT       = %f ",x0);
      printf("\n \t \t \tITERATIONS = %d ",count);
      printf("\n -----------------------------------------------");
      exit(0);
      }
    }
printf("\n ----------------------------------------------------");
printf("\n \t \t    ROOT       = %7.4f ",x0);
printf("\n \t \t    ITERATIONS = %d ",count-1);
printf("\n ----------------------------------------------------");
}

4 comments:

  1. Replies
    1. WE TAKE LOG ON BASE 10 NOT ON BASE 2.......

      Delete
  2. please, help me Obtain the local minimize of the function f(x) = X^5-5*X^3-20*X+5 on [-3,O] and [0,3] by use bisection method in C Language. and show while loop.

    help me , please!!!




    ReplyDelete
  3. How can we perform this by for loop?

    ReplyDelete