Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Name:-simran

Department:cse2b
Roll:-87
Question:-find the root of x3-x-2=0 using bisectionn method upto
accuracy 0.01
#include<stdio.h>
#include<math.h>
#define F(x)(x)*(x)*(x)-(x)-4
int main()
{
float a,b,x0,fa,fb,fx0,e,m;
int step=1;
printf("\n enter two intial guesses:\n");
scanf("%f%f",&a,&b);
printf("enter tolerable error:\n");
scanf("%f",&e);
fa=F(a);
fb=F(b);if(fa*fb>0.0)
{
printf("incorrect initial guesses!please enter another
guesses.\n");
}
printf("\n\tstep\t\ta\t\tb\t\tx0\t\tf(x0)\t\t[a-b]\n");
printf("\n");
do
{
x0=(a+b)/2;
fx0=F(x0);
m=fabs(a-b);
printf("\t%d\t\t%f\t%f\t%f\t%f\t%f\n",step,a,b,x0,fx0,m);
if(fa*fx0<0){
b=x0;
fb=fx0;
}
else{
a=x0;
fa=fx0;
}
step=step+1;
}
while(fabs(a-b)>=e);
printf("\nRoot is:%f",x0);
return 0;
}

You might also like