2019-12-15 13:12:07 +05:45
|
|
|
# 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
|
2023-07-16 20:42:35 +05:30
|
|
|
# quickly find a good approximation for the root of a real-valued function.
|
|
|
|
# https://en.wikipedia.org/wiki/Newton%27s_method
|
2021-09-07 13:37:03 +02:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-07-16 12:19:58 +05:30
|
|
|
from sympy import diff, symbols, sympify
|
2019-12-15 13:12:07 +05:45
|
|
|
|
|
|
|
|
2023-07-16 07:52:50 +00:00
|
|
|
def newton_raphson(
|
|
|
|
func: str, start_point: float, precision: float = 10**-10
|
|
|
|
) -> float:
|
2020-09-10 16:31:26 +08:00
|
|
|
"""Finds root from the point 'a' onwards by Newton-Raphson method
|
2019-12-15 13:12:07 +05:45
|
|
|
>>> newton_raphson("sin(x)", 2)
|
|
|
|
3.1415926536808043
|
|
|
|
>>> newton_raphson("x**2 - 5*x +2", 0.4)
|
2023-07-16 12:19:58 +05:30
|
|
|
0.4384471871911696
|
2019-12-15 13:12:07 +05:45
|
|
|
>>> newton_raphson("x**2 - 5", 0.1)
|
|
|
|
2.23606797749979
|
|
|
|
>>> newton_raphson("log(x)- 1", 2)
|
|
|
|
2.718281828458938
|
2023-07-16 20:42:27 +05:30
|
|
|
>>> from scipy.optimize import newton
|
2023-07-16 17:16:21 +02:00
|
|
|
>>> all(newton_raphson("log(x)- 1", 2) == newton("log(x)- 1", 2)
|
|
|
|
... for precision in 10, 100, 1000, 10000))
|
2023-07-16 20:42:27 +05:30
|
|
|
True
|
|
|
|
>>> newton_raphson("log(x)- 1", 2, 0)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
ValueError: precision must be greater than zero
|
|
|
|
>>> newton_raphson("log(x)- 1", 2, -1)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
ValueError: precision must be greater than zero
|
2019-12-15 13:12:07 +05:45
|
|
|
"""
|
2023-07-16 13:21:38 +05:30
|
|
|
x = start_point
|
2023-07-16 07:52:50 +00:00
|
|
|
symbol = symbols("x")
|
2023-07-16 13:21:38 +05:30
|
|
|
|
2023-07-16 12:51:54 +05:30
|
|
|
# expressions to be represented symbolically and manipulated algebraically
|
2023-07-16 13:21:38 +05:30
|
|
|
expression = sympify(func)
|
|
|
|
|
|
|
|
# calculates the derivative value at the current x value
|
|
|
|
derivative = diff(expression, symbol)
|
|
|
|
|
|
|
|
max_iterations = 100
|
2023-07-16 12:19:58 +05:30
|
|
|
|
2023-07-16 13:21:38 +05:30
|
|
|
for _ in range(max_iterations):
|
|
|
|
function_value = expression.subs(symbol, x)
|
|
|
|
derivative_value = derivative.subs(symbol, x)
|
2023-07-16 12:19:58 +05:30
|
|
|
|
2023-07-16 13:21:38 +05:30
|
|
|
if abs(function_value) < precision:
|
2019-12-15 13:12:07 +05:45
|
|
|
return float(x)
|
|
|
|
|
2023-07-16 13:21:38 +05:30
|
|
|
x = x - (function_value / derivative_value)
|
2023-07-16 12:19:58 +05:30
|
|
|
|
|
|
|
return float(x)
|
|
|
|
|
2019-12-15 13:12:07 +05:45
|
|
|
|
|
|
|
# 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)}")
|