% test_svd.m   eigenvalues in MatLab
format compact
fid=fopen('test_svd_m.out','w');
fprintf(fid,'test_svd.m running \n');
A=zeros(4);

for i=1:4
  for j=1:4
    A(i,j) = 1.0/(i+j)
    fprintf(fid,'A(%d,%d)=%f \n',i,j,A(i,j));
  end
end
  
fprintf(fid,'compute eigenvectors and eigenvalues \n');
fprintf(fid,'[v e]=eig(A) \n');
[v e]=eig(A) % compute eigenvectors and eigenvalues
for i=1:4
  fprintf(fid,'e(%d,%d)=%e \n',i,i,e(i,i));
end
fprintf(fid,' \n');
for i=1:4
  for j=1:4
    fprintf(fid,'v(%d,%d)=%f \n',i,j,v(i,j));
  end
end


fprintf(fid,'check det(A-eI)=0 for all four e \n')
I=eye(4);    % identity matrix
for i=1:4
  z1=det(A-I.*e(i,i)) % should be near zero
  fprintf(fid,'for e(%d)=%e \n',i,z1);
end
fprintf(fid,' \n');

fprintf(fid,'check Av - ev =0  all four e \n')
for i=1:4
  z2=A*v(:,i)-e(i,i)*v(:,i) % note columns, near zero
  fprintf(fid,'for e(%d)=%e %e %e %e\n',i,z2);
end
fprintf(fid,' \n');
	
fprintf(fid,'check trace(A)= trace(e) \n')
z3=trace(A)-trace(e) % should be near zero
fprintf(fid,'trace(A)-trace(e)=%e \n',z3);

fprintf(fid,' \n');
fprintf(fid,'test_svd.m finished \n');
