#include<stdio.h>
#include<math.h>
#include<conio.h>
#define EPS 0.00000005
#define f(x) x*x*x-x-1
void bisect();
int count=1,n;
float root=1;
void main()
{
clrscr();
printf("\n solution by BISECTION METHOD \n");
printf("\n equation is ");
printf("\n \t\t\t x*x*x-x-1=0\n\n");
printf("enter the number of iterations:");
scanf("%d",&n);
bisect();
getch();
}
void bisect()
{
float x0,x1,x2,f0,f1,f2;
int i=0;
for(x2=1;;x2++)
{
f2=f(x2);
if(f2>0)
{
break;
}
}
for(x1=x2-1;;x2--)
{
f1=f(x1);
if(f1<0)
{
break;
}
}
for(;count<=n;count++)
{
x0=(x1+x2)/2.0;
f0=f(x0);
if(f0==0)
{
root=x0;
}
if(f0*f1<0)
{
x2=x0;
}
else
{
x1=x0;
f1=f0;
}
printf("\n\t\t iteration %d",count);
printf("\t :\t %f",x0);
if(fabs((x1-x2)/x1)<EPS)
{
printf("\n\t\t--------------------------------------------");
printf("\n\t\t root=%f",x0);
printf("\n\t\t iteration=%d\n",count);
printf("\t\t----------------------------------------------");
exit(0);
}
}
printf("\n\t\t---------------------------------------------------");
printf("\n\t\t\t root=%7.4f",x0);
printf("\n\t\t\t iteration = %d\n",count-1);
printf("\t------------------------------------------------------------------------------------------------------------");
}
#include<math.h>
#include<conio.h>
#define EPS 0.00000005
#define f(x) x*x*x-x-1
void bisect();
int count=1,n;
float root=1;
void main()
{
clrscr();
printf("\n solution by BISECTION METHOD \n");
printf("\n equation is ");
printf("\n \t\t\t x*x*x-x-1=0\n\n");
printf("enter the number of iterations:");
scanf("%d",&n);
bisect();
getch();
}
void bisect()
{
float x0,x1,x2,f0,f1,f2;
int i=0;
for(x2=1;;x2++)
{
f2=f(x2);
if(f2>0)
{
break;
}
}
for(x1=x2-1;;x2--)
{
f1=f(x1);
if(f1<0)
{
break;
}
}
for(;count<=n;count++)
{
x0=(x1+x2)/2.0;
f0=f(x0);
if(f0==0)
{
root=x0;
}
if(f0*f1<0)
{
x2=x0;
}
else
{
x1=x0;
f1=f0;
}
printf("\n\t\t iteration %d",count);
printf("\t :\t %f",x0);
if(fabs((x1-x2)/x1)<EPS)
{
printf("\n\t\t--------------------------------------------");
printf("\n\t\t root=%f",x0);
printf("\n\t\t iteration=%d\n",count);
printf("\t\t----------------------------------------------");
exit(0);
}
}
printf("\n\t\t---------------------------------------------------");
printf("\n\t\t\t root=%7.4f",x0);
printf("\n\t\t\t iteration = %d\n",count-1);
printf("\t------------------------------------------------------------------------------------------------------------");
}
No comments:
Post a Comment