-
Notifications
You must be signed in to change notification settings - Fork 0
/
bisection_method.py
64 lines (51 loc) · 1.15 KB
/
bisection_method.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
64
"""
-Problem:
The Bernoulli equation to an open-channel fluid flow with a small protuberance is given.
Where:
Q = 1.2m^2/s (volumetric flow)
g = 9.81m/s^2 (gravity)
b = 1.8m (channel width)
ho = 0.6m (upward water level)
hs = 0.075m (size of protuberance)
h = Water level above the protuberance
Determine value of h
-Solution approach:
Bisection method
"""
import numpy as np
#Constants after some algebraic manipulations
b = 0.5879239501
c = 0.02265262204
def f(x):
"""
Given Bernoulli equation
"""
return pow(x, 3) -b*pow(x, 2) + c
def bisection_method():
#Setting initial conditions up
a = -0.05
b = 1
#Defining initial conditions for bisection method
xr = (a + b )/ 2
f_a = f(a)
f_b = f(b)
f_xr = f(xr)
mul = f_a * f_xr
error = 1
#Doing Bisection Method iterations
while error != 0:
if mul > 0: a = xr
if mul < 0: b = xr
prev_xr = xr
xr = (a + b)/2
f_a = f(a)
f_b = f(b)
f_xr = f(xr)
mul = f_a * f_xr
error = abs(((xr - prev_xr )/ xr) * 100)
return xr
def main():
#Displaying result
print(f"Value of h: {bisection_method()}")
if __name__ == '__main__':
main()