Skip to content

Create NewtonRaphsonMethod.py #284

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 13, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions ArithmeticAnalysis/NewtonRaphsonMethod.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Implementing Newton Raphson method in python
# Author: Haseeb

from sympy import diff
from decimal import Decimal
from math import sin, cos, exp

def NewtonRaphson(func, a):
''' Finds root from the point 'a' onwards by Newton-Raphson method '''
while True:
x = a
c = Decimal(a) - ( Decimal(eval(func)) / Decimal(eval(str(diff(func)))) )

x = c
a = c
# This number dictates the accuracy of the answer
if abs(eval(func)) < 10**-15:
return c


# Let's Execute
if __name__ == '__main__':
# Find root of trignometric fucntion
# Find value of pi
print ('sin(x) = 0', NewtonRaphson('sin(x)', 2))

# Find root of polynomial
print ('x**2 - 5*x +2 = 0', NewtonRaphson('x**2 - 5*x +2', 0.4))

# Find Square Root of 5
print ('x**2 - 5 = 0', NewtonRaphson('x**2 - 5', 0.1))

# Exponential Roots
print ('exp(x) - 1 = 0', NewtonRaphson('exp(x) - 1', 0))