CPSC/Math 402: Numerical Linear Algebra
Lab Exercises
Using the C++ Matrix Class
The first goal of lab is to become a little familiar with the C++
program handed out in class. The header file shows you the methods
and data members of the class. Two additional methods, one to compute
the residual and another to perform one iteration of iterative
refinement have been added (they are not in the copy handed out in
class -- and the refine method is not complete yet).
Start by copying the following files of source code to your directory:
Now do the following:
- Compile and run testMat.cc to see how it works. One way to compile
is with the following command (this compiles each file and links them
and stores the executable in the file testM;
you may also compile and link separately or
use a Makefile):
g++ -o testM testMat.cc Matrix.cc
Now execute the program by typing
./testM
Test the program by using the following input:
- The system in Computer Problem 2.9 part (a) (page 102)
with epsilon = 0.000001 (10^-6). That is, the system
0.000001 1 1.000001
1 1 2
All three pivoting strategies should give you the same solution.
- The same system with the following values of epsilon: 10^-15, 10^-16,
and 10^-18. Notice that naive pivoting doesn't work so well anymore!
- The system in Computer Problem 2.1 part (a) (page 100). In theory
this matrix is singular but roundoff in representation (the entries are
not machine numbers in base 2) means the
computer doesn't notice (see part (c)). Save the answers you get.
We'll use Mathematica on this later and compare.
- Now compile and run testHilbert.cc. This program asks you for the
size of the Hilbert matrix you want to generate. The Hilbert matrix
is ill-conditioned but the program does ok for small n.
- Now open Matrix.cc in emacs and complete the function refine
that computes one iteration of iterative refinement. Part of it is already
done for you. You have three places to add code -- there are comments
indicating what you are to do.
- Open up testMat.cc and add a call to the refine function after the
system has been solved using naive pivoting.
- Compile your program and test it on the system from problem 2.9.
You should see that when you use epsilon = 10^-16 (or any value that
gave a different answer for naive pivoting) the iterative refinement
corrects the solution.
Numerical Linear Algebra Using Mathematica
Reboot into Windows then follow the instructions in the handout on Mathematica.