2019-12-15 07:27:07 +00:00
|
|
|
# Implementing Newton Raphson method in Python
|
|
|
|
# Author: Syed Haseeb Shah (github.com/QuantumNovice)
|
|
|
|
# The Newton-Raphson method (also known as Newton's method) is a way to
|
|
|
|
# quickly find a good approximation for the root of a real-valued function
|
2021-09-07 11:37:03 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2019-12-15 07:27:07 +00:00
|
|
|
from decimal import Decimal
|
|
|
|
from math import * # noqa: F401, F403
|
2020-07-06 07:44:19 +00:00
|
|
|
|
2019-12-15 07:27:07 +00:00
|
|
|
from sympy import diff
|
|
|
|
|
|
|
|
|
2020-12-23 09:52:43 +00:00
|
|
|
def newton_raphson(
|
2021-09-07 11:37:03 +00:00
|
|
|
func: str, a: float | Decimal, precision: float = 10 ** -10
|
2020-12-23 09:52:43 +00:00
|
|
|
) -> float:
|
2020-09-10 08:31:26 +00:00
|
|
|
"""Finds root from the point 'a' onwards by Newton-Raphson method
|
2019-12-15 07:27:07 +00:00
|
|
|
>>> newton_raphson("sin(x)", 2)
|
|
|
|
3.1415926536808043
|
|
|
|
>>> newton_raphson("x**2 - 5*x +2", 0.4)
|
|
|
|
0.4384471871911695
|
|
|
|
>>> newton_raphson("x**2 - 5", 0.1)
|
|
|
|
2.23606797749979
|
|
|
|
>>> newton_raphson("log(x)- 1", 2)
|
|
|
|
2.718281828458938
|
|
|
|
"""
|
|
|
|
x = a
|
|
|
|
while True:
|
|
|
|
x = Decimal(x) - (Decimal(eval(func)) / Decimal(eval(str(diff(func)))))
|
|
|
|
# This number dictates the accuracy of the answer
|
|
|
|
if abs(eval(func)) < precision:
|
|
|
|
return float(x)
|
|
|
|
|
|
|
|
|
|
|
|
# Let's Execute
|
|
|
|
if __name__ == "__main__":
|
|
|
|
# Find root of trigonometric function
|
|
|
|
# Find value of pi
|
|
|
|
print(f"The root of sin(x) = 0 is {newton_raphson('sin(x)', 2)}")
|
|
|
|
# Find root of polynomial
|
|
|
|
print(f"The root of x**2 - 5*x + 2 = 0 is {newton_raphson('x**2 - 5*x + 2', 0.4)}")
|
|
|
|
# Find Square Root of 5
|
|
|
|
print(f"The root of log(x) - 1 = 0 is {newton_raphson('log(x) - 1', 2)}")
|
|
|
|
# Exponential Roots
|
|
|
|
print(f"The root of exp(x) - 1 = 0 is {newton_raphson('exp(x) - 1', 0)}")
|