|
| 1 | +# Implementing Newton Raphson method in Python |
| 2 | +# Author: Saksham Gupta |
| 3 | +# |
| 4 | +# The Newton-Raphson method (also known as Newton's method) is a way to |
| 5 | +# quickly find a good approximation for the root of a functreal-valued ion |
| 6 | +# The method can also be extended to complex functions |
| 7 | +# |
| 8 | +# Newton's Method - https://en.wikipedia.org/wiki/Newton's_method |
| 9 | + |
| 10 | +from sympy import diff, lambdify, symbols |
| 11 | +from sympy.functions import * # noqa: F401, F403 |
| 12 | + |
| 13 | + |
| 14 | +def newton_raphson( |
| 15 | + function: str, |
| 16 | + starting_point: complex, |
| 17 | + variable: str = "x", |
| 18 | + precision: float = 10**-10, |
| 19 | + multiplicity: int = 1, |
| 20 | +) -> complex: |
| 21 | + """Finds root from the 'starting_point' onwards by Newton-Raphson method |
| 22 | + Refer to https://docs.sympy.org/latest/modules/functions/index.html |
| 23 | + for usable mathematical functions |
| 24 | +
|
| 25 | + >>> newton_raphson("sin(x)", 2) |
| 26 | + 3.141592653589793 |
| 27 | + >>> newton_raphson("x**4 -5", 0.4 + 5j) |
| 28 | + (-7.52316384526264e-37+1.4953487812212207j) |
| 29 | + >>> newton_raphson('log(y) - 1', 2, variable='y') |
| 30 | + 2.7182818284590455 |
| 31 | + >>> newton_raphson('exp(x) - 1', 10, precision=0.005) |
| 32 | + 1.2186556186174883e-10 |
| 33 | + >>> newton_raphson('cos(x)', 0) |
| 34 | + Traceback (most recent call last): |
| 35 | + ... |
| 36 | + ZeroDivisionError: Could not find root |
| 37 | + """ |
| 38 | + |
| 39 | + x = symbols(variable) |
| 40 | + func = lambdify(x, function) |
| 41 | + diff_function = lambdify(x, diff(function, x)) |
| 42 | + |
| 43 | + prev_guess = starting_point |
| 44 | + |
| 45 | + while True: |
| 46 | + if diff_function(prev_guess) != 0: |
| 47 | + next_guess = prev_guess - multiplicity * func(prev_guess) / diff_function( |
| 48 | + prev_guess |
| 49 | + ) |
| 50 | + else: |
| 51 | + raise ZeroDivisionError("Could not find root") from None |
| 52 | + |
| 53 | + # Precision is checked by comparing the difference of consecutive guesses |
| 54 | + if abs(next_guess - prev_guess) < precision: |
| 55 | + return next_guess |
| 56 | + |
| 57 | + prev_guess = next_guess |
| 58 | + |
| 59 | + |
| 60 | +# Let's Execute |
| 61 | +if __name__ == "__main__": |
| 62 | + |
| 63 | + # Find root of trigonometric function |
| 64 | + # Find value of pi |
| 65 | + print(f"The root of sin(x) = 0 is {newton_raphson('sin(x)', 2)}") |
| 66 | + |
| 67 | + # Find root of polynomial |
| 68 | + # Find fourth Root of 5 |
| 69 | + print(f"The root of x**4 - 5 = 0 is {newton_raphson('x**4 -5', 0.4 +5j)}") |
| 70 | + |
| 71 | + # Find value of e |
| 72 | + print( |
| 73 | + "The root of log(y) - 1 = 0 is ", |
| 74 | + f"{newton_raphson('log(y) - 1', 2, variable='y')}", |
| 75 | + ) |
| 76 | + |
| 77 | + # Exponential Roots |
| 78 | + print( |
| 79 | + "The root of exp(x) - 1 = 0 is", |
| 80 | + f"{newton_raphson('exp(x) - 1', 10, precision=0.005)}", |
| 81 | + ) |
| 82 | + |
| 83 | + # Find root of cos(x) |
| 84 | + print(f"The root of cos(x) = 0 is {newton_raphson('cos(x)', 0)}") |
0 commit comments