% eigen3.m  demonstrate even more eigenvalue testing
format compact
e1=1+j*4; % desired eigenvalues, complex
e2=2+j*3;
e3=3+j*2;
e4=4+j*1;
P=poly([e1 e2 e3 e4]) % make polynomial from roots
r=roots(P) % check that roots come back
A=zeros(4);
A(1,1)=-P(2); % build matrix A
A(1,2)=-P(3);
A(1,3)=-P(4);
A(1,4)=-P(5);
A(2,1)=1;
A(3,2)=1;
A(4,3)=1
Q=randn(4)
QI=randn(4)
QC=Q+j*QI
B=QC*A*inv(QC)
B=B.*(5+j*6) % scale then unscale eigenvalues
[v e]=eig(B) % compute eigenvectors and eigenvalues
ee=e./(5+j*6) % expect e1, e2, e3, e4
I=eye(4);    % identity matrix
for i=1:4
  z1=det(B-I.*e(i,i)) % should be near zero
end
for i=1:4
  z2=B*v(:,i)-e(i,i)*v(:,i) % note columns, near zero
end
z3=trace(B)-trace(e) % should be near zero
for i=1:4
  z3=det(A-I.*ee(i,i)) % should be near zero, not in MatLab
end
z4=det(e) % should be zero
