Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. %% gram_schmidt: function description
  2. function outputs = gram_schmidt(arg)
  3. c1 = arg([1 : rows(arg)],1);
  4. c2 = arg([1 : rows(arg)],2);
  5.  
  6. res1 = c1 / sqrt(dot(c1, c1));
  7.  
  8. res2 = c2 - dot(c2, c1) / dot(c1, c1) * c1;
  9. res2 = res2 /sqrt(dot(res2,res2));
  10.  
  11. outputs =transpose([transpose(res1); transpose(res2)]);
  12. end
  13.  
  14. function [Z,B]=dwaww (mat,max_iter_count =1000,TOL=0.001)
  15. if(norm(mat-transpose(mat))!=0)
  16. error("not a symmetric matrix")
  17. endif
  18.  
  19. iter_count = 0;
  20. columns_count = columns(mat);
  21. Z = eye(columns_count,2);
  22.  
  23. while(true)
  24. if(iter_count > max_iter_count)
  25. error('exceeded the maximal iteration limit')
  26. endif
  27. iter_count = iter_count+1;
  28.  
  29. W = mat*Z;
  30. B = transpose(Z) * mat * Z;
  31. if (abs(B(2, 1)) < TOL)
  32. return
  33. endif
  34. Z = gram_schmidt(W);
  35. endwhile
  36. end
  37.  
  38.  
  39. %% test1: function description
  40. function [] = test1(i)
  41. % for i=2:arg
  42. sim_matrix=randi(20,i,i);
  43. sim_matrix=sim_matrix-tril(sim_matrix,-1)+triu(sim_matrix,1)';
  44. [z,b]=dwaww(sim_matrix)
  45.  
  46. all_eigenvalues= eig(sim_matrix)
  47. eigenvalues=-sort(-abs(eig(sim_matrix)))([1:2])
  48. my_eigenvalues= -sort(abs([b(1,1),b(2,2)]))
  49. if(norm(eigenvalues-my_eigenvalues)>0.1)
  50. error('eigenvalues are not equal');
  51. endif
  52. % endfor
  53. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement