Reading and references:
For each of the following exercises create the specified files with your answer(s). Submit your homework using the course submit utility.
The faster a galaxy is receding, the farther away it is. You'd think that as you went farther away you'd be looking at a larger volume of space, so there should be more galaxies. If you did it right, your histogram should show that the opposite is true; that is, there is a peak and then beyond that the the number falls off rapidly with increasing velocity. Why?
Answer: It has to do with the flux limit of the survey. The survey omitted galaxies whose apparent brightness (as seen on Earth) fell below a certain threshold. At great distance a galaxy had to be very bright at the source in order to be included. But the number of very bright galaxies decreases rapidly with increasing brightness.
Submit only the files n_vs_vr and n_vs_vr.gpl.
In this exercise you are to improve a program for computing the roots of a quadratic equation ax2 + bx + c = 0 and in the next exercise, use it to explore round-off errors. I have a crude single-precision version in ~p6720/examples/c++/quadratic.cc. Copy this program, but modify it as follows:
x = [-b +/- sqrt(b^2 - 4*a*c)]/(2*a)
and using the alternative formula
x = 2*c/[-b -/+ sqrt(b^2 - 4*a*c)]
You should compare the second formula (by hand algebra) with the
standard one and notice that the root with the plus sign before the
square root in one formula gives the same root as with the minus sign
in the other. (Nothing to hand in.) By choosing the better formula
carefully for each root you can avoid any round-off errors. Write all
four answers, so you can compare them. Have your program label the
two more reliable roots "robust" and the two less reliable roots
"risky".
make quadratic
(a) In the conventional formula, when b is positive, which sign in front of the square root leads to a possible loss of significant digits? When it is negative, which?
(b) A large loss of significance occurs when you subtract two numbers that are almost equal. In each case the two numbers are b and sqrt(b^2 - 4*a*c). What simple mathematical condition can you put on a, b, and c that would cause the magnitudes of these two numbers to be very close?
(c) Then run your program for the cases
a = 0.02, b = 2, c = 4
a = 2e-4 , b = 3, c = 5
a = 1e-2, b = 8, c = 1e-3
Put your results in a text file roundoff.txt.
Identify which of the pair of results is the correct answer. Then
explain the differences in the two sets of results in terms of
round-off error. Explain also the size of the round-off error
in the poorly evaluated roots.
NOTE: It is not sufficient to give only numerical results here. You must also give a clear EXPLANATION of those results. Your explanation should say why the risky formula gives such a bad result in the worst case, and why it gives such a good result in the best case.
Submit the file roundoff.txt.