Physics 3730/6720 Assignment 6

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.


Exercise 1. Understanding a home-made class for solving a tridiagonal system

Please see the notes describing the tridiag class and, if you need them, the pseudocode notes. Then answer these questions. Please use your editor to put your answers in a file called playtridiag.txt.
  1. Which of the class methods carries out the Gaussian elimination steps leading to an upper triangular matrix?
  2. Consider the tridiagonal system
           2  1   0 | u0 |  1
           2  2  -1 | u1 |  0
           0  1   2 | u2 | -1
    
    Compare it with the matrix form in the notes and be sure you can read off the elements of the vectors a, b, c, and r. Then by hand calculation ("playing computer"), carry out the steps in the method triangulate and backsubst. Notice that each of these methods has one for loop. Show the values at the end of each pass through each for loop. To show the current values, copy the lines above as a template and replace the values of the entries that are changed. For example, in triangulate, in the first iteration of the loop you have i = 0. When it reaches the bottom of the loop the computer has replaced a[1], b[1], and r[1] and you would write the current status of the system as
           2  1   0 | u0 |  1
           0  1  -1 | u1 | -1
           0  1   2 | u2 | -1
    
    Continue in the same manner and do this for the remaining steps. Altogether, you should have five new versions of the linear system plus the original system. Put all of these versions in your answer file.

    You should recognize the steps as standard Gaussian elimination plus back substitution, except that there is no row pivoting and we don't divide to get a one on the diagonal of the upper triangular matrix.

  3. Verify the code in the check method by working through the case in the previous example. Let the vectors a, b, c, and r be as they are in the original system, and let the proposed solution vector x be the exact solution to the linear system that you found above. In your answer file show the calculation of the new value of diff in each instance. For example, you would show the calculation of the first assignment to diff as follows:
         diff = 2*1 + 1*(-1) - 1
    
    Do the same for the remaining lines.

    You should recognize the steps as the calculation of the components of the vector A x - r where A x is the product of the original matrix times the vector x.


Exercise 2. Programming with the class

In this exercise you are to use the home-made tridiagsys class for solving a tridiagonal system of any size. You will use the file tridiagsys.h as a header (include) file in your code. Please note that you need to put using namespace std; at the top of your file before including the tridiagsys.h header.

You should create a main program file solvetd.cc, and a make file Makefile for building your code. The name of the target and executable is solvetd.

Write your main program solvetd.cc so it reads the matrix problem in the format described under operator>> in tridiagsys.html handout, solves it, prints out the solution, checks the answer, and reports the value of eps, the value returned by the check method.

Hints: Your code should be under 25 lines long, not counting comments! To read all the numbers for the linear system, please see what the operator>> does with cin and this class. It is OK to start with an empty tridiagsys, since the process of reading in the numbers will cause it to grow to the right size. To solve the linear system, please look at the solve method. When you write out your answer, you won't be able to use << with the vector class and cout. So you'll need to do it with a for loop, writing out each element u[i] one-by-one. Finally, to tell how many elements there are in a vector, please see the notes on the standard library vector class. It tells you what method to use.

Note that in order to check the answer, you have to keep a copy of the original tridiagonal system, because after you run the solve method, the system is altered so it becomes trivial.

Hand in only Makefile and solvetd.cc. We already have the file tridiagsys.h.


Exercise 3. Running your program

Run your program with the test input in ~p6720/examples/tridiag/tddata. My own version of the program is also there in case you want to compare your results and mine for any data you throw at it. Put the solution vector u in a file tdsolve.txt. Also give the value of eps reported by your program.
This page is maintained by:
Carleton DeTar Mail Form
Last modified 27 October 2008