Page - 127 - in Programming for Computations β Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Image of the Page - 127 -
Text of the Page - 127 -
5.7 Exercises 127
larger theq, thequicker theerrorgoes tozeroas thenumberof iterations (n)grows
(whenen < 1). With the given error model, we may compute the convergencerate
from
q= ln(en+1/en)
ln(en/enβ1) .
This is derived by considering the error model for three consecutive iterations,
dividingoneequationby the otherandsolving forq. If then a seriesof iterations is
run,wecancomputeasequenceofvalues forq as the iterationcountern increases.
As n increases, the computed q values are expected to approach the convergence
rate that characterizes the particular iterative method. For the ratio we are looking
athere, theconvergenceratio is1.
Extend your module with a functioncompute_rates, which takes an array (or
a list) F with (e.g., 20) Fibonacci numbers as input and computes (and prints)
the corresponding values for q. Call the function from the test block and run the
program.Do theconvergenceratesapproach theexpectedvalue?
Later, in Sect.6.6.2, you will learn that convergence rates are very useful when
testing (verifying)software.
Filename:Fibonacci_numbers.py.
Exercise5.5:ReadFile:TotalVolume ofBoxes
A file box_data.dat contains volume data for a collection of rectangular boxes.
These boxes all have the same bottom surface area, but (typically) differ in height.
Thefile could, forexample, read:
Volume data for rectangular boxes
10.0 3.0
4.0
2.0
3.0
5.0
Apart from the header, each line represents one box. However, since they all have
the same bottom surface area, that area (10.0) is only given for the first box. For
that first box, also the height (3.0) is given, as it is for each of the following
boxes.
a) Writedownaformulaforcomputingthe totalvolumeofallboxesrepresentedin
the file. That formula should be written such that a minimum of multiplications
andadditions isused.
b) Write a program that reads the filebox_data.dat, computes the total volume
of all boxes represented in the file, and prints that volume to the screen. In the
calculations,apply the formula just derived.
(Note that, as afirst step,youmayread thefile and just print (to screen)what
is read.Comparingthisprintoutwithfilecontent(usesomeeditor) is thenagood
idea.)
c) In the filebox_data.dat, after the last line (containing the height of the βlastβ
box), insert a coupleof empty lines, i.e. just press enter a few times. Then, save
thefile and run theprogramanew.Whathappens?Explainbriefly.
Programming for Computations β Python
A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
- Title
- Programming for Computations β Python
- Subtitle
- A Gentle Introduction to Numerical Simulations with Python 3.6
- Volume
- Second Edition
- Authors
- Svein Linge
- Hans Petter Langtangen
- Publisher
- Springer Open
- Date
- 2020
- Language
- English
- License
- CC BY 4.0
- ISBN
- 978-3-319-32428-9
- Size
- 17.8 x 25.4 cm
- Pages
- 356
- Keywords
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Category
- Informatik