From f4779bc04ae706b0c548e2fe6d7a59efe8b85524 Mon Sep 17 00:00:00 2001 From: Rohit Joshi <34398948+rohitjoshi21@users.noreply.github.com> Date: Sun, 15 Dec 2019 13:12:07 +0545 Subject: [PATCH] Bug Fixed in newton_raphson_method.py (#1634) * Bug Fixed * Fixed newton_raphson_method.py * Fixed newton_raphson_method.py 2 * Fixed newton_raphson_method.py 3 * Fixed newton_raphson_method.py 4 * Fixed newton_raphson_method.py 5 * Fixed newton_raphson_method.py 6 * Update newton_raphson_method.py * Update newton_raphson_method.py * # noqa: F401, F403 * newton_raphson * newton_raphson * precision: int=10 ** -10 * return float(x) * 3.1415926536808043 * Update newton_raphson_method.py * 2.23606797749979 * Update newton_raphson_method.py * Rename newton_raphson_method.py to newton_raphson.py --- arithmetic_analysis/newton_raphson.py | 40 ++++++++++++++++++++ arithmetic_analysis/newton_raphson_method.py | 34 ----------------- 2 files changed, 40 insertions(+), 34 deletions(-) create mode 100644 arithmetic_analysis/newton_raphson.py delete mode 100644 arithmetic_analysis/newton_raphson_method.py diff --git a/arithmetic_analysis/newton_raphson.py b/arithmetic_analysis/newton_raphson.py new file mode 100644 index 000000000..8aa816cd0 --- /dev/null +++ b/arithmetic_analysis/newton_raphson.py @@ -0,0 +1,40 @@ +# 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 + +from decimal import Decimal +from math import * # noqa: F401, F403 +from sympy import diff + + +def newton_raphson(func: str, a: int, precision: int=10 ** -10) -> float: + """ Finds root from the point 'a' onwards by Newton-Raphson method + >>> 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)}") diff --git a/arithmetic_analysis/newton_raphson_method.py b/arithmetic_analysis/newton_raphson_method.py deleted file mode 100644 index 646b352a9..000000000 --- a/arithmetic_analysis/newton_raphson_method.py +++ /dev/null @@ -1,34 +0,0 @@ -# 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 -from sympy import diff -from decimal import Decimal - - -def NewtonRaphson(func, a): - """ Finds root from the point 'a' onwards by Newton-Raphson method """ - while True: - c = Decimal(a) - (Decimal(eval(func)) / Decimal(eval(str(diff(func))))) - - 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 trigonometric function - # 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))