% Test Script C4
clc
close all
% A small dense example..
randn('seed',0)
% Generate the problem and "true" solution...
m = 1000; n = 500; A = randn(m,n); b = randn(m,1);
x0 = A\b;
format long
x = zeros(n,1);
itMax = 5; nStepClusters = 50;
relerr = zeros(1,nStepClusters);
for j=1:nStepClusters
x = LSviaGS(A,b,itMax,x);
relerr(j) = norm(x-x0)/norm(x0);
end
semilogy(itMax*(1:nStepClusters),relerr)
xlabel('Iterations')
ylabel('Relative Error')
shg

% A large sparse example..
randn('seed',0)
% Generate the problem and "true" solution...
m = 10000; n = 500; A = randn(m,n); b = randn(m,1);
A = sparse(0);
p = .01;
% Set up an m-by-n sparse matrix with 1% nonzeros
for i=1:m
for j = 1:n
if rand(1,1)<p
A(i,j) = randn(1,1);
end
end
end
A(1,:) = randn(1,n);
x0 = A\b;
format long
x = zeros(n,1);
itMax = 1; nStepClusters = 100;
relerr = zeros(1,nStepClusters);
for j=1:nStepClusters
x = LSviaGS(A,b,itMax,x);
relerr(j) = norm(x-x0)/norm(x0);
end
figure
semilogy(itMax*(1:nStepClusters),relerr)
xlabel('Iterations')
ylabel('Relative Error')
shg