This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Due: November 20, 2008 CS 257 (Luke Olson): Homework #10 Solutions Problem 1 Problem 1 In this problem, we compare the performance of Jacobi, GaussSeidel, and Conjugate Gradient versus the problem size. Using the functions jacobi.m , gauss seidel , and cg.m that you will provide, we will plot the number of iterations until convergence for each of the three solvers. The code is in this driver file, called solvercompare.m : Listing 1: Driver code 1 % solvercompare.m 2 3 nmax = 50; iters = zeros (nmax3,3); 4 5 for n = 4:nmax 6 A = gallery ( ’poisson’ ,n); 7 b = sin (2 * pi * (1:n * n)/(n * n))’; 8 9 % Jacobi 10 x = zeros (n * n,1); 11 [x,niters] = jacobi(A,b,x); 12 iters(n3,1) = niters; 13 14 % GaussSeidel 15 x = zeros (n * n,1); 16 [x,niters] = gauss_seidel(A,b,x); 17 iters(n3,2) = niters; 18 19 % Conjugate Gradient 20 x = zeros (n * n,1); 21 [x,niters] = cg(A,b,x,ones(n * n,1)); 22 iters(n3,3) = niters; 23 end 24 25 % plot results 26 probsizes = (4:nmax)’; 27 probsizes = probsizes .ˆ 2; 28 loglog (probsizes,iters(:,1), ’b’ ); 29 hold on; 30 loglog (probsizes,iters(:,2), ’r’ ); 31 loglog (probsizes,iters(:,3), ’g’ ); 32 legend ( ’Jacobi’ , ’GaussSeidel’ , ’CG’ ); 33 xlabel ( ’problem size’ ); 34 ylabel ( ’# of iterations’ ); Here is a template for jacobi.m . You may use the code from Lecture 22 iterative jacobi.m , but you will have to modify it to also return the number of iterations used and loop until convergence (using an appropriate test) instead of for a fixed number of iterations. Problem 1 continued on next page... Page 1 of 10 Due: November 20, 2008 CS 257 (Luke Olson): Homework #10 Solutions Problem 1 (continued) Listing 2: Jacobi solver 1 function [x,niters]=jacobi(A,b,x) 2 % jacobi(A,b,x)  computes solution to Ax=b using Jacobi iteration 3 % Usage: 4 % [x,niters] = jacobi(A,b,x) 5 % A  n x n matrix 6 % b  n x 1 vector (righthand side) 7 % x  n x 1 vector that contains initial guess 8 % Returns: 9 % niters  number of iterations until convergence 10 % x  solution Here is a template for gauss seidel.m . You may use the code from Lecture 22 iterative gauss seidel.m , but you will have to modify it to also return the number of iterations used and loop until convergence (using an appropriate test) instead of for a fixed number of iterations. Listing 3: GaussSeidel solver 1 function [x,niters]=gauss_seidel(A,b,x) 2 % gauss_seidel(A,b,x)  computes solution to Ax=b using GaussSeidel iteration 3 % Usage: 4 % [x,niters] = gauss_seidel(A,b,x) 5 % A  n x n matrix 6 % b  n x 1 vector (righthand side) 7 % x  n x 1 vector that contains initial guess 8 % Returns: 9 % niters  number of iterations until convergence 10 % x  solution Here is a template for cg.m . For this function, you will also need to make it return the error at each step in the A norm, in addition to the solution and the number of iterations used. The A norm of a vector x is given by  x  A = √ x T Ax , which is only a norm when...
View
Full Document
 Spring '08
 Olson
 Numerical Analysis, Articles with example pseudocode, Gauss–Seidel method, Jacobi method, Iterative method, Luke Olson

Click to edit the document details