Bisection Method

Submitted by Karthikeyan on

Sample C program that uses Bisection Method in mathematics


#include
#include
#include
float function(float x)
{
    float f;
	f= (x*x*x)-4*x-9;/*Given Function*/
    return(f);
}

void design()
{
    int m;
    printf("\n");
    for(m=1; m<80;m++)
    printf("*");
}

main()
{
    float x0 = 0.0, v = 0.0, x1=0.0, f0, f1, f2, x2, x, e;
	int i;
	clrscr();
    printf("\n\t\t Bisection Method");
    printf("\n\t\t ****************");
	printf("\n");
	printf("Given Function x^3 - 4x - 9 = 0\n");
	printf("Enter Accuracy : ");
	scanf("%f", &e);
	values:
	   x0 = v;
       x1 = x1 +1;
	   f0 = function(x0);
	   f1 = function(x1);
	   v = x1;
	if ((f0*f1)>0.0)
	goto values;
	else
	{
		design();
		printf("\n\t x0\t  x1\t   x2\t  f0\t  f1\t  f2");
        design();
        do
        {
            f0 = function(x0);
            f1 = function(x1);
            x2 = (x0 + x1)/2;
            f2 = function(x2);
			printf("\n %7.4f\t%7.4f\t%7.4f\t%7.4f\t%7.4f\t%7.4f\t", x0,x1,x2,f0,f1,f2);
            if((f0*f2)<0)
            x1 = x2;
            else
            x0=x2;
        }
		while(fabs((x0-x1)/x1)>=e);
        printf("\n");
        design();
		printf("\n\t\t Root can varges at = %7.4f\t", x2);
		design();
	  }
	  getch();
}