# 3 10 points section 25 computer problem 1 jacobi

• Homework Help
• 9

This preview shows page 3 - 6 out of 9 pages.

3. (10 Points) Section 2.5, Computer Problem 1 %Jacobi method written for the course MATH 472 function Jacobi_method_TOL(A,b,x0,x_exact,tol) D=diag(diag(A));%diag(A) returns only a vector x=x0; k=0; while (norm(x-x_exact,Inf)>tol) k=k+1; x= D\(b-(A-D)*x); end BE=norm(b-A*x,Inf); fprintf(’The Jacobi method required %d iterations with Backward Error %d.\n’,k,BE) Run twice the .m file Comp_25_1. %Comp_25_1 n=input(’Provide the size n of the matrix: ’); x0=zeros(n,1); B=[-ones(n,1), 3*ones(n,1),-ones(n,1)]; A=spdiags(B,-1:1,n,n); b=zeros(n,1); b(1)=2;b(2:n-1)=1;b(n)=2; xexact=ones(n,1); TOL=0.5*10^(-6); Jacobi_method_TOL(A,b,x0,xexact,TOL) >> Comp_25_1 Provide the size n of the matrix: 100 The Jacobi method required 36 iterations with Backward Error 4.578518e-07. >> Comp_25_1 Provide the size n of the matrix: 100000 The Jacobi method required 36 iterations with Backward Error 4.578518e-07. 3
4. (10 Points) Solve again Computer Problem 1 of Section 2.5 by means of Gauss-Seidel Method. %Gauss-Seidel method written for the course MATH 472 function GS_method_TOL(A,b,x0,x_exact,tol) U=triu(A,1); LD=A-U; x=x0; k=0; while (norm(x-x_exact,Inf)>tol) k=k+1; x= LD\(b-U*x); end BE=norm(b-A*x,Inf); fprintf(’The Gauss-Seidel method required %d iterations with Backward Error %d.\n’,k,BE) %Comp_25_1 n=input(’Provide the size n of the matrix: ’); x0=zeros(n,1); B=[-ones(n,1), 3*ones(n,1),-ones(n,1)]; A=spdiags(B,-1:1,n,n); b=zeros(n,1); b(1)=2;b(2:n-1)=1;b(n)=2; xexact=ones(n,1); TOL=0.5*10^(-6); GS_method_TOL(A,b,x0,xexact,TOL) >> Comp_25_1_GS Provide the size n of the matrix: 100 The Gauss-Seidel method required 21 iterations with Backward Error 4.779339e-07. >> Comp_25_1_GS Provide the size n of the matrix: 100000 The Gauss-Seidel method required 21 iterations with Backward Error 4.779339e-07. 5. (10 Points) Section 2.6, Exercise 8 (a) The Cholesky factorisation is determined by the matrix R := 2 0 - 1 0 1 1 0 0 1 . We solve the system R > y = b , where b := [4 2 0] > , and we obtain y = [2 2 0] > . We solve, now, the system Rx = y and we obtain x = [1 2 0] > . (b) The Cholesky factorisation is determined by the matrix R := 2 - 1 0 0 1 - 1 0 0 2 . We solve the system R > y = b , where b := [0 3 - 2] > , and we obtain y = [0 3 - 5] > . We solve, now, the system Rx = y and we obtain x = [1 2 - 1] > . 4
6. (10 Points) Section 2.6, Computer Problem 5 %Conjugate Gradient method function CGM(A,b,x) d=b-A*x; r=d; for k=1:size(A,1) if norm(r,Inf)<eps break; else alpha=dot(r,r)/dot(d,A*d); x=x+alpha*d; r_old=r; r=r-alpha*A*d; beta=dot(r,r)/dot(r_old,r_old); d=r+beta*d; end end fprintf(’The size (for the Inf-Norm) of the residual is %d \n’, norm(r,Inf)) fprintf(’and the number of steps is %d.\n’,k) % This is a modification of sparsesetup.m of the textbook % Input: n = size of system % Outputs: sparse matrix a, r.h.s. b n=input(’Provide the size n of the matrix: ’); e = ones(n,1); n2=n/2; A = spdiags([-e 3*e -e],-1:1,n,n); C=spdiags([e/2],0,n,n);C=fliplr(C);A=A+C; A(n2+1,n2) = -1; A(n2,n2+1) = -1; % Fix up 2 entries b=zeros(n,1); % Entries of r.h.s. b