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 | # Feigenbaum constant calculation
# FB - 201011151
# http://en.wikipedia.org/wiki/Feigenbaum_constant
# Source:
# http://keithbriggs.info/documents/how-to-calc.pdf
import math
def b(a, k):
if k == 0:
return 0
else:
return a - math.pow(b(a, k - 1), 2)
def b_prime(a, k):
if k == 0:
return 0
else:
return 1 - 2 * b_prime(a, k - 1) * b(a, k - 1)
# main
print 'Feigenbaum constant calculation:'
maxIt = 10
maxItJ = 10
ai_1 = 1
ai_2 = 0
di_1 = 3.2
print 'i', 'di'
for i in range(2, maxIt):
ai = ai_1 + (ai_1 - ai_2) / di_1
for j in range(maxItJ):
ai = ai - b(ai, math.pow(2, i)) / b_prime(ai, math.pow(2, i))
#
di = (ai_1 - ai_2) / (ai - ai_1)
print i, di
# prepare for the next iteration
di_1 = di
ai_2 = ai_1
ai_1 = ai
|
This version does not use recursive functions, so it is way faster: