Newton Raphson method in Python

The Newton-Raphson method in Python is like a treasure map for finding the roots of tricky math problems. It takes a guess, improves it step by step, and unveils the hidden "X marks the spot" where the function equals zero.

In various fields of science, engineering, and finance, finding the roots of equations is a common problem. The Newton-Raphson Method is a popular numerical technique used to tackle this challenge. By iterative refining estimates, it quickly converges to the root of an equation. 

This blog aims to explain this powerful method in a way that anyone can understand, regardless of their mathematical background or programming expertise.

newton raphson method python, f x

Newton-Raphson Method Explained

The Newton-Raphson Method is a powerful numerical technique used to find the roots of a given equation. At its core, it relies on an iterative formula that refines the estimation of the root with each iteration until it converges to a satisfactory solution. This iterative method is particularly effective when You have an excellent initial guess for the root, and it can quickly provide highly accurate results.

The key idea behind the Newton's method is to use the tangent line at a particular point on the curve of the function and find its x-intercept, which corresponds to the estimated root. The formula for each iteration is as follows:

x_{n+1} = x_n - f(x_n) / f'(x_n)


  • x_{n+1} is the next estimation of the root,
  • x_n is the current estimation of the root,
  • f(x_n) is the value of the function at x_n, and
  • f'(x_n) is the derivative of the function at x_n.

By repeating this process, the method iteratively refines the estimation, bringing it closer to the actual root.

Key Steps for Implementing the Newton-Raphson Method in Python:

1. Define the Target Function:

Start your program by defining the function for which you want to find the root. Let's say we want to find the root of the equation f(x) = x^3 - 5x^2 + 2x + 1. Define this function in Python:

def target_function(x):
    return x**3 - 5*x**2 + 2*x + 1

2. Calculate the Derivative of the Function:

Next, we need to calculate the derivative of the target function, as it is required in the Newton-Raphson formula. We can use numerical differentiation or provide an analytical derivative if possible. For our example, let's Calculate the same function derivative analytically:

def derivative_function(x):
    return 3*x**2 - 10*x + 2

3. Iterate Using the Newton-Raphson Formula:

Now, let's implement the first iteration of the process using the Newton-Raphson formula. We'll set an initial guess for the root, and then update it iteratively until the desired level of accuracy is achieved:

def newton_raphson_method(initial_guess, tolerance, max_iterations):
    x_n = initial_guess
    for iteration in range(max_iterations):
        f_x = target_function(x_n)
        f_prime_x = derivative_function(x_n)

        if abs(f_x) < tolerance:

        x_n = x_n - f_x / f_prime_x

    return x_n

In the above code, the `initial_guess` parameter is your starting point for finding the root, the `tolerance` determines the range between the error and desired accuracy, and the `max_iterations` limits the number of iterations to avoid infinite loops.

4. Use the Function to Find the Root:

Finally, call the `newton_raphson_method` function with your input and desired parameters to find the root:

initial_guess = 2.0
tolerance = 1e-6
max_iterations = 100

root = newton_raphson_method(initial_guess, tolerance, max_iterations)
print("Estimated Root:", root)

By running the above code, you can assume you will find the estimated root of the equation f(x) = x^3 - 5*x^2 + 2*x + 1.

By following these steps, for example, you can effectively implement the Newton-Raphson Method in the Python program to find the roots of various equations with high precision.


In conclusion, the Newton-Raphson Method is a valuable tool for finding the roots of equations with high precision values. Following this comprehensive guide and implementing it in Python will give you a deeper understanding of the root of equation-finding techniques and enhance your problem-solving skills.

You can also check these blogs:

  1. How to Draw a Circle in Python?
  2. How to print a list in Python
  3. Get current year in Python
  4. Jaccard similarity in Python
  5. Python count unique values in column
  6. rstrip vs strip in Python: Trimming whitespaces made easy
  7. Python import class from another file
  8. Apply function to list in Python
  9. Get class name as string in Python
  10. How to append multiple elements in a list in Python?