C Program of Newton Raphson Method

C Program of Newton Raphson Method

In this article we will see C Program of Newton Raphson Method i.e. how to solve the algebraic and transcendental equations using Newton Raphson Method.

The Newton-Raphson method (also known as Newton’s method) is a method to quickly find a good approximation for the root of a real-valued function f ( x ) = 0 f(x) = 0 f(x)=0.

C Program of Newton Raphson Method

We will see a C program for finding a real root of the equation x3-3x-5=0 by using Newton Raphson Method correct to four decimal places which lies in [2,3].

C Program of Newton Raphson Method

/*  Exp.No 3: Newton-Raphson method (Finding the roots of given Equ.) */
#include<stdio.h>
#include<conio.h>
#include<math.h>

#define f(x)   pow(x,3)+x*x-1
#define df(x)  3*x*x+2*x
#define e      0.00001
int main()
{
  long x0,x1,f0,f1;
  int maxitr,i;

  printf("Enter initial root \n");
  scanf("%lf",&x0);

  printf("Enter maximum iteration \n");
  scanf("%d",&maxitr);

  for(i=1;i<=maxitr;i++)
  {
    f0 = f(x0);
    f1 = df(x0);
    x1 = x0-(f0/f1);
    if((fabs(x1-x0)/x1) < e)
    {
        printf("\nsolution converges to a roots \n");
        printf("\nNumber of Iteration = %d\n",i);
        printf("\nRoot of the equation is = %8.4lf", x1);
        break;
    }
    else
    {
        x0=x1;
    }
  }
  return 0;
}

OUTPUT

Enter initial root
2
Enter maximum iteration
10

solution converges to a roots

Number of Iteration = 4

Root of the equation is =   2.2790

You may also practice :-

  • Write a C program for finding a root of the equation x3+x2-x-1 = 0  correct up four decimal places.
  • Write a C program for finding a root of the equation 2x3-2.5x2-5 = 0 for the root in [1,2].

You might also Like :-

Leave a Reply