F How can you implement Newton Raphson method in C programming? | CodeTheta

How can you implement Newton Raphson method in C programming?

January 17, 2014

This newton raphson program uses math function header file for implementing mathematical calculation.
/******************************************
         NEWTON RAPHSON METHOD
    http://native-code.blogspot.com
 ******************************************/

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x)(pow(x,3)-8*x-4)
#define g(x) (3*pow(x,2)-8)

void main()
{
   float x,x0,ac;
   int i,mxit;

   clrscr();
   //--------------------------

   printf("\nEnter initial value & desired accuracy:");
   scanf("%f %f",&x0,&ac);

   printf("\nEnter maximum iteration:");
   scanf("%d",&mxit);

   //--------------------------

   i=0;

   do
   { x=x0-(f(x)/g(x));
     x0=x; i++;
   }while((fabs(f(x))>=ac) && (i<=mxit));

   //--------------------------

   if(i<=mxit)
     printf("\n\n\nTHE ROOT OF THE EQUATION:%f",x);
   else
     printf("\n\n\nTHE EQUATION DOES NOT CONVERGE.");

   getch();
}

/* http://native-code.blogspot.com */ 

Post a Comment