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