Skip to content
This repository has been archived by the owner on Nov 18, 2022. It is now read-only.

Commit

Permalink
Newton Raphson Method calculator (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaurav-shrestha committed Oct 1, 2022
1 parent 8f189b7 commit 2d88afb
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions Python/newton_raphson.py
@@ -0,0 +1,32 @@
# Newton Raphson Method

# import sympy
from sympy import *

x_symb = symbols('x')

# Taking equation as string
function = input("Enter the function: ")
iter = int(input("Enter number of iterations: "))
x_value = float(input("Enter the value of x: "))
decimal_points = int(input("Enter number after decimal points: "))

# Finding Derivative function
der_fx = Derivative(function, x_symb).doit()


# Converting Strings to SymPy Expressions
fx0_sympy = sympify(function)

print(f"Expression : \n{function}")
print(f"\nValue of the derivative : \n{der_fx} ")
for i in range(iter):
# Final Calculations
fx0 = round(fx0_sympy.subs(x_symb, x_value), decimal_points)
der_fx0 = round(der_fx.subs(x_symb, x_value), decimal_points)
x1 = round(x_value - (fx0)/der_fx0, decimal_points)

print(f"Value of f({x_value}): {fx0:.{decimal_points}f}")
print(f"Value of f'({x_value}): {der_fx0:.{decimal_points}f}")
print(f"Value of x({i+1}): {x1:.{decimal_points}f}\n")
x_value = x1

0 comments on commit 2d88afb

Please sign in to comment.