Bisection Method using C Langauge

Bisection Method using C Language

Bisection method is a method to find the roots of a equation that applies to any continuous functions for which one knows two values with opposite signs. Now we will find the roots of the equation by using Bisection Method using C Language.

Let’s take a example of equation to find the roots using Bisection method :-

x3-2x-5=0

C program to find the roots of the equation by using Bisection Method :-

/*  Exp.No 1: Bisection Method (Finding the roots of given Equ.) */
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x)  pow(x,3)-2*x-5.0
int main()
{
  float x0,x1,x2, y0,y1,y2, e;
  printf("Enter two initial starting roots \n");
  scanf("%f%f",&x0,&x1);
  printf("Enter the tolerance value \n");
  scanf("%f",&e);

  y0 = f(x0);
  y1 = f(x1);
  if (y0*y1 > 0.0)
  {
     printf("Initial values are unsuitable \n");
  }
  else
  {
      while((fabs(x1-x0)/x1) > e)
      {
          x2 = (x0 + x1)/2.0;
          y2 = f(x2);
          if (y0*y2 > 0.0)
              x0 = x2;
          else
              x1 = x2;
      }
      printf("\noutput: \n\n");
      printf("Solution converges to a root \n\n");
      printf("Root of the given equation is = %8.3f\n", x2);
  }
  return 0;
}

 

OUTPUT :-

 

Enter two initial starting roots
2 3
Enter the tolerance value
.0001

output:

Solution converges to a root

Root of the given equation is =    2.095

Leave a Reply