import math def function beta H2 50 return beta math tan beta H2 def t

 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
65
66
67
68
import math
def function(beta):
L = 1.0
H2 = 50.0
return (beta * math.tan(beta * L)) - H2
def testFunction(x):
L = 1.0
return math.cos(2 * L * x)
def expectedNumberIterations(upperBound, lowerBound, threshold):
return ((math.log10(upperBound - lowerBound)) - (math.log10(threshold))) \
/ (math.log10(2))
def findRoots():
lowerBound = 0.0
upperBound = 18.0
rangeThreshold = math.pow(10, -3)
root = None
roots = []
stepSize = upperBound - lowerBound
k = 1.0
while(stepSize >= rangeThreshold):
rangeLowerBound = lowerBound
rangeUpperBound = rangeLowerBound + stepSize
while (rangeUpperBound <= upperBound):
root = bisectionMethod(rangeLowerBound, rangeUpperBound)
if((root is not None) and ((root in roots) != True) and \
(testFunction(root) != -1)):
roots.insert(len(roots), root)
root = None
rangeLowerBound = rangeUpperBound
rangeUpperBound = rangeUpperBound + stepSize
stepSize = ((lowerBound + upperBound) / math.pow(2, k)) - lowerBound
k = k + 1
return roots
def bisectionMethod(lowerBound, upperBound):
threshold = math.pow(10, -30)
absoluteError = 1
k = 0.0
maxNumberIter = round(expectedNumberIterations(upperBound, lowerBound, \
threshold))
if ((function(lowerBound) * function(upperBound)) < 0):
while ((absoluteError >= threshold) or (k <= maxNumberIter)):
midPoint = (upperBound + lowerBound) / 2.0
if ((function(lowerBound) * function(midPoint)) < 0.0):
upperBound = midPoint
elif ((function(lowerBound) * function(midPoint)) > 0.0):
lowerBound = midPoint
else:
return midPoint
absoluteError = math.fabs((((upperBound + lowerBound) / 2.0) - \
(midPoint)) / ((upperBound + lowerBound) / 2.0))
k = k + 1.0
return midPoint
else:
return None