Assignment 5 test Scripts
P1
% P1: Tests NearestSym
randn('seed',0)
n = 10;
clc
for eg = 1:2
A = randn(n,n);
[alpha,u] = NearestSym(A);
disp(sprintf('min = %20.15f',norm(A-alpha*u*u','fro')))
disp(sprintf('alpha = %20.15f',alpha))
disp(sprintf(' u ='))
disp(sprintf(' %20.15f\n',u))
disp(' ')
end
P2
% P2: Tests nLeftHalfPlaneEigs
randn('seed',0);
rand('seed'0);
n = 17;
clc
disp('p = number of eigenvalues equal to -1')
disp('r = number of complex eigenvalue pairs in lhp')
disp('m = nLeftHalfPlaneEigs(U)')
disp('m0 = sum(real(eig(U))<0)')
disp(' ')
disp(' p r m m0')
disp('------------------------')
for p=0:n
% p = the number of eigenvalues equal to -1
for r=0:floor((n-p)/2)
% r = the number of complex conj eigenvalue pairs in lhp.
% Generate initial U as a direct sum of 1-by-1 and 2-by-2's noting
% that [c s;-s c] has eigenvalues c+is and c-is
U = eye(n,n);
% The -1 eigenvalues...
for k=1:p
U(k,k) = -1;
end
% The complex eigenvalues in the lhp....
for k=1:r
theta = (pi/2)*(1+rand); c = cos(theta); s = sin(theta);
U(p+2*k-1:p+2*k,p+2*k-1:p+2*k) = [c s;-s c];
end
% The complex eigenvalues in the rhp...
for k=1:floor((n-p-2*r)/2)
theta = (pi/2)*rand; c = cos(theta); s = sin(theta);
U(p+2*r+2*k-1:p+2*r+2*k,p+2*r+2*k-1:p+2*r+2*k) = [c s;-s c];
end
% Now scramble the block diagonal via random orthogoal
% similarity...
[Q,R] = qr(randn(n,n));
U = Q*U*Q';
% Compute the answer two different ways...
m = nLeftHalfPlaneEigs(U);
m0 = sum(real(eig(U))<0);
disp(sprintf(' %4d %4d %4d %4d',p,r,m,m0))
end
end