Write a program to demonstrate Newton-Raphson Method in C language.

Tested on: Turbo C++ Version 4.0Time Complexity: O (n)Space Complexity: O (1)Open Source: MIT
1
2
            
3
    #include <stdio.h>
4
    #include <math.h>
5
    double f(float x)
6
    {
7
        double val = 0.0;
8
        val = x * x * x + x * x - 1;
9
        return val;
10
    }
11
    double diff(float x)
12
    {
13
        double val = 0.0;
14
        val = 3 * x * x + 2 * x;
15
        return val;
16
    }
17
    void main()
18
    {
19
        int i, n, max, count = 0;
20
        float x0, x1, aerr;
21
        double err, y0, dy0;
22
        printf("Enter initial guess, number of iterations and maximum allowed error: \n");
23
        scanf("%f%d%f", &x0, &max, &aerr);
24
        while (count < max)
25
        {
26
            y0 = f(x0);
27
            dy0 = diff(x0);
28
            if (y0 == 0)
29
            {
30
                printf("Root is %f ", x0);
31
                return;
32
            }
33
            else
34
            {
35
                x1 = x0 - (y0 / dy0);
36
                err = fabs(x1 - x0);
37
                if (err < aerr)
38
                {
39
                    count++;
40
                    printf("\nAfter %d iterations root=%f\n", count, x1);
41
                    return;
42
                }
43
                count++;
44
                x0 = x1;
45
                printf("Iteration no. %d: x=%f\n", count, x0);
46
            }
47
        }
48
        printf("Solution does not converge,iterations not sufficient");
49
        return;
50
    }
51
52
            

Output

1
2
           
3
    Enter initial guess, number of iterations and maximum allowed error: 
4
    4 10 0.008
5
    Iteration no. 1: x=2.589286
6
    Iteration no. 2: x=1.677368
7
    Iteration no. 3: x=1.123514
8
    Iteration no. 4: x=0.845007
9
    Iteration no. 5: x=0.762180
10
11
    After 6 iterations root=0.754931  
12
           
13
            

Rate the Code

Reviews are published after a quick safety check (usually within 24 hours).

Reviews are public. Developers can see basic device specifications to respond. Learn more

How to run Newton Raphson Method program in C-Language?

To run Newton Raphson Method program in C-Language, follow the below mentioned steps:

  1. Copy the Code: Press the copy button in the code block to copy the complete code.
  2. Open Turbo C++ Version 4: Open the Turbo C++ (Version 4) software on your system.
  3. Paste the code: Paste the copied code into Turbo C++’s editor.
  4. Compile the Code: Compile the program by pressing the Alt+F9 keys. This shows you the errors and warnings, if any.
  5. Run the code: Press the Ctrl+F9 keys to run the program if the program is successfully compiled.
  6. Enter the Input: Provide the required inputs or your desired inputs, if applicable.
  7. Receive the Result: Get the result of the program on the screen based on the provided inputs.

More About the Newton-Raphson Method

Discovered by: Sir Isaac Newton

Discovered Year: 1669

Modified by: Joseph Raphson in 1690 into a simple and modular formula

Newton-Raphson is the most popular method to find the exact root (solution) of a non-linear equation (f(x)=0), where f can be any function.

How does it work?

  1. It starts from a single initial guessed point (x0).
  2. Then find the derivative (slope or tangent) at that guessed point (x0).
  3. Detect the intersection point (x1) of the derivative or tangent with respect to the x-axis.
  4. Then the new intersection point is considered a new guess.
  5. The process is repeated from point 2 to point 4 until the result is accurate.

Why is the Newton-Raphson method used?

Due to quadratic convergence, it is very fast; for each iteration step, the accuracy of solution digits is almost doubled, which is why it is much faster than the Bisection method.

Where to use the Newton-Raphson method?

  • Analyze the Load Flow in an electrical power system: to calculate the voltage, power flow, and system stability in big power grids.
  • Computer Graphics & Game Development: To find out the intersection points between light rays and objects in 3D ray tracing and ray casting.
  • GPS & Aerospace: To find out the precision values of the object in GPS and Mechanics by solving the complex equations.

Limitations of Newton-Raphson method

  1. Always need derivative (f ’ (x)): Due to the derivative dependency in each method, it is complex because calculating the derivative of each function is very difficult.
  2. Initial Guess has a wide Range: If your initial guess has a wide range then the method may diverge that causes very slow speed or incorrect solution.
  3. If the slope is zero: during the calculations, if the slope is zero, then the method will fail.

Alternate ways

  1. Secant Method: If the derivative of the function is complex, then the secant method is used because the secant method doesn’t use the derivative to find the solution
  2. Brent’s Method: It is also used because it is a combination of the Newton-Raphson and the Bisection Method's best features.
License: This program's source code is open-sourced under the MIT License, while the rest of the program's text content and overall dhimanp platform and brand content remain proprietary under dhimanp.

Submit your modified code

Your submissions are published after a quick safety check (usually within 7 days).

Submissions are public. Developers can see basic device specifications to respond. Learn more

Disclaimer

The information that you shared with us will be publicly visible, and everyone can see your information. And we (dhimanp) are not responsible if anyone misuses this information. We can remove any submitted code at any time for better transparency.

Privacy Policy

If you share your data with us by submitting your code for credit, then we may show your full name, school or college name, and city name on our website.

We are not using this data to track you and are not selling this data to anyone.

If you want to remove your name from our website, you can contact us, and we will remove your name from our website. In the removal process, full credit, including your full name, school or college, city, and code, will be removed from our website.