-
Notifications
You must be signed in to change notification settings - Fork 1
/
bisection.py
63 lines (48 loc) · 1.88 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
54
55
56
57
58
59
60
61
62
63
###########################################
'''
File Name: bisection.py
Author: Mohammad Yasir
Module to perform root-finding calculation via the bisection method.
'''
###########################################
from sympy import symbols, lambdify
import math
def bisection():
'''
The bisection() method does exactly what it sounds like.
Returns:
True: If the calculations were successful. OR
False: If the calculations did not succeed. For instnace, illegal inputs lead to a False result. Note that the function returns True even if convergence is not achieved.
'''
print("\n")
# Set up the function that must be solved.
x = symbols('x')
functionString = str(input("Enter the function in the form \'x**3 - 5*x - 9\': "))
expression = lambdify(x, functionString, "math")
# Necessary inputs.
maxIterations = int(input("Enter maximum number of iterations: "))
accuracy = float(input("Enter the desired tolerance: "))
x1 = float(input("Enter the first guess: "))
x2 = float(input("Enter the second guess: "))
if expression(x1) * expression(x2) > 0:
print("The expression has the same sign on both the given points. Please choose another pair of starting points.\nPress enter to cotinue")
delay = input()
return False
count = 0
fx3 = 0.0
x3 = 0.0
# Print headers
print("\nS. No.\tx1\t\tx2\t\tx3\t\tf(x3)")
while True:
x3 = (x1 + x2) / 2
fx3 = expression(x3)
print("{0}\t{1:.8f}\t{2:.8f}\t{3:.8f}\t{4:.8f}".format(count+1, x1, x2, x3, fx3))
if expression(x1) * fx3 < 0:
x2 = x3
else:
x1 = x3
count += 1
if (abs(fx3) < accuracy) or (count >= maxIterations):
break
print(f"After {count} iterations, the root of the given equation is\n x = {x3}\nf(x) = {fx3}\n\n")
return True