//Bisection Method
#include<stdio.h>
#include<math.h>
float root( float x)
{
float f;
f=x*x*x-x-1;
return f;
}
void main()
{
int i,n;
float a,b,x,e,t,p;
clrscr();
printf(" Type a and b\n");
scanf("%d%d",&a,&b);
printf(" Type maximum no. of interval n\n");
scanf("%d", &n);
printf(" Type the value of e\n");
scanf("%f",&e);
p=root(a)*root(b);
if(p>0)
printf(" Root does not lie between a and b");
else
{
i=1; t=1;
while(t>e && i<=n)
{
x=(a+b)/2;
if(root(x)*root(a)>0)
a=x;
else
b=x;
t=fabs(a-b);
if(fabs(root(x)))
t=fabs(root(x));
i++;
}
if(i>n)
printf(" Iteration does not converge");
else
{
printf(" approximate root = %8.4f\n",x);
printf( " Number of iteration = %d\n",i);
}
}
getch();
}
No comments:
Post a Comment