Home >> Sem 2 >> Data-structur >> Bisection method
/* Write a Program to solve equation using Bisection Method */
#include<math.h>
float bisection(float,float,float);
void main()
{
float xl,xu,ea,fxr;
clrscr();
printf("Enter Lower Bound : ");
scanf("%f",&xl);
printf("Enter Upper Bound : ");
scanf("%f",&xu);
printf("Enter Percentage Error : ");
scanf("%f",&ea);
fxr = bisection(xl,xu,ea);
printf("F(xr) : %.2f",fxr);
getch();
}
float bisection(float xl,float xu,float ea)
{
float xr,fxr,tmp;
do
{
xr = (xl + xu) / 2;
fxr = ((xr*xr*xr) -9 * xr + 1);
if(fxr*xu < 0)
xu = xr;
else if(fxr*xu>0)
xl = xr;
else
xr = 0;
if(fxr<0)
tmp = fxr * -1;
}while(tmp>ea);
return xr;
}