EXPLORING CYBERSPACE…

Finding roots of equation using Newton Raphson Method

June 10, 2009 · Leave a Comment

#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<math.h>
float f(float x)
{
 return(x*x*x-24);
}
float df(float x)
{
 return 3*x*x;
}

int main()
{
 clrscr();
 int itr,maxitr;
 float x0,h,aerr,x1;
 printf("\n\nEnter the values of x0, allowed error and maximum iterations==>\n");
 scanf("%f %f %d",&x0,&aerr,&maxitr);
 for(itr=1;itr<=maxitr;itr++)
 {
 h=-(f(x0)/df(x0));
 x1=x0+h;
 printf("Iteration no. %d X=%f\n",itr,x1);
 if(fabs(h)<aerr)
 {
 printf("After%d Iterations, root=%f\n",itr,x1);
 delay(4000);
 return 0;
 }
 x0=x1;
 }
 printf("\nSolution does not converge, Iterations not Sufficient");
 delay(4000);
 return 1;
}

Categories: Numerical Techniques · Programming in C

0 responses so far ↓

  • There are no comments yet...Kick things off by filling out the form below.

Leave a Comment