Find Roots using Newton Raphson’s Method

AIM: Write a Python program to find the roots of non-linear equation using Newton Raphson’s method.

Source Code:
#Program for Raphson Method

def f(x):
  return x**3 - 5*x - 9

def g(x):
  return 3*x**2 - 5

# Implementing Newton Raphson Method
def newtonRaphson(x0,e,N):
  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("Required Root is: %0.8f"% x1)
  else:
    print("Not Convergent.")

x0 = float(input("Enter Guess:"))
e = float(input("Tolerable Error:"))
N = int(input("Maximum Step:"))
newtonRaphson(x0,e,N)
Output:
Enter Guess:2
Tolerable Error:0.0001
Maximum Step:10
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