## CPSC/Math 402: Numerical Linear Algebra

### Lab Exercises - Using the LinearSystem Class

The goal of lab is to become more familiar with the Java class and main program handed out in class.

Start by copying the files LinearSystem.java and SystemDriver.java of source code to your directory either by clicking on the links and saving or by using the following copy commands.

```
cp ~cpsc/public_html/Spring2006/CPSC402A/LinearSystem.java .
cp ~cpsc/public_html/Spring2006/CPSC402A/SystemDriver.java .

```

Now do the following:

1. Compile and run the program to see how it works. Test the program as follows:
1. Read in the 4 by 4 system on page 163. (Choose the read system option.)
2. Choose the option to solve by Gaussian elimination with naive pivoting. The solution should be correct.
3. Choose to print the LU decomposition. You should note that there was no pivoting.
4. Now do the preceding two steps with partial pivoting. Note that there were row swaps so PA, the permuted matrix, has rows in a different order than A.
5. Now do the same with scaled partial pivoting. You see a different order of rows (the text does this same system with scaled on pages 166-167).

2. Now test the Hilbert matrix option. Systems with the Hilbert matrix as the coefficient matrix are notoriously ill-conditioned. The program generates the system described in problem #18 on page 170 which has exact solution a vector with all ones.
1. Run the program as described in part (a) of #18. Note the solutions. For n = 7 (and each solution strategy) note the number of accurate digits in the most inaccurate entry in the solution vector.
2. Run the program as described in part (b). Note the accuracy as above for n = 13.
3. Based on your results above approximately what power of 10 would you hypothesize for the condition number of the 7 by 7 Hilbert matrix and the 13 by 13 Hilbert matrix?

3. Now implement the option of solving a system using the LU decomposition.
1. Open LinearSystem.java in emacs and type in the LUSolve() method that we started in class Wednesday. Note that its signature should be:
```    public void LUSolve()
```
2. Make sure the program compiles.
3. Now open SystemDriver.java and add the call to LUSolve in the appropriate menu option.
4. Compile and run the program. Test it using the system in #15 on page 203-204 of the text. Note that there is a menu option for changing just the b vector. The correct solutions are (1,-1,1,-1,1) for b1, (-1,1,1,1,-1) for b2, and (1, -2, 3, -2, 1) for b3.

4. Add a method to the LinearSystem class to compute and return the determinant of the coefficient matrix. Remember that this can easily be computed from the LU decomposition. The signature for the method should be
```      public double determinant()
```
```      public double normA()