-
Notifications
You must be signed in to change notification settings - Fork 0
/
cgdemo.m
67 lines (57 loc) · 1.22 KB
/
cgdemo.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
% A simple demonstration of the conjugate gradient algorithm
% and the method of steepest descents for quadratic minimization
A = gallery('wathen',5,9);
% a random sparse SPD finite element mass matrix
n=length(A);
b=ones(n,1);
% initial guess
x=zeros(n,1);
% number of iterations
niter = 2*n;
% store norm of residual in res
res=zeros(niter+1,1);
% conjugate gradients
r = b-A*x;
s = r;
r2 = r'*r;
res(1) = sqrt(r2);
for i = 1:niter
t = A*s;
s2 = s'*t;
lambda = r2/s2;
x = x + lambda*s;
r = r-lambda*t;
r2old = r2;
r2 = r'*r;
s=r+(r2/r2old)*s;
res(i+1)=sqrt(r2);
end
% plot norms of residuals: linear convergence means a straight line
semilogy(res)
hold on
% for comparison show steepest descents
% initial guess
x=zeros(n,1);
% number of iterations
niter = 2*n;
% store norm of residual in res
ressd=zeros(niter+1,1);
% steepest descents
for i = 1:niter
s = b-A*x;
s2 = s'*s;
l = s2/(s'*A*s);
x = x + l*s;
ressd(i)=sqrt(s2);
end
s = b-A*x;
ressd(niter+1)=sqrt(s'*s);
% plot norms of residuals: linear convergence means a straight line
semilogy(ressd,'r')
grid on
xlabel('iterations')
ylabel('norm of residual')
legend('conjugate gradient','steepest descents')
disp('hit any key to close window')
pause
close