-
Notifications
You must be signed in to change notification settings - Fork 0
/
NumericalRoot.py
258 lines (185 loc) · 7.92 KB
/
NumericalRoot.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#Auxiliary Methods
def VerificationBolzanoWeierstrassTheorem(function, a, b):
'''Check if exist a root in interval ([a,b])
Return True or False
Reference: https://en.wikipedia.org/wiki/Bolzano%E2%80%93Weierstrass_theorem
Required inputs:
'function' is analyzed function
'a' is left edge of interval
'b' is right edge of interval
'xk' if a value value within interval'''
if function(a) * function(b) <= 0:
return True
else:
return False
def BinaryChoice(function, a, b, xk):
'''Selects the interval ([a,xk] or [xk,b]) that contains the root
Return a, xk or xk, b
Reference: https://en.wikipedia.org/wiki/Binary_search_algorithm
Required inputs:
'function' is analyzed function
'a' is left edge of interval
'b' is right edge of interval
'xk' if a value value within interval'''
if VerificationBolzanoWeierstrassTheorem(function, a, xk):
return a, xk
else:
return xk, b
#Interval Methods
def Bisection(function,a,b,i_max=1e6,e_abs=1e-6,e_rel=1e-6,logs=False):
'''Calculates the numerical root by the bisection method
Reference: https://en.wikipedia.org/wiki/Bisection_method
Required inputs:
'function' is analyzed function
'a' is left edge of interval
'b' is right edge of interval
Optional keyword arguments:
Halting problem. Set 'False' to don't use the stop criterion
'i_max' is maximum number of iterations of the method
'e_abs' is a absolute error between approximate root and 0
'e_rel' is a relative error between current root approximation and previous root approximation
Show data. Set True for for show data for each iteration
'logs'
'''
i = i_max
erk = 1
xk_1 = a
if VerificationBolzanoWeierstrassTheorem(function, a, b):
while ((i > 0) and (erk > e_rel) and (abs(function(xk_1)) > e_abs)):
#Bisection update
xk = (a + b)/2
a, b = BinaryChoice(function, a, b, xk)
#
i = i - 1
erk = abs((xk_1 - xk) / xk)
if logs:
print({'root':xk, 'iterations':i_max - i, 'e_rel':erk, 'e_abs':abs(function(xk))})
xk_1 = xk
return {'root':xk, 'iterations':i_max - i, 'e_rel':erk, 'e_abs':abs(function(xk))}
else:
print('No root found in interval')
def FalsePosition(function,a,b,i_max=1e6,e_abs=1e-6,e_rel=1e-6,logs=False):
'''Calculates the numerical root by the false position method
Reference: https://en.wikipedia.org/wiki/Regula_falsi
Required inputs:
'function' is analyzed function
'a' is left edge of interval
'b' is right edge of interval
Optional keyword arguments:
Halting problem. Set 'False' to don't use the stop criterion
'i_max' is maximum number of iterations of the method
'e_abs' is a absolute error between approximate root and 0
'e_rel' is a relative error between current root approximation and previous root approximation
Show data. Set True for for show data for each iteration
'logs'
'''
i = i_max
erk = 1
xk_1 = a
if VerificationBolzanoWeierstrassTheorem(function, a, b):
while ((i > 0) and (erk > e_rel) and (abs(function(xk_1)) > e_abs)):
#FalsePosition update
m_inclination = (function(b) - function(a)) / (b - a)
y_intersection = function(b) - (m_inclination * b)
xk = (-y_intersection)/m_inclination
a, b = BinaryChoice(function, a, b, xk)
#
i = i - 1
erk = abs((xk_1 - xk) / xk)
if logs:
print({'root':xk, 'iterations':i_max - i, 'e_rel':erk, 'e_abs':abs(function(xk))})
xk_1 = xk
return {'root':xk, 'iterations':i_max - i, 'e_rel':erk, 'e_abs':abs(function(xk))}
else:
print('No root found in interval')
#Open Methods
def FixedPoint(function,phi,x0,i_max=1e6,e_abs=1e-6,e_rel=1e-6,logs=False):
'''Calculates the numerical root by the fixed point method
Reference: https://en.wikipedia.org/wiki/Fixed-point_iteration
Required inputs:
'function' is analyzed function
'phi' is phi function (check reference)
'x0' is initial value
Optional keyword arguments:
Halting problem. Set 'False' to don't use the stop criterion
'i_max' is maximum number of iterations of the method
'e_abs' is a absolute error between approximate root and 0
'e_rel' is a relative error between current root approximation and previous root approximation
Show data. Set True for for show data for each iteration
'logs'
'''
i = i_max
erk = 1
xk_1 = x0
while ((i > 0) and (erk > e_rel) and (abs(function(xk_1)) > e_abs)):
#FixedPoint update
xk = phi(xk_1)
#
i = i - 1
erk = abs((xk_1 - xk) / xk)
if logs:
print({'root':xk, 'iterations':i_max - i, 'e_rel':erk, 'e_abs':abs(function(xk))})
xk_1 = xk
return {'root':xk, 'iterations':i_max - i, 'e_rel':erk, 'e_abs':abs(function(xk))}
def NewtonRaphson(function,derivative_function,x0,i_max=1e6,e_abs=1e-6,e_rel=1e-6,logs=False):
'''Calculates the numerical root by the Newton-Raphson method
Reference: https://en.wikipedia.org/wiki/Newton's_method
Required inputs:
'function' is analyzed function
'derivative_function' is derivative function (check reference)
'x0' is initial value
Optional keyword arguments:
Halting problem. Set 'False' to don't use the stop criterion
'i_max' is maximum number of iterations of the method
'e_abs' is a absolute error between approximate root and 0
'e_rel' is a relative error between current root approximation and previous root approximation
Show data. Set True for for show data for each iteration
'logs'
'''
i = i_max
erk = 1
xk_1 = x0
while ((i > 0) and (erk > e_rel) and (abs(function(xk_1)) > e_abs)):
#NewtonRaphson update
xk = xk_1 - (function(xk_1)/derivative_function(xk_1))
#
i = i - 1
erk = abs((xk_1 - xk) / xk)
if logs:
print({'root':xk, 'iterations':i_max - i, 'e_rel':erk, 'e_abs':abs(function(xk))})
xk_1 = xk
return {'root':xk, 'iterations':i_max - i, 'e_rel':erk, 'e_abs':abs(function(xk))}
def Secant(function,x0,d=0.00001,x_1=False,i_max=1e6,e_abs=1e-6,e_rel=1e-6,logs=False):
'''Calculates the numerical root by the secant method
Reference: https://en.wikipedia.org/wiki/Secant_method
Required inputs:
'function' is analyzed function
'x0' is initial value
'd' is infinitesimal value
'x_1' is second value. If False, x_1 = x0 + d
Optional keyword arguments:
Halting problem. Set 'False' to don't use the stop criterion
'i_max' is maximum number of iterations of the method
'e_abs' is a absolute error between approximate root and 0
'e_rel' is a relative error between current root approximation and previous root approximation
Show data. Set True for for show data for each iteration
'logs'
'''
i = i_max
erk = 1
xk_1 = x0
if x_1 == False:
xk_2 = xk_1 + d
else:
xk_2 = x_1
while ((i > 0) and (erk > e_rel) and (abs(function(xk_1)) > e_abs)):
# Secant update
xk = xk_1 - (function(xk_1)*(xk_2 - xk_1)) / (function(xk_2) - function(xk_1))
#
i = i - 1
erk = abs((xk_1 - xk) / xk)
if logs:
print({'root':xk, 'iterations':i_max - i, 'e_rel':erk, 'e_abs':abs(function(xk))})
xk_2 = xk_1
xk_1 = xk
return {'root':xk, 'iterations':i_max - i, 'e_rel':erk, 'e_abs':abs(function(xk))}