5.7 Exercises
127
larger the q, the quicker the error goes to zero as the number of iterations (n) grows
(when e n < 1). With the given error model, we may compute the convergence rate
from
q =
ln(e n+1 /e n )
ln(e n /e n−1 )
.
This is derived by considering the error model for three consecutive iterations,
dividing one equation by the other and solving for q. If then a series of iterations is
run, we can compute a sequence of values for q as the iteration counter n 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
at here, the convergence ratio is 1.
Extend your module with a function compute_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 the convergence rates approach the expected value?
Later, in Sect. 6.6.2, you will learn that convergence rates are very useful when
testing (verifying) software.
Filename: Fibonacci_numbers.py.
Exercise 5.5: Read File: Total Volume of Boxes
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.
The file could, for example, 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) Write down a formula for computing the total volume of all boxes represented in
the file. That formula should be written such that a minimum of multiplications
and additions is used.
b) Write a program that reads the file box_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 a first step, you may read the file and just print (to screen) what
is read. Comparing this printout with file content (use some editor) is then a good
idea.)
c) In the file box_data.dat, after the last line (containing the height of the “last”
box), insert a couple of empty lines, i.e. just press enter a few times. Then, save
the file and run the program anew. What happens? Explain briefly.
127
larger the q, the quicker the error goes to zero as the number of iterations (n) grows
(when e n < 1). With the given error model, we may compute the convergence rate
from
q =
ln(e n+1 /e n )
ln(e n /e n−1 )
.
This is derived by considering the error model for three consecutive iterations,
dividing one equation by the other and solving for q. If then a series of iterations is
run, we can compute a sequence of values for q as the iteration counter n 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
at here, the convergence ratio is 1.
Extend your module with a function compute_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 the convergence rates approach the expected value?
Later, in Sect. 6.6.2, you will learn that convergence rates are very useful when
testing (verifying) software.
Filename: Fibonacci_numbers.py.
Exercise 5.5: Read File: Total Volume of Boxes
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.
The file could, for example, 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) Write down a formula for computing the total volume of all boxes represented in
the file. That formula should be written such that a minimum of multiplications
and additions is used.
b) Write a program that reads the file box_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 a first step, you may read the file and just print (to screen) what
is read. Comparing this printout with file content (use some editor) is then a good
idea.)
c) In the file box_data.dat, after the last line (containing the height of the “last”
box), insert a couple of empty lines, i.e. just press enter a few times. Then, save
the file and run the program anew. What happens? Explain briefly.
