Python Program Newton Raphson (NR) Method (with Output)
This program implements Newton Raphson method for finding real root of nonlinear function in python programming language.
In this python program, x0 is initial guess, e is tolerable error, f(x) is non-linear function whose root is being obtained using Newton Raphson method.
Python Source Code: Newton Raphson Method
# Defining Function
def f(x):
return x**3 - 5*x - 9
# Defining derivative of function
def g(x):
return 3*x**2 - 5
# Implementing Newton Raphson Method
def newtonRaphson(x0,e,N):
print('\n\n*** NEWTON RAPHSON METHOD IMPLEMENTATION ***')
step = 1
flag = 1
condition = True
while condition:
if g(x0) == 0.0:
print('Divide by zero error!')
break
x1 = x0 - f(x0)/g(x0)
print('Iteration-%d, x1 = %0.6f and f(x1) = %0.6f' % (step, x1, f(x1)))
x0 = x1
step = step + 1
if step > N:
flag = 0
break
condition = abs(f(x1)) > e
if flag==1:
print('\nRequired root is: %0.8f' % x1)
else:
print('\nNot Convergent.')
# Input Section
x0 = input('Enter Guess: ')
e = input('Tolerable Error: ')
N = input('Maximum Step: ')
# Converting x0 and e to float
x0 = float(x0)
e = float(e)
# Converting N to integer
N = int(N)
#Note: You can combine above three section like this
# x0 = float(input('Enter Guess: '))
# e = float(input('Tolerable Error: '))
# N = int(input('Maximum Step: '))
# Starting Newton Raphson Method
newtonRaphson(x0,e,N)
Newton Raphson Python Output
Enter Guess: 2 Tolerable Error: 0.000001 Maximum Step: 10 *** NEWTON RAPHSON METHOD IMPLEMENTATION *** Iteration-1, x1 = 3.571429 and f(x1) = 18.696793 Iteration-2, x1 = 3.009378 and f(x1) = 3.207103 Iteration-3, x1 = 2.864712 and f(x1) = 0.185915 Iteration-4, x1 = 2.855236 and f(x1) = 0.000771 Iteration-5, x1 = 2.855197 and f(x1) = 0.000000 Required root is: 2.85519654
Example
In this program we will solve f(x) = 3*cos(x) - ex using python. Everything is similar as above python program for Newton Raphson method. The difference here is import math
and use of mathematical functions.
import math
# Defining Function
def f(x):
return 3*math.cos(x) - math.exp(x)
# Defining derivative of function
def g(x):
return -3*math.sin(x) - math.exp(x)
# Implementing Newton Raphson Method
def newtonRaphson(x0,e,N):
print('\n\n*** NEWTON RAPHSON METHOD IMPLEMENTATION ***')
step = 1
flag = 1
condition = True
while condition:
if g(x0) == 0.0:
print('Divide by zero error!')
break
x1 = x0 - f(x0)/g(x0)
print('Iteration-%d, x1 = %0.6f, f(x1) = %0.6f and g(x1) = %0.6f' % (step, x1, f(x1), g(x1)))
x0 = x1
step = step + 1
if step > N:
flag = 0
break
condition = abs(f(x1)) > e
if flag==1:
print('\nRequired root is: %0.8f' % x1)
else:
print('\nNot Convergent.')
# Input Section
x0 = input('Enter Guess: ')
e = input('Tolerable Error: ')
N = input('Maximum Step: ')
# Converting x0 and e to float
x0 = float(x0)
e = float(e)
# Converting N to integer
N = int(N)
#Note: You can combine above three section like this
# x0 = float(input('Enter Guess: '))
# e = float(input('Tolerable Error: '))
# N = int(input('Maximum Step: '))
# Starting Newton Raphson Method
newtonRaphson(x0,e,N)
The output of the above program is:
Enter Guess: 0.5 Tolerable Error: 0.00001 Maximum Step: 10 *** NEWTON RAPHSON METHOD IMPLEMENTATION *** Iteration-1, x1 = 0.818765, f(x1) = -0.218326 and g(x1) = -4.458605 Iteration-2, x1 = 0.769798, f(x1) = -0.005174 and g(x1) = -4.247299 Iteration-3, x1 = 0.768579, f(x1) = -0.000003 and g(x1) = -4.242044 Required root is: 0.76857930