Homework/Programming 9. (Due April 9)

advertisement
Homework/Programming 9. (Due April 9)
Math. 417
We shall be again applying iterative methods to sparse matrices in this
assignment. We will again use the files for A3 (from Assignment 7) and
some additional files for A5 , the two dimensional analogue of A3 . The goal
will be to compare the number of iterations for Jacobi with that for the
conjugate gradient method.
We shall use the conjugate gradient iteration provided by MATLAB, specifically the function pcg. Although pcg is meant to be used with a preconditioner, it can be used without preconditioning with the following call
x=pcg(@aeval,b,tol,maxit)
Here aeval refers to a function aeval.m which does the multiplication of a
crs stored matrix times a vector. Its interface should look like:
function w=aeval(x)
global n rind cind val
.
.
.
Note that we declare the variables n, rind, cind and val as globals. Global
variables are shared with the functions declaring them as such. The function
aeval will be called by pcg. aeval needs the sparse matrix structure while
pcg only passes the vector x (in fact, pcg does not know anything about the
matrix A). The use of global variables allows the main or calling function to
define the global variables (by reading the input files using readcrs) making
them available to the aeval function. Note that the main function also needs
the global declaration (appearing before the readcrs call). Note that the
remainder of aeval is the application of a crs matrix to a vector which, more
or less, is given on Jun Ren’s home page www.math.tamu.edu/˜ junren.
1
2
For all computations we shall use b(i) = i/n, i = 1, . . . , n as the right
hand side vector. The pcg routine checks the relative residual error in the
Euclidean norm against the tolerance, i.e., it iterates until
(0.1)
norm(b − Ax)/norm(b) < tol.
It also always uses x0 = 0. For all runs below use tol = .1 and make maxit
(for pcg) large enough to reach the tolerance. To compare with the results
from pcg, you need to modify your Jacobi iteration of Homework 7 so that
it uses the same x0 , b and stopping criteria (0.1).
Problem 1. Develop a main and aeval routine for implementing pcg with crs
structure for A read from a file. Modify your Jacobi routines from Homework
7 as discussed above. Apply pcg and your modified Jacobi iteration to the
six crs files of Homework 7 with stopping criteria, initial iterate and b as
discussed above. Report the number of iterations and the achieved value for
norm(b − Ax)/norm(b) for both Jacobi and pcg (pcg automatically reports
these values).
We next consider the matrix A5 . This matrix comes from applying finite
differences to a boundary value problem on the unit square in R2 . It is
defined in terms of a parameter m and leads to an SPD problem with n = m2
unknowns. The m2 × m2 matrix is sparse and contains at most 5 entries
per row. In this case, the spectral radius of GJ and the condition number
K(A5 ) grows like O(m2 ) (not O(n2 )).
The crs data for the A5 matrices are given below:
(a) a5-4.txt
(b) a5-8.txt
(c) a5-16.txt
(d) a5-32.txt
(e) a5-64.txt
(f) a5-128.txt
3
The parameters 4, 8, 16, . . . , 128 refer to m. These files are much larger than
those for A3 since n = m2 .
Problem 2. Apply pcg and your modified Jacobi iteration to the six crs files
for A5 given above. Report the number of iterations and the achieved value
for norm(b − Ax)/norm(b) for both Jacobi and pcg. Depending on what
computer you are using, you may find that the compute time for the Jacobi
iterations is getting very large for large m. In any event, you should be able
to run up to m = 32 and need not report the iteration results for the Jacobi
iteration for larger two cases if the runs become too long.
Download