-
Notifications
You must be signed in to change notification settings - Fork 0
/
lectureCode_Lec4-roots.py
82 lines (67 loc) · 1.92 KB
/
lectureCode_Lec4-roots.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# From Lecture 4, Understanding Root Finding
# root code
def findRoot1(x, power, epsilon):
low = 0
high = x
ans = (high+low)/2.0
while abs(ans**power - x) > epsilon:
if ans**power < x:
low = ans
else:
high = ans
ans = (high+low)/2.0
return ans
##print findRoot1(25.0, 2, .001)
##print findRoot1(27.0, 3, .001)
##print findRoot1(-27.0, 3, .001)
# so can't find cube root of negative number
def findRoot2(x, power, epsilon):
if x < 0 and power%2 == 0:
return None
# can't find even powered root of negative number
low = min(0, x)
high = max(0, x)
ans = (high+low)/2.0
while abs(ans**power - x) > epsilon:
if ans**power < x:
low = ans
else:
high = ans
ans = (high+low)/2.0
return ans
##print findRoot2(25.0, 2, .001)
##print findRoot2(27.0, 3, .001)
##print findRoot2(-27.0, 3, .001)
##
##print findRoot2(0.25, 2, .001)
##print findRoot2(-0.125, 3, .001)
def findRoot3(x, power, epsilon):
if x < 0 and power%2 == 0:
return None
# can't find even powered root of negative number
low = min(-1.0, x)
high = max(1.0, x)
ans = (high+low)/2.0
while abs(ans**power - x) > epsilon:
if ans**power < x:
low = ans
else:
high = ans
ans = (high+low)/2.0
return ans
print findRoot3(25.0, 2, .001)
print findRoot3(27.0, 3, .001)
print findRoot3(-27.0, 3, .001)
print findRoot3(0.25, 2, .001)
print findRoot3(-0.125, 3, .001)
def testFindRoot():
epsilon = 0.0001
for x in (0.25, -0.25, 2, -2, 8, -8):
for power in range(1,4):
print('Testing x = ' + str(x) +\
' and power = ' + str(power))
res = findRoot3(x, power, epsilon)
if res == None:
print(' No root')
else:
print(' ' + str(res**power) + ' ~= ' + str(x))