]> git.rustad.me Git - optimering/commitdiff
Add code for CG and SR1 method
authorBjørn Rustad <bjorn@rustad.me>
Fri, 9 Mar 2012 10:13:46 +0000 (11:13 +0100)
committerBjørn Rustad <bjorn@rustad.me>
Fri, 9 Mar 2012 10:13:46 +0000 (11:13 +0100)
CG.m [new file with mode: 0644]
SR1.m [new file with mode: 0644]

diff --git a/CG.m b/CG.m
new file mode 100644 (file)
index 0000000..ea8fb5f
--- /dev/null
+++ b/CG.m
@@ -0,0 +1,12 @@
+% Standard CG
+x = zeros(size(b)); g = -b ; p = -g; 
+
+% Iteration
+for loop = 1:nsim
+       Ap = A*p;
+       alfa = -(p'*g)./(p'*Ap);
+       x = x + alfa*p;
+       g = g + alfa*Ap; % g = A*x-b;
+       beta = (g'*Ap)./(p'*Ap);
+       p = -g + beta*p;
+end; 
diff --git a/SR1.m b/SR1.m
new file mode 100644 (file)
index 0000000..aeb8352
--- /dev/null
+++ b/SR1.m
@@ -0,0 +1,16 @@
+% Initialization:
+x = 0 ; g = -b;
+B = eye(ndim); H = B;
+
+% Primitive SR1 iterasjon
+for loop = 1:nsim
+       p = -H*g;
+       x = x + p;
+       y = -g;
+       g = A*x-b;
+       y = g + y;
+       v = y - B*p;
+       B = B + v*v'/(v'*p);
+       w = p - H*y;
+       H = H + w*w'/(w'*y);
+end