Nov 18, 2008

Bisection Method

#include
#include
#include

float f(float x)
{
float y;
y=x*x-25;
return y;
}

int main()
{
int i;
float x0,x1,x2,y0,y1,y2,e;
printf("Enter the value of x0,x1,error allowed\n");
scanf("%f\n %f\n %f",&x0, &x1,&e);
printf("x0=%f, x1=%f,e=%f\n",x0,x1,e);
y0=f(x0);
y1=f(x1);
i=0;
if((y0*y1)>0)
{
printf("Starting value unsuitable\n");
printf("x0=%f, x1=%f, y0=%f, y1=%f",x0,x1,y0,y1);
exit(1);
}
printf(" Iteration x0 x1 x2 f(x0) f(x1) f(x2)\n");
while((fabs((x1-x0)/x1))>e)
{
x2=(x0+x1)/2;
y2=f(x2);
i++;
if((y0*y2)>0)
{
x0=x2;
}
else
{
x1=x2;
}
printf(" %d %f %f %f %f %f %f\n",i,x0,x1,x2,y0,y1,y2);
}
printf("Solution is convergent to a root.\n");
printf("No. of iteration= %d & x=%f\n\n",i,x2);

return 0;

}

0 comments: