5. We consider here the same problem Ax-b 14 4 -2 -2 0 0-2 0 20 0 -2 2 0 0 1 3 2
ID: 3184841 • Letter: 5
Question
5. We consider here the same problem Ax-b 14 4 -2 -2 0 0-2 0 20 0 -2 2 0 0 1 3 2-1 0 1 0 0 -3 5-1-25 1 -1 0 1 2-1 6 21 -1-1 0 -19 21 10 2 3111, and b= 0 2 -5 5 1 2 10 20 2 17 0 0 3 1-1 1 0-2 15-5 0 - 0 1 22-57 -13 with A being (symmetric) positive-definite. (a) (5 points) Solve this problem using the conjugate-gradient method to tolerance Tol = 10-15, with initial guess x,-(1, 1, 1, 1, 1, 1, 1, 1, 1,1)" and the maximum number of iterations marter = 1000. Record the solution x and the number of iterations required. (b) (5 points) Compare the relative speed of solving this problem using the Cholesky method and using conjugate-gradient. To time the Cholesky method in MATLAB, you can use the commands timeCHOL=0 ; opts.SYM=true ; opts.POSDEF=true ; for ii 1:NTRY tic XX-linsolve(A, b, opts); timeCHOL-timeCIIOL toc; end timeCHOL-timeCHOL/NTRY Since MATLAB'slinsolve function uses a Cholesky solver if the matrix is specified with the option, opt, to be symmetric positive-definite. Use, say, NTRY = 1000 You can time the conjugate-gradient method by rewriting the code demo precg.m as a MATLAB function file to output the solution as x - cg fun(A, b, Tol, maxIter, x0). Do so and then replace linsolve in the commands above with cg.fun. Give the average times required for the Cholesky solver and for conjugate- gradient with TOL = 10-15, 10-7, and 10-3. For matrices of such small size, what can you infer about the relative merits of using Cholesky versus conjugate-gradient?Explanation / Answer
matlab code
close all
clear
clc
% Part (a)
disp('Part (a)');
A = [4 -2 -2 0 0 -2 0 2 0 0;
-2 2 0 0 1 3 2 -1 0 1;
-2 0 11 -3 2 -1 -5 -4 3 -4;
0 0 -3 5 -1 -2 5 1 1 -1;
0 1 2 -1 6 2 1 -1 -1 0;
-2 3 -1 -2 2 10 2 -3 1 1;
0 2 -5 5 1 2 10 2 0 2;
2 -1 -4 1 -1 -3 2 5 -2 2;
0 0 3 1 -1 1 0 -2 15 -5;
0 1 -4 -1 0 1 2 2 -5 7];
b = [-14 9 44 -19 21 22 -17 -28 2 -13]';
tol = 1e-15; % Change this to 1e-7 and 1e-5
maxit = 1000;
tic
x = cgs(A,b,tol,maxit);
toc
x
% Part (b)
fprintf(' Part (b) ');
opts.SYM = true; opts.POSDEF = true;
NTRY = maxit;
tic
for ii=1:NTRY
x = linsolve(A,b,opts);
end
toc
x
output
Part (a)
cgs converged at iteration 16 to a solution with relative residual 1.1e-16.
Elapsed time is 0.001813 seconds.
x =
1.0000
3.0000
3.0000
-1.0000
1.0000
1.0000
0.0000
-2.0000
-1.0000
-1.0000
Part (b)
Elapsed time is 0.012030 seconds.
x =
1
3
3
-1
1
1
0
-2
-1
-1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.