§
/* Newton Raphson Method*/
PROGRAMMING
IN C: ASSIGNMENT
PRATYUSH DAS
BEE 3RD
YEAR
Electrical
Engineering Department, Jadavpur University
-------Contents-------
§
Gauss-elimination method
§
Gauss-Seidel method
§
Bisection method
§
False position method
§
Secant method
§
Newton-Raphson method ...............................................................................
.........................................................................
/* Newton Raphson Method*/
#include<stdio.h>
#include<math.h>
#define
f(x) (x*x*x-4*x-9) // any equation can be taken here
#define
f1(x) (3*x*x-4)
void
main()
{
int
itr,N;
float
h,x0,x1,err;
printf("------------NEWTON-RAPHSON
METHODOLOGY------------\n\n\n");
printf("enter
x0,allowed error,maximum iterations\n");
scanf("%f
%f %d",&x0,&err,&N);
printf("\n\n");
for(itr=1;itr<=N;itr++)
{
h=f(x0)/f1(x0);
x1=x0-h;
printf("iteration no. %3d
gives ,x= %9.6f\n",itr,x1);
if(fabs(h)<err)
{
printf("after %3d iterations,""root is \n x=%8.6f\n",itr,x1);
getch();
exit(1);
}
x0=x1;
}
printf("iterations not sufficient, solution does not
converge\n");
getch();
}
ª
Results:
(Newton-Raphson method)
------------NEWTON-RAPHSON METHODOLOGY-------
enter x0,allowed error,maximum iterations
2 .005 66
iteration no. 1 gives ,x=
3.125000
iteration no. 2 gives ,x=
2.768530
iteration no. 3 gives ,x=
2.708196
iteration no. 4 gives ,x=
2.706529
after 4
iterations,root is
x=2.706529