Assignment 4 Scripts

 

P1

% P1
% Tests PTLS

clc
randn('seed',0)
m = 10; n = 4; A = randn(m,n); b = randn(m,1);

for p=0:n
   x = PTLS(A,b,p);
   disp(' ')
   disp(sprintf('p = %1d',p))
   disp('x = ')
   disp(sprintf('  %20.16f \n',x))
end

P2

% P2
% Tests ComplexGivens

clc
i = sqrt(-1);
Examples = [ 0  2  3+2*i   (3+2*i)*10^(-40)  (3+2*i)*10^(40); ... 
             0  0  4+5*i   (4+5*i)*10^(-40)  (4+5*i)*10^(40)];
       
disp('[c  conj(s); -s  c]''*[y;z] = e')
disp(' ')    
disp('  real(y)   imag(y)   real(z)   imag(z)       abs(e(2))             norm([c s]) ')  
disp('----------------------------------------------------------------------------------')
for v = Examples
   y = v(1);
   z = v(2);
   [c,s] = ComplexGivens(y,z);
   e = [c conj(s); -s c]'*[y;z];
   disp(sprintf(' %6.1e  %6.1e  %6.1e  %6.1e      %10.3e      %20.15e',real(y),imag(y),real(z),imag(z),abs(e(2)),norm([c s])))
end

P3

% P3
% Tests RidgeCV

clc
randn('seed',0)

% Generate an ill-conditioned A

m = 100; n = 4; 
[U,R] = qr(randn(m,m));
[V,R] = qr(randn(n,n));
S = [diag(linspace(eps,1,n));zeros(m-n,n)];
A0 = U*S*V';
b0 = randn(m,1);

disp('     dmax             lambda_opt ')
disp('----------------------------------')
for dmax = [2 10 20 40 100] 
   % Scale the equations..
   D = diag(linspace(1,dmax,m));
   A = D*A0; b = D*b0;
   [x,lambda_opt] = RidgeCV(A,b);
   disp(sprintf('%10.3f  %20.15f',dmax,lambda_opt))
end
x = x