Bisection Method
Code :
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
return ((x*x*x)-x-11);
}
void main()
{
float a,b,m,eps;
int c,acc,temp;
clrscr();
printf("Equation : X^3-X-11");
printf("enter a:");
scanf("%f",&a);
printf("enter b:");
scanf("%f",&b);
while(f(a)*f(b)>0)
{
printf("\nintervals are invalid:");
printf("\nenter a:");
scanf("%f",&a);
printf("\nenter b:");
scanf("%f",&b);
}
printf("\nenter accuracy:");
scanf("%d",&acc);
eps=pow(10,-(acc));
for(c=1;c<50;c++)
{
m=(a+b)/2;
if((f(m)*f(a))>0)
a=m;
else
b=m;
printf("\niteration no. x[%d] is %f",c,m);
if(fabs(a-b)<eps)
break;
}
a=m*pow(10,acc);
temp=(int)a;
m=temp/pow(10,acc);
printf("\nrequired %dD accurate ans is %g",acc,m);
getch();
}
Output :
No comments:
Post a Comment