-
Notifications
You must be signed in to change notification settings - Fork 0
/
steepest_descent.m
56 lines (46 loc) · 1.04 KB
/
steepest_descent.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
clear
n = 1000;
b = rand(n,1);
e1 = 1:1000;
A = sprandsym(n,.01,e1);
x = zeros(n,1);
r = b-A*x;
normr(1) = norm(r);
iter = 1;
while normr(iter)/normr(1) > 1e-6 && iter < 1000
alpha = r'*r/(r'*A*r);
x = x + alpha*r;
r = b-A*x;
iter = iter + 1;
normr(iter) = norm(r);
end
e2 = 1:.1:100.9;
A = sprandsym(n,.01,e2);
x = zeros(n,1);
r = b-A*x;
normr2(1) = norm(r);
iter = 1;
while normr2(iter)/normr2(1) > 1e-6 && iter < 1000
alpha = r'*r/(r'*A*r);
x = x + alpha*r;
r = b-A*x;
iter = iter + 1;
normr2(iter) = norm(r);
end
e3 = 1:.01:10.99;
size(e3)
A = sprandsym(n,.01,e3);
x = zeros(n,1);
r = b-A*x;
normr3(1) = norm(r);
iter = 1;
while normr3(iter)/normr3(1) > 1e-6 && iter < 1000
alpha = r'*r/(r'*A*r);
x = x + alpha*r;
r = b-A*x;
iter = iter + 1;
normr3(iter) = norm(r);
end
semilogy(0:size(normr,2)-1,normr/normr(1),0:size(normr2,2)-1,normr2/normr2(1),0:size(normr3,2)-1,normr3/normr3(1))
xlabel('iterations')
ylabel('relative residual')