/
numerical-methods.py
executable file
·142 lines (128 loc) · 4.4 KB
/
numerical-methods.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env python
# This code was written by Zahir Jacobs:
# http://zahirj.wordpress.com/2009/04/04/complete-listing-of-python-code-for-selected-root-finding-methods/
import math
def swap_points(x):
s = []
s = x
s.sort()
f = s[1]
sn = s[2]
t = s[0]
s[0] = f
s[1] = sn
s[2] = t
return s
def sub_dict(somedict, somekeys, default=math):
return dict([ (k, somedict.get(k, default)) for k in somekeys ])
def evaluate(func, x):
safe_list = ['math','acos', 'asin', 'atan', 'atan2', 'ceil', 'cos', 'cosh', 'de grees', 'e', 'exp', 'fabs', 'floor', 'fmod', 'frexp', 'hypot', 'ldexp', 'log', 'log10', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh'] #use the list to filter the local namespace
safe_dict = sub_dict(locals(),safe_list)
safe_dict['abs'] = abs
safe_dict['x']=x
return eval(compile(func,"",'eval'),{"__builtins__":{}},safe_dict)
def mullers_method(func, a, b, r, maxSteps=30):
print_header("Muller's method", func)
x = [a,b,r]
for loopCount in range(maxSteps):
x = swap_points(x)
y = evaluate(func,float(x[0])),evaluate(func,float(x[1])),evaluate(func,float(x[2]))
h1 = x[1]-x[0]
h2 = x[0]-x[2]
lam = h2/h1
c = y[0]
a = (lam*y[1] - y[0]*((1.0+lam))+y[2])/(lam*h1**2.0*(1+lam))
b = (y[1] - y[0] - a*((h1)**2.0))/(h1)
if b > 0:
root = x[0] - ((2.0*c)/(b+ (b**2 - 4.0*a*c)**0.5))
else:
root = x[0] - ((2.0*c)/(b- (b**2 - 4.0*a*c)**0.5))
print "a = %.5f b = %.5f c = %.5f root = %.5f " % (a,b,c,root)
print "Current approximation is %.9f" % root
if abs(evaluate(func,float(root))) > x[0]:
x = [x[1],x[0],root]
else:
x = [x[2],x[0],root]
x = swap_points(x)
print_end()
def bisection(func, a, b, maxSteps=30):
print_header("Bisection Method", func)
initial = evaluate(func,float(a))
for loopCounter in range(maxSteps):
midPoint = a + (b-a)/2.0
result = evaluate(func,float(midPoint))
print "Accuracy is within %.9f " % (abs(b-a)/2.0)
if (result == 0 or ( (abs(b-a)/2.0) > 0)):
a = midPoint
initial = result
else:
b = midPoint
print_end()
def secant(func, a, b, maxSteps=30, tolerance=0.0001):
print_header("secant method",func)
if (abs(evaluate(func,float(a))) < abs(evaluate(func,float(b)))):
t = b
b = a
a = t
print "a = %.9f b = %.9f" % (a,b)
for loopCount in range(maxSteps):
p = b - (evaluate(func,float(a)) * ((a-b)/(evaluate(func,float(a))-evaluate(func,float(b)))))
print "Current approximation is %.9f" % p
if (abs(evaluate(func,float(p))) < tolerance):
print "Root is %.9f (%d iterations)" % (p,loopCount+1)
return
a = b
b = p
print "Root find stopped at %.9f" % p
print_end()
def regula_falsi(func, a, b, maxSteps=30, tolerance=0.0001):
print_header("regula falsi (false position)",func)
p = 0.0
for loopCount in range(maxSteps):
p = b - (evaluate(func,float(b)) * ((a-b)/(evaluate(func,float(a))-evaluate(func,float(b)))))
print "Current approximation is %.9f" % p
if (math.copysign(evaluate(func,float(a)),evaluate(func,float(b))) != evaluate(func,float(a))):
b = p
else:
a = p
if (abs(evaluate(func,float(p))) < tolerance):
print "Root is %.9f (%d iterations)" % (p,int(loopCount))
return
print "Root find cancelled at %.9f" % p
print_end()
def print_header(t, f):
print "\n"
print "-" * 50
print "Using %s to solve %s" % (t,f)
print "-" * 50
print "\n"
def print_end():
print "-" * 50
def fixed_point(func, initialApproximation, maxSteps=30, tolerance=0.0001):
print_header("fixed point iteration",func)
p = initialApproximation
loopCounter = 1
for loopCounter in range(maxSteps):
oldP = evaluate(func,float(p))
print "Current approximation is %.9f" % oldP
if (abs(p - oldP) < tolerance):
print "Approximate root is %.9f (found in %d steps)" % (oldP,int(loopCounter))
return
p = oldP
print_end()
def newton_raphson(func, derFunc, initialApproximation, maxSteps=30, tolerance=0.0001):
print_header("newton/raphson",func + " with " + defFunc + " as derivative")
for loopCounter in range(maxSteps):
p = initialApproximation - (evaluate(func,float(initialApproximation))/evaluate(derFunc,float(initialApproximation)))
if (abs(p - initialApproximation) < tolerance):
print "Approximate root is %.9f (found in %i steps)" %(p,loopCounter)
break
print "Current approximation %.9f" % initialApproximation
initialApproximation = p
print_end()
if __name__ == '__main__':
try:
print "in __main__"
mullers_method("math.cos(x) - x", 0.0, 0.5, 1.0)
finally:
print "Fully done."