-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnewton newton.py
22 lines (18 loc) · 984 Bytes
/
newton newton.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from sympy import *
def newton(interval, function_expression, precision=0.001):
function_variable = var("x")
function = sympify(function_expression)
if function.subs(function_variable, interval[0]) * function.subs(function_variable, interval[1]) < 0:
x = (interval[0] + interval[1]) / 2
derivative_symbol = Symbol("x")
derivative = diff(function)
derivative_value = lambdify(derivative_symbol, derivative)
term = function.subs(function_variable, x) / derivative_value(x)
while abs(term) >= precision:
term = function.subs(function_variable, x) / derivative_value(x)
x = x - term
return "The value of the root is : {}".format(x)
if __name__ == '__main__':
given_function = input("please enter your function: ")
given_interval = eval(input("please enter an interval like [a, b] where a and b are numbers: ")))
print(newton(given_interval, given_function))