Newton-Raphson Method

This method is also called "Tangent Method".
ALGORITHM:
  1. START.
  2. Find the first order derivative f'(x) of the given non-linear equation f(x).
  3. Assume initial guess as x0.
  4. Find the next value from N-R formula as x=x0-f(x0)/f'(x0). 
  5. If |x-x0|<accuracy, root = x, Else set x0=x and go to step 4.
  6. STOP.
PROGRAM in C:
#include<stdio.h>
#include<math.h>
#include<conio.h>

float f(float x)
{
  return(3*x-cos(x)-1); /* 3x=cos(x)+1 */
}

float g(float y)
{
  return(3+sin(y)); /* 3+sin(y) */
}

void main()
{
  float x0,x=0,eps=0.001,tmp;
  int iteration=0;
  printf("\nEnter guess:");
  scanf("%f",&x0);
  do
  {
    tmp=x;
    x=x0-(f(x0)/g(x0));
    iteration++;
    x0=x;
  }while(fabs(tmp-x)>eps);
  printf("Root = %.4f Iteration = %d",x,iteration);
  getch();

No comments:

Post a Comment