-
Notifications
You must be signed in to change notification settings - Fork 1
/
bisection.py
23 lines (19 loc) · 1.02 KB
/
bisection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from sympy import *
def bisection(interval, function_expression, precision=0.01):
variable = var("x")
function = sympify(function_expression)
if function.subs(variable, interval[0]) * function.subs(variable, interval[1]) >= 0:
return "this function has no roots in the given interval."
initiation_point = interval[0]
while interval[1] - interval[0] >= precision:
initiation_point = (interval[0] + interval[1]) / 2
if function.subs(variable, initiation_point) == 0.0:
break
if function.subs(variable, initiation_point) * function.subs(variable, interval[0]) < 0:
interval[1] = initiation_point
else:
interval[0] = initiation_point
return "The value of the root is : {}".format(initiation_point)
given_interval = eval(input("please enter and interval like [a, b] where a and b are numbers: "))
given_function = input("please enter a function like 2 * x + 3 * sin(x)")
print(bisection(given_interval, given_function))