Write a program to find the roots of an equation f (x) = 0 using Bisection method.

#include<stdio.h>
#include<conio.h>
#include<math.h>
float fun(float x)
{
     return(pow(x,3)-x-1);
}
 void main()
 {
         float a,b,eps,x;
         int i=0;
         clrscr();
         printf("\n enter the lower and upper limit ");
         scanf("%f%f",&a,&b);
         printf("\n enter the epsilon value");
         scanf("%f",&eps);
         if((fun(a)*fun(b))>0)
         printf("\n starting value is unsuitable ");
         else
         while(fabs((b-a)/b)>eps)
                {
                    x=(a+b)/2;
                    i++;
                    if((fun(x)*fun(a))>0)
                    a=x;
                    else
                    b=x;
                    }
        printf("\n solution converges to a root \n");
        printf("\n number of iterationo=%d\n",i);
        printf("%f\n",x);
        getch();
   }

No comments:

Post a Comment