Tuesday, November 27, 2007

Program in C for Bisection method...

Here is code for Bisection method. Documented code in C.
Important notes:
1. while running the program replace [] (in #include )by less than and greater than symbol since i cant publish post with those symbol.
2. if there is written like less than then use symbol in actual program.



/*programm for bisecion method by hemant abhare*/
#include[stdio.h]
#include[math.h]
#define epsilon 1e-6
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);
}