You are on page 1of 2

#include[stdio.

h]
#include[math.h]
#define epsilon 1e-6
void main()
{
double g1,g2,g,v,v1,v2,dx;
int found,converged,i;
found=0;
printf(" enter the first guess\n");
scanf("%lf",&g1);
v1=g1*g1*g1-15;
printf("value 1 is %lf\n",v1);
while (found==0)
{
printf("enter the second guess\n");
scanf("%lf",&g2);
v2=g2*g2*g2-15;
printf(" value 2 is %lf\n",v2);
if (v1*v2>0)
{found=0;}
else
found=1;
}
printf("right guess\n");
i=1;
while (converged==0)
{
printf("\n iteration=%d\n",i);
g=(g1+g2)/2;
printf("new guess is %lf\n",g);
v=g*g*g-15;
printf("new value is%lf\n",v);
if (v*v1>0)
{
g1=g;
printf("the next guess is %lf\n",g);
dx=(g1-g2)/g1;
}
else
{
g2=g;
printf("the next guess is %lf\n",g);
dx=(g1-g2)/g1;
}
if (fabs(dx)'less than' epsilon
{converged=1;}
i=i+1;
}
printf("\nth calculated value is %lf\n",v);

}
Read more: http://wiki.answers.com/Q/C_programming_of_bisection_method#ixzz202ZxxEmL

You might also like