#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#define f(x) x*x-3*x+2
#define df(x) 2*x-3
#define E 0.00001
int main()
{
float x0,x1,y0,dy0;
int i,n;
printf("Enter the value of x : ");
scanf("%f",&x0);
printf("\nEnter the number of iteration : ");
scanf("%d",&n);
printf("\nIter.\tx0\tf(x0)\tdf(x0)\tx1\n");
for(i=0;i<n;i++)
{
y0=f(x0);
dy0=df(x0);
x1=(x0-(y0/dy0));
x0=x1;
printf("%d\t%.4f\t%.4f\t%.4f\t%.4f",i,x0,y0,dy0,x1);
}
if((fabs(x1-x0)/x1)<E)
{
printf("\nNumber of iteration is %d",i);
printf("\nRoot = %f\n",x1);
}
getch();
return(0);
}
No comments:
Post a Comment