126
5 Some More Python Essentials
Google), and you probably have to look into more details about how Taylor series
are handled with sympy.
To your comfort, this is a very typical situation for engineers and scientists. They
need to solve a problem, but do not (yet!) have the required knowledge for all parts
of the problem. Being able to find and understand the required information is then
very important.
Exercise 5.4: Fibonacci Numbers
The Fibonacci numbers 13 is a sequence of integers in which each number (except
the two first ones) is given as a sum of the two preceding numbers:
F n = F n−1 + F n−2 ,
F 0 = 1, F 1 = 1,
n = 2, 3, . . .
Thus, the sequence starts out as
1, 1, 2, 3, 5, 8, 13, 21, 34, . . .
a) Write a function make_Fibonacci that generates, and returns, the N first
Fibonacci numbers, when N is an input parameter to the function. Place the
function in a module named fibonacci (i.e., a file named fibonacci.py).
The module should have a test block, so that if run as a program, e.g., the 20
first Fibonacci numbers are printed to screen. Check that the program behaves
as intended.
b) The famous Johannes Kepler 14 found that the ratio of consecutive Fibonacci
numbers converges to the golden ratio, i.e.
lim
n→∞
F n+1
F n
=
1 +
√
5
2
.
Extend your module by defining a function converging_ratio, which takes
an array (or a list) F with (e.g., 20) Fibonacci numbers as input and then
checks (you decide how) whether Kepler’s understanding seems correct. Place
a call to the function in the test block and run the program. Was Kepler
right?
c) With the iterative procedure of the previous question, the ratios converged
to the golden ratio at a certain rate. This brings in the concept of
convergence rate, which we have not yet addressed (see, e.g., Sect. 7.5, or
elsewhere). However, if you are motivated, you may get a head start right
now.
In brief, if we define the difference (in absolute value) between
F n+1
F n
and the golden
ratio as the error e n at iteration n, this error (when small enough) will develop as
e n+1 = Ce
q
n , where C is some constant and q is the convergence rate (in fact,
this error model is typical for iterative methods). That is, we have a relation that
predicts how the error changes from one iteration to the next. We note that the
13 Read more about the Fibonacci numbers, e.g., on Wikipedia (https://en.wikipedia. org/wiki/Fibonacci_number).
14 https://en.wikipedia.org/wiki/Johannes_Kepler.
Précédent

- 147/350

Suivant