Beruflich Dokumente
Kultur Dokumente
-049/13 branch-ece
//to find root of the equation using bisection method
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define ESP 0.001
#define F(x) (x)*(x)*(x)-3*(x)-9
void main()
{
int i=1;
float x0,x1,x2;
double f1,f2,f0,t;
printf("\n Enter the value of x0:");
scanf("%f",&x0);
printf("\n Enter the value of x1:");
scanf("%f",&x1);
printf("\n-----------------------------------------------");
printf("\n interation \t x0 \t x1 \t x2 \t f0 \t f1 \t f2");
printf("\n-----------------------------------------------");
do
{
x2=(x0+x1)/2;
f0=F(x0);
f1=F(x1);
f2=F(x2);
printf("\n\t%d\t%.1f\t%.1f\t%.1f\t%.1f\t%.1f\t%.1f",i,x0,x1,x2,f0,f1,f2);
if(f0*f2<0)
{
x1=x2;
}
else
{
x0=x2;
}
i++;
if(i<=4)
continue;
else
break;
}while(fabs(f2)>ESP);
printf("\n----------------------");
printf("\n\nApp. root=%f",x2);
getch();
}
output:
enter the value of x0:2
enter the value of x1:3
interation
x0
x1
x2
f0
f1
f2
2.0
3.0
2.5
-7.8
9.0
-0.9
2.5
3.0
2.8
-0.9
9.0
3.5
2.5
2.8
2.6
-0.9
3.5
1.2
2.5
2.6
2.6
-0.9
1.2
0.1
Approot=2.562500