Sunday 22 March 2015

Find the root of a equation using Newton Raphson Using C

# include <stdio.h>
# include <conio.h>
# include <math.h>

# define EPS 0.000001
# define MAXIT 20
# define F(x) (x) * (x) * (x) - 2 * (x) - 5
# define FD(x) 3 * (x) * (x) - 2

void main()
{
int count ;
float x0, xn, fx, fdx ;
clrscr() ;
printf("Input initial value of x : ") ;
scanf("%f", &x0) ;
count = 1 ;
begin :
fx = F(x0) ;
fdx = FD(x0) ;
xn = x0 - fx / fdx ;
if(fabs((xn - x0) / xn) < EPS)
{
printf("\nRoot = %f", xn) ;
printf("\n\nNumber of iterations = %d", count) ;
}
else
{
x0 = xn ;
count = count + 1 ;
if(count < MAXIT)
{
goto begin ;
}
else
{
printf("\nSolution does not coverage in %d iterations",
MAXIT) ;
}
}
getch() ;
}

RUN 1 :
~~~~~~~
Input initial value of x : 0
Root = 2.094552
Number of iterations = 19

No comments:

Post a Comment