#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x) x*x*x-5*x+3
#define df(x) 3*x*x-5
//int printline()
//{
//int i;
//for(i=1;i<=60;i++)
//printf("-");
//printf("\n");
//}
int main()
{
float
x0,x1,y0,dy0,e,delta;
int i,n;
printf("enter the
initial guess\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);
printf("enter the
minimum allowed value of slope\n");
scanf("%f",&delta);
//printline();
printf("iter\tX0\tF(X0)\tF'(x0)\tX1\n");
//printline();
for(i=1;i<=n;i++)
{
y0 = f(x0);
dy0= df(x0);
if ((fabs(dy0)) < delta)
{
printf("slope is too small\n");
return(0);
}
x1=x0-(y0/dy0);
printf("
%d\t%.2f\t%.2f\t%.2f\t%.2f\n",i,x0,y0,dy0,x1);
if ((fabs(x1-x0)/x1)<=e)
{
//printline();
printf("converges to the
root\n");
printf("number of iteration is
%d\n",i);
printf("root is %.2f\n",x1);
getch();
return(0);
}
x0=x1;
}
//printline();
printf("does not
converges to the root in %d iteration",n);
getch();
}
No comments:
Write comments