-
Notifications
You must be signed in to change notification settings - Fork 93
/
Bisection.py
53 lines (45 loc) · 1.71 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
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
########################################
# BISECTION METHOD FOR SOLVING EQUATIONS
########################################
'''The aim of this porgram is to solve equation by bisection method in given interval.
As the equatoin Identifier was not made yet so please type the
equation before execution of code in a manner that python can
understand it easily.'''
def function(interval):
#identifying the interval.
numbers = interval[1:-1].split(",")
low_limit_val = f(float(numbers[0]))
upper_limit_val = f(float(numbers[1]))
if interval[0] == "[":
low_limit = "inclusive"
if low_limit_val == 0:
print("Root of this equation is:",float(numbers[0]))
return 0
else: low_limit = "exclusive"
if interval[-1] == "]":
upper_limit = "inclusive"
if upper_limit_val == 0:
print("Root of this equation is:",float(numbers[1]))
return 0
else: upper_limit = "exclusive"
# Bisection method
if low_limit_val*upper_limit_val < 0:
a = float(numbers[0])
b = float(numbers[1])
limit = (a+b)/2
ans = f(limit)
while (str(ans)[:4] != "0.00"): #checking for two decimal places only
if ans < 0:
a = limit
elif ans > 0:
b = limit
limit = (a+b)/2
ans = f(limit)
if str(ans)[:4] == "0.00":
print("Root of this equation is:",limit)
else: print("No root exists in this interval")
def f(x):
return x**3 - x -1 #Edit this equation here if you want to try this for other equations.
######main_function#####
interval = input("Enter the interval:") #format [3,5] or (3,5)
function(interval)