C Program for Newton Raphson Method

4 Min Read

Newton Raphson method is one of the most popular methods of solving a linear equation. In the past, it was used to solve astronomical problems, but now it is being used in different fields.

The most important reason behind this popularity is that it is easy to implement and does not require any additional software or tool. But, there are few things that you need to be aware of before using this method.

Here are the important tips to implement Newton Raphson method.

Newton-Raphson method, also known as the Newton’s Method, is the simplest and fastest approach to find the root of a function. It is an open bracket method and requires only one initial guess. The C program for Newton Raphson method presented here is a programming approach that can be used to find the real roots of not only a nonlinear function, but also those of algebraic and transcendental equations.

Newton’s method is often used to improve the result or value of the root obtained from other methods. This method is more useful when the first derivative of f(x) is a large value.

The programming effort for Newton Raphson Method in C language is relatively simple and fast. The convergence is the fastest of all the root finding methods discussed in Numerical Methods Tutorial section – the bisection method, the secant method and the regula-falsi method.

Features of Newton Raphson Method:

  • Type – open bracket
  • No. of initial guesses – 1
  • Convergence – quadratic
  • Rate of convergence – faster
  • Accuracy – good
  • Programming effort – easy
  • Approach – Taylor’s series

Below is a very short and simple source code in C program for Newton’s method to find the root of x*log10(x) – 1.2.

Variables:

  • itr – a counter which keeps track of the no. of iterations performed
  • maxmitr – maximum number of iterations to be performed
  • df(x) – the derivative of f(x) with respect to x
  • x0 – the value of root at nth iteration
  • x1 – the value of root at (n+1)th iteration
  • allerr – allowed error

f(x) = x*log10(x) – 1.2

Source Code for Newton Raphson Method in C:

#include<stdio.h>
#include<math.h>
float f(float x)
{
    return x*log10(x) - 1.2;
}
float df (float x)
{
    return log10(x) + 0.43429;
}
void main()
{
    int itr, maxmitr;
    float h, x0, x1, allerr;
    printf("\nEnter x0, allowed error and maximum iterations\n");
    scanf("%f %f %d", &x0, &allerr, &maxmitr);
    for (itr=1; itr<=maxmitr; itr++)
    {
        h=f(x0)/df(x0);
        x1=x0-h;
        printf(" At Iteration no. %3d, x = %9.6f\n", itr, x1);
        if (fabs(h) < allerr)
        {
            printf("After %3d iterations, root = %8.6f\n", itr, x1);
            return 0;
        }
        x0=x1;
    }
    printf(" The required solution does not converge or iterations are insufficient\n");
    return 1;
}

Input/Output:

Also see,
Newton -Raphson Method MATLAB Program
Newton-Raphson Method Algorithm/Flowchart
Numerical Methods Tutorial Compilation

Limitations of Newton-Raphson Method:

  • Finding the f'(x) i.e. the first derivative of f(x) can be difficult if f(x) is complicated.
  • When f'(xn) tends to zero i.e. the first derivative of f(xn) tends to zero, Newton-Raphson method gives no solution.
  • Near local maxima or local minima, there is infinite oscillation resulting in slow convergence.
  • In Newton’s method C program, if the initial guess is far from the desired root, then the method may converge to some other roots.
  • If root jumping occurs, the intended solution is not obtained.

Any queries or doubts regarding Newton Raphson method or its C programming source code aforementioned, can be directly brought up to us from the comments below.

Share This Article
22 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

English
Exit mobile version