/*Newton Raphson*/
# include <stdio.h>
# include <conio.h>
# include <math.h>
# define EPS .00001
# define f(x) x*x+x-2
# define f_(x) 2*x+1
void main()
{
int n=0;
float f0,f1,x0,x1;
clrscr();
printf(" Solution of Equation x%c+x-2 through Newton raphson method",253);
printf("\nEnter Initial Guess : ");
scanf("%f",&x0);
f0=f(x0);
f1=f_(x0);
x1=(x0-(f0/f1));
printf("No\t x0\t\t f(x0)\t\t f'(x0)\t\t x1\n");
printf("-------------------------------------------------------------------\n");
while(fabs(x1-x0)/x1>EPS)
{
n++;
printf("%d\t%f\t\%f\t%f\t%f\n",n,x0,f0,f1,x1);
x0=x1;
f0=f(x0);
f1=f_(x0);
x1=(x0-(f0/f1));
}
printf("-------------------------------------------------------------------\n");
printf("Root = %f \n",x0);
printf("Function value = %f\n",f(x0));
printf("-------------------------------------------------------------------\n");
getch();
}
OUTPUT
Solution of Equation x²+x-2 through Newton raphson method
Enter Initial Guess : 0
No x0 f(x0) f'(x0) x1
--------------------------------------------------------------------------
1 0.000000 -2.000000 1.000000 2.000000
2 2.000000 4.000000 5.000000 1.200000
3 1.200000 0.640000 3.400000 1.011765
4 1.011765 0.035433 3.023530 1.000046
5 1.000046 0.000137 3.000092 1.000000
--------------------------------------------------------------------------
Root = 1.000000
Function value = 0.000000
thank u so much...it really helped me alot
ReplyDelete