Advertisement
SonicDesu

Metoda Jacobiego

Dec 16th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. clear;
  2. clc;
  3. close all;
  4.  
  5. A = [2 2 2
  6. 2 5 7
  7. 2 7 9]; %Macierz z polecenia zadania
  8. X = [1 0 0
  9. 0 1 0
  10. 0 0 1]; %Macierz jednostkowa
  11. k=12; %Ilosc kroków
  12. for g=1:k
  13.  
  14. [i_end,j_end]=size(A); %Dostaję wymiar macierzy
  15. max_A=0;
  16. p=1;
  17. q=1;
  18. for i=1:i_end %Pętle sterujące po macierzy A
  19. for j=1:j_end
  20. if(i~=j && abs(A(i,j))>max_A)
  21. p=i;
  22. q=j;
  23. max_A=abs(A(i,j));
  24. end
  25. end
  26. end
  27.  
  28.  
  29. u=(A(p,p)-A(q,q))/2;
  30. v=sqrt(A(p,q)*A(p,q)+u^2);
  31.  
  32. cos_th=sqrt((abs(u)+v)/(2*v));
  33. sin_th=-1*A(p,q)*sign(u)/(2*v*cos_th);
  34.  
  35. for i=1:i_end
  36. if(i~=p&&i~=q)
  37. x=A(i,p)*cos_th-A(i,q)*sin_th
  38. y=A(i,p)*sin_th+A(i,q)*cos_th
  39. A(i,p)=x;
  40. A(i,q)=y;
  41. end
  42. end
  43.  
  44. for j=1:j_end
  45. if(j~=p&&j~=q)
  46. x=A(p,j)*cos_th-A(q,j)*sin_th;
  47. y=A(p,j)*sin_th+A(q,j)*cos_th;
  48. A(p,j)=x;
  49. A(q,j)=y;
  50. end
  51. end
  52.  
  53. x=A(p,p)*cos_th*cos_th+A(q,q)*sin_th*sin_th-2*A(p,q)*sin_th*cos_th;
  54. y=A(p,p)*sin_th*sin_th+A(q,q)*cos_th*cos_th+2*A(p,q)*sin_th*cos_th;
  55. z=(A(p,p)-A(q,q))*sin_th*cos_th+A(p,q)*(cos_th*cos_th-sin_th*sin_th);
  56. A(p,p)=x;
  57. A(q,q)=y;
  58. A(q,p)=z;
  59. A(p,q)=z;
  60.  
  61. for i=1:i_end
  62. x = X(i,p)*cos_th-X(i,q)*sin_th;
  63. y=X(i,p)*sin_th+X(i,q)*cos_th;
  64. X(i,p)=x;
  65. X(i,q)=y;
  66. end
  67.  
  68.  
  69. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement