2018-10-08 06:03:53 +00:00
|
|
|
# Implementing Newton Raphson method in Python
|
2018-04-13 15:25:47 +00:00
|
|
|
# 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__':
|
2018-10-08 06:03:53 +00:00
|
|
|
# Find root of trigonometric function
|
|
|
|
# Find value of pi
|
2018-04-13 15:25:47 +00:00
|
|
|
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))
|
|
|
|
|
2018-10-08 06:03:53 +00:00
|
|
|
# Exponential Roots
|
2018-04-13 15:25:47 +00:00
|
|
|
print ('exp(x) - 1 = 0', NewtonRaphson('exp(x) - 1', 0))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|