-
Notifications
You must be signed in to change notification settings - Fork 2
/
steffensen.py
40 lines (32 loc) · 978 Bytes
/
steffensen.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
import math
def steffensen(f,init,N=100):
"""
Implementation of Steffensen's method
Parameters:
f - Function to use
init - Initialization for the algoirthm
N - Number of iterations
Return value:
p - An array containing the iterations of Steffensen's method
"""
# Aitken's Delta^2 method
def delta_squared(p):
return p[0] - ((p[1]-p[0])**2) / (p[2]-2*p[1]+p[0])
# p[i] gives you the values in the i-th step
p = [[init]]
for i in range(0,N):
p[i].append( f(p[i][0]) ) # p_1^(i)
p[i].append( f(p[i][1]) ) # p_2^(i)
p.append([ delta_squared(p[i]) ]) # Initialization for next step
return p
# f(x) = cos(x)
def f(x):
return math.cos(x)
# Start iteration
p = steffensen(f,1,2)
for i in range(0,len(p)):
for j in range(0,len(p[i])):
if p[i][j] > 0:
print(f"p_{j}^{i} {p[i][j]}")
else:
print(f"p_{j}^{i} {p[i][j]}")