#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x) x*x*x-4*x+1
int main()
{
float
x0,x1,e,y0,y1,x2,y2;
int i,n;
//clrscr();
printf("enter the
initial guesses\n");
scanf("%f%f",&x0,&x1);
printf("enter the
prescribed precision value\n");
scanf("%f",&e);
printf("enter the
maximum number of iteration\n");
scanf("%d",&n);
y0=f(x0);
y1=f(x1);
if(y0*y1 > 0)
{
printf("initial guesses are not
suitable\n");
return (0);
//exit(0);
}
//printline();
printf("iter\tX0\tF(X0)\tX1\tF(X1)\tX2\tF(X2)\n");
//printline();
//for(i=1;i<=n;i++)
{
x2=(x0*y1-x1*y0)/(y1-y0);
y2=f(x2);
printf("
%d\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\n",i,x0,y0,x1,y1,x2,y2);
if ((fabs(y2))<=e)
{
//printline();
printf("converges to the
root\n");
printf("number of iteration is
%d\n",i);
printf("root is %.2f\n",x2);
getch();
return (0);
//exit(0);
}
if (y0*y2>0)
{
x0=x2;
y0=y2;
}
else
{
x1=x2;
y1=y2;
}
}
//printline();
printf("does not
converges to the root in %d iteration",n);
getch();
}
No comments:
Write comments