Welcome to the World of Modelling and Simulation

What is Modelling?

This blog is all about system dynamics modelling and simulation applied in the engineering field, especially mechanical, electrical, and ...

A Python Program for Application of the Newton-Raphson Method

In this tutorial, we will develop a simple Python program to implement the famous Newton-Raphson algorithm. Let us consider the following function:

f(x) = exp (-0.5x) (4 - x ) - 2

We will find the roots of the above function by the Newton-Raphson approach and code them using Python. We will start with an initial guess for the solution and then change it to check for other initial guesses to see whether the solution converges or diverges. This is a very basic and fundamental application of the Newton-Raphson method to find the roots of an unknown function. In the following, Python codes are written by Anaconda to find the roots of the unknown function by the Newton-Raphson method. We begin with, x = 2 for our first initial guess.

Python Script for Newton-Raphson Method

import sympy as sp  # Importing symbolic mathematics library
import pandas as pd  # Importing pandas to work with arrays
import math # Importing mathematics library
def f(x):
    return (math.e**(-0.5*x)*(4-x))-2  # Given function 
x = sp.symbols('x')
fp = f(x).diff(x) # Derivative of the function
x_value = [2] # Initial guess
error = [1]
tol = 0.0001
MaxIter = 100
i = 0
while i <= MaxIter and abs(error[i]) > tol: # Setting convergence criteria
    x_new = x_value[i] - (f(x_value[i])/fp.subs(x,x_value[i])) # Newton-Raphson Algorithm
    x_value.append(x_new)
    ernew = (x_new - x_value[i])/x_new
    error.append(ernew)
    i+=1
solution = [[i, x_value[i], error[i]] for i in range(len(x_value))];
solution_new = pd.DataFrame(solution,columns=["No of Iterations", "x_value", "Error"])
print(solution_new)

Results:

When x = 2,

   No of Iterations            x_value                Error
0                 0                  2                    1
1                 1  0.281718171540955    -6.09929355660769
2                 2  0.776886845045375    0.637375541447737
3                 3  0.881707878928567    0.118884084386961
4                 4  0.885703241166645  0.00451094909940144
5                 5  0.885708801994023  6.27839236320089e-6

So, the algorithm converges for the initial guess, x = 2.


When x = 6,

The algorithm diverges, relative error is NAN or infinity.

When x = 8,

The algorithm again diverges, relative error is close to infinity.

As we see from the results that the algorithm converges only when x = 2, but it diverges for the other two cases. The reason behind this is the derivative of the given function is zero when x = 6 and close to zero for x = 8. Since the slope of the function reaches to zero, the algorithm fails to provide a converged solution. From this example, it is evident that for any numerical method to work for a solution, the initial condition is a very important parameter, which needs to have an educated guess.



#Python #PythonScripting #NewtonRaphson #NumericalMethod