1) F(x) = X^3 – X - 1
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define e 0.001
float frac(float
a)
{
float f1;
f1=a*a*a-a-1;
return f1;
}
void main()
{
float x1,x2,x0,f0,f1,f2;
do
{
printf("Enter values of x1 and x2\n");
scanf("%f%f",&x1,&x2);
printf("\nx1 = %f\n",x1);
printf("x2 = %f\n",x2);
f1=frac(x1);
f2=frac(x2);
}
while((f1*f2)>0);
printf("_______________________________________________________________________________________\n");
printf(" x1
| x2 |
x0 | f1
| f2 |
f0 |\n");
printf("---------------------------------------------------------------------------------------\n");
do
{
f1=frac(x1);
f2=frac(x2);
x0=(x1+x2)/2;
f0=frac(x0);
if((f1*f0)<0)
{
printf(" %f |
%f | %f
| %f |
%f | %f |
\n",x1,x2,x0,f1,f2,f0);
x2=x0;
f2=f0;
}
else
{
printf(" %f |
%f | %f
| %f |
%f | %f |
\n",x1,x2,x0,f1,f2,f0);
x1=x0;
f1=f0;
}
}
while(fabs(x2-x1)>e);
printf("---------------------------------------------------------------------------------------\n\n");
printf("\n Root of
the equation is: %f",x0);
getch();
}