Homework 9 December 5, 2009 Problem 1 4x1 − x2 = 2 −x1 + 4x2 − x3 = 6 −x2 + 4x3 = 2 Ho wl e Consider the following linear system: Vi ct or ia (a) Compute the Jacobi iteration matrix J for this system. (b) Determine the spectral radius ρ(J) of J. (c) Determine or deduce that the spectral radius of the Gauss-Seidel matrix G satisfies ρ(G) < ρ(J). Problem 2 c) 20 09 Don’t be intimidated by the length of this problem! It looks long because I am walking you through most of the steps. In this problem, you are exploring in MATLAB the differences in a couple of iterative methods we have learned. You will build a sample matrix, then solve a system Ax = b using some of the methods we have talked about. py r ig ht ( (a) Write a MATLAB function that takes as input a matrix A and a tolerance, builds the Jacobi matrix, and iterates until the relative residual norm (norm of the residual divided by the norm of b) is less than the given tolerance. (I recommend that you also give your code a maximum number of iterations in case the iteration fails.) Your code should output the solution x at the end of the iteration. (b) Write a similar MATLAB function that does Gauss-Seidel iteration. Co (c) Next we will use your two functions on a test system. First, make a test matrix that we know to be HPD (in this case, it’s SPD). A = sprandsym(1000,0.2,0.05,1); xtrue = rand(1000,1); b = A*xtrue; 1 The sprandsym command above creates a symmetric positive definite matrix of size 1000 × 1000 with approximately 0.2 × 10002 nonzeros and condition number approximately equal to 1/0.05 = 20. What is the spectral radius of the Jacobi matrix J? What is the spectral radius of the GaussSeidel matrix G? (You can use the command max(abs(eig(full(J)))), for example, for the spectral radius of J.) Ho wl e Based on the theory we covered in class (also in Ackleh) and the spectral radius, do you expect the Jacobi iteration to converge? Do you expect Gauss-Seidel to converge? Run your Jacobi and Gauss-Seidel codes on this system. Do they converge? How many iterations and how much time does each method take? (Use tic and toc for timings.) What is the error |xtrue − xn | in the solution from each method? (d) Next, we will compare these results with solution using conjugate gradients. or ia MATLAB has a built-in CG method called pcg. We will solve the system using CG without preconditioning, then again using an incomplete Cholesky preconditioner. Vi ct First, solve the system using CG without preconditioning. Use the following command. Check MATLAB’s help to understand all of the inputs and outputs. 20 09 tic; [x1,flag1,relres1,iter1,resvec1] = pcg(A, b,1e-6,1000); toc; How many iterations and how much time did unpreconditioned CG take? What is the error in the solution? py r ig ht ( c) Next, we will build a simple preconditioner using an incomplete factorization. Since the matrix is SPD, we’ll use Cholesky. Recall that even when a matrix is sparse, unless the sparsity structure is special — banded, for example — the Cholesky factors will typically be fairly dense. However, we can build approximate Cholesky factors by simply leaving out any elements that would cause fill (nonzeros appearing where the original sparsity structure had zeros). The resulting factors are no longer an exact factorization of A, but we do have A ≈ R∗ R, and for some matrices this can be a very effective preconditioner. Co Build an incomplete factorization of A using R = cholinc(A,’0’); For comparison, build the complete Cholesky factorization: R2 = chol(A); 2 How sparse is the incomplete factor R compared to the full Cholesky factor R2? (Use MATLAB’s nnz command to find the number of nonzeros.) Finally, solve the system using CG preconditioned with the incomplete Cholesky factorization. Again, see help for what all of the inputs mean. tic; [x2,flag2,relres2,iter2,resvec2] = pcg(A, b,1e-6,100,R’,R); toc; Ho wl e How many iterations and how much time did preconditioned CG take? What is the error in the solution? Co py r ig ht ( c) 20 09 Vi ct or ia (e) Finally, compare the previous results to using MATLAB’s backslash. What method will backslash to use on this system? How much time does backslash take? What is the error in the solution? 3