DEV Community

Nitesh Thakur
Nitesh Thakur

Posted on

Newton-Raphson Method

Understanding the Newton-Raphson Method: Fast Root Finding with C

If you've ever wondered how calculators find square roots, or how engineering software solves complex equations in milliseconds — the answer often involves a technique called the Newton-Raphson Method.

Today I'll walk you through the concept, the algorithm, and a working C implementation — all from scratch.


What Problem Does It Solve?

Given a function f(x), we want to find a value of x where f(x) = 0 — called a root.

For example: what is the root of x³ - 2x - 5 = 0?

We can't solve this algebraically with a simple formula. This is where numerical methods come in.


The Core Idea

Imagine you're standing on a curve and you want to find where it crosses zero. Newton-Raphson says:

"Draw a tangent line at your current point. Where does that tangent line hit zero? That's your next guess."

Mathematically, the formula is:

x₁ = x₀ - f(x₀) / f'(x₀)
Enter fullscreen mode Exit fullscreen mode

Where:

  • x₀ is your current guess
  • f(x₀) is the function value at that guess
  • f'(x₀) is the derivative (slope) at that point
  • x₁ is your improved guess

You repeat this process until the change between guesses is smaller than your desired tolerance.


The Algorithm (Step by Step)

INPUT:  f(x), f'(x), initial guess x₀, tolerance ε, max iterations N

Step 1: Set iteration counter i = 0
Step 2: WHILE i < N:
   2.1  Calculate f(xᵢ) and f'(xᵢ)
   2.2  IF |f'(xᵢ)| < ε → derivative too small, STOP
   2.3  xᵢ₊₁ = xᵢ - f(xᵢ) / f'(xᵢ)
   2.4  error = |xᵢ₊₁ - xᵢ|
   2.5  IF error < ε → ROOT FOUND, STOP
   2.6  Set xᵢ = xᵢ₊₁, increment i
Step 3: IF i = N → "Max iterations reached"
Enter fullscreen mode Exit fullscreen mode

C Implementation

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

#define MAX_ITERATIONS 100
#define EPSILON 0.00001

// Function: x^3 - 2x - 5
double f(double x) {
    return x*x*x - 2*x - 5;
}

// Derivative: 3x^2 - 2
double f_prime(double x) {
    return 3*x*x - 2;
}

int main() {
    double x0, x1, error;
    int iteration = 0;

    printf("Newton-Raphson Method\n");
    printf("Enter initial guess: ");
    scanf("%lf", &x0);

    printf("\nIteration\t x0\t\t f(x0)\t\t x1\t\t Error\n");
    printf("----------------------------------------------------------------\n");

    do {
        if (fabs(f_prime(x0)) < EPSILON) {
            printf("Derivative too small. Method fails!\n");
            exit(1);
        }

        x1    = x0 - f(x0) / f_prime(x0);
        error = fabs(x1 - x0);

        printf("%d\t\t %.6f\t %.6f\t %.6f\t %.6f\n",
               iteration + 1, x0, f(x0), x1, error);

        x0 = x1;
        iteration++;

        if (iteration > MAX_ITERATIONS) {
            printf("Max iterations reached.\n");
            break;
        }

    } while (error > EPSILON);

    printf("\nRoot = %.6f\n", x1);
    printf("f(root) = %.6f\n", f(x1));
    printf("Iterations: %d\n", iteration);

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Sample Output

Starting with initial guess x₀ = 2:

Iteration    x0          f(x0)       x1          Error
----------------------------------------------------------------
1            2.000000    -1.000000   2.100000    0.100000
2            2.100000     0.061000   2.094568    0.005432
3            2.094568     0.000202   2.094551    0.000017
4            2.094551     0.000000   2.094551    0.000000

Root = 2.094551
f(root) = 0.000000
Iterations: 4
Enter fullscreen mode Exit fullscreen mode

Only 4 iterations to find the root with 6 decimal places of accuracy. That's the power of Newton-Raphson.


Why Is It So Fast?

Newton-Raphson has quadratic convergence — meaning the number of correct decimal places roughly doubles with every iteration. Compare this to bisection method which adds about one correct bit per iteration (linear convergence).


When It Can Fail

  • f'(x) = 0 at some point — division by zero, method breaks
  • Poor initial guess — may overshoot and diverge
  • Functions with multiple roots — may converge to a different root than expected

Key Takeaway

Newton-Raphson is one of the most elegant algorithms in numerical analysis. It combines simplicity, speed, and practicality — and it's a must-know for any CS or engineering student.

Try changing the function f(x) and f_prime(x) in the code above to solve your own equations!


Full source code and README available on GitHub: https://github.com/nt3005156/Numerical-Method-All

Tags: #numericalMethods #cprogramming #algorithms #computerScience #mathematics

Top comments (0)