Flexiple Logo
  1. Home
  2. Blogs
  3. Python
  4. Newton Raphson method in Python

Newton Raphson method in Python

Author image

Harsh Pandey

Software Developer

Published on Wed Mar 27 2024

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 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-Raphson 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:

xn+1 = xn - f(xn) / f'(xn)

where:

  • xn+1 is the next estimation of the root,
  • xn is the current estimation of the root,
  • f(xn) is the value of the function at xn, and
  • f'(xn) is the derivative of the function at xn.

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 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 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 iteration 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:
                break
    
            x_n = x_n - f_x / f_prime_x
    
        return x_n
    
  4. Use the Function to Find the Root:

    Finally, call the newton_raphson_method function with your 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 will find the estimated root of the equation f(x) = x^3 - 5*x^2 + 2*x + 1.

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

Conclusion

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

Related Blogs

Browse Flexiple's talent pool

Explore our network of top tech talent. Find the perfect match for your dream team.