Seite - 126 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Bild der Seite - 126 -
Text der Seite - 126 -
126 5 SomeMorePythonEssentials
Google), and you probably have to look into more details about how Taylor series
arehandledwithsympy.
Toyourcomfort, this isavery typicalsituationforengineersandscientists.They
need to solvea problem,but donot (yet!)have the requiredknowledgefor all parts
of the problem. Being able to find and understand the required information is then
very important.
Exercise5.4:FibonacciNumbers
The Fibonacci numbers13 is a sequence of integers in which each number (except
the twofirst ones) isgivenasasumof the twoprecedingnumbers:
Fn=Fn−1 +Fn−2, F0 =1,F1 =1, n=2,3,.. .
Thus, thesequencestartsoutas
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 Kepler14 found that the ratio of consecutive Fibonacci
numbersconvergesto the goldenratio, i.e.
lim
n→∞ Fn+1
Fn = 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.
Inbrief, ifwedefine thedifference(inabsolutevalue)between Fn+1
Fn and thegolden
ratio as the error en at iterationn, this error (when small enough) will develop as
en+1 = Ceqn, 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/Fi-
bonacci_number).
14 https://en.wikipedia.org/wiki/Johannes_Kepler.
Programming for Computations – Python
A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
- Titel
- Programming for Computations – Python
- Untertitel
- A Gentle Introduction to Numerical Simulations with Python 3.6
- Band
- Second Edition
- Autoren
- Svein Linge
- Hans Petter Langtangen
- Verlag
- Springer Open
- Datum
- 2020
- Sprache
- englisch
- Lizenz
- CC BY 4.0
- ISBN
- 978-3-319-32428-9
- Abmessungen
- 17.8 x 25.4 cm
- Seiten
- 356
- Schlagwörter
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Kategorie
- Informatik