Advertisement
tojooko

MN 8 v0.5

May 19th, 2013
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. /*
  2. najwiekszy moduł:
  3. wyjście: lamba, x, N
  4. zbieżność ?
  5. N = f (eps)
  6. czy zbieżność zależy od y0
  7.  
  8. Kryłow: - rozmiar n
  9. y0,..,yn -> ukl. rownan -> p1,...,pn -> r char -> lamba -> X
  10.  
  11. najlepiej Gauss/Kramer
  12. A = [2 -2 3; 1 1 1; 1 3 -1];
  13. B - [3 -4 3; -4 6 3; 3 3 1];
  14. C = [3 30 -48; 3 14 -24; 3 15 -25];
  15. ROZWIAZANIE ANALITYCZNE BY WOLFRAM!
  16.  
  17. */
  18.  
  19. #include <iostream>
  20. #include <cmath>
  21. #include <vector>
  22.  
  23.  
  24. using namespace std;
  25.  
  26. vector<float> Gauss(float** in, int n);
  27.  
  28. int main()
  29. {
  30. const int n = 3; // Rzad macierzy
  31. float A[n][n]={{1,1,1},{1,2,3},{1,3,6}};//{{2, -2, 3}, {1 ,1 ,1}, {1,3,-1}};
  32. float *y0 = new float[n];
  33. float Y[n][n+1];
  34.  
  35.  
  36.  
  37. Y[0][0] = y0[0] = 1;
  38. for(int i=1;i<n;i++)
  39. Y[i][0] = y0[i] = 0;
  40. float* yp = y0;
  41. float* y = new float[n]; //nowy wektor y
  42. for(int k=1;k<n+1;k++)
  43. {
  44. for(int i=0;i<n;i++)
  45. y[i] = 0;
  46.  
  47. for(int i=0;i<n;i++)
  48. {
  49. for(int j=0;j<n;j++)
  50. y[i] += A[i][j]*yp[j];
  51. Y[i][k] = y[i];
  52. }
  53. float* temp = yp;
  54. yp = y;
  55. y = temp;
  56. }
  57. delete[] y,y0;
  58.  
  59. for(int i=0;i<n;i++)
  60. Y[i][n] *=-1;
  61.  
  62.  
  63. for(int i=0;i<n;i++)
  64. {
  65. for(int j=0;j<n+1;j++)
  66. cout<<Y[i][j]<<' ';
  67. cout<<'\n';
  68. }
  69.  
  70. float** C = new float*[n];
  71. for(int i=0;i<n;i++)
  72. {
  73. C[i] = new float[n+1];
  74. for(int j=0;j<n+1;j++)
  75. C[i][j] = Y[i][j];
  76. }
  77. vector<float> out;
  78. try{
  79. out = Gauss(C,n);
  80. }
  81. catch(int x)
  82. {
  83. if(x == 1)
  84. cout<<"Nie ma pojedynczego rozwiazania ukladu\n";
  85.  
  86. }
  87. for(int i=0;i<n;i++)
  88. cout<<out[i]<<' ';
  89. for(int i=0;i<n;i++)
  90. delete[] C[i];
  91. delete[] C;
  92.  
  93. system("pause");
  94. return 0;
  95. }
  96.  
  97.  
  98. vector<float> Gauss(float** in, int n)
  99. {
  100. vector<float> out;
  101. float** A = new float*[n];
  102. for(int i=0;i<n;i++)
  103. {
  104. A[i] = new float[n + 1];
  105. for(int j=0;j<n+1;j++)
  106. A[i][j] = in[i][j];
  107. }
  108. for(int s=0;s<n;s++) // dla kazdego wiersza
  109. {
  110. if(A[s][s] == 0) //Nalezy zamienic kolejnosc wierszy
  111. {
  112. int w = -1;
  113. for(int i=s+1;i<n;i++)
  114. if(A[i][s] != 0)
  115. {
  116. w = i;
  117. break;
  118. }
  119. if(w == -1)
  120. throw 1;
  121. float temp;
  122. for(int i=0;i<n;i++)
  123. {
  124. temp = A[s][i];
  125. A[s][i] = A[w][i];
  126. A[w][i] = temp;
  127. }
  128. }
  129. for(int i=s+1;i<n;i++) //dla każdego niższego wiersza
  130. {
  131.  
  132. for(int j=0;j<n + 1;j++) //dla każdego elementu
  133. if(j!= s)
  134. A[i][j] -= A[s][j] * A[i][s]/A[s][s];
  135. A[i][s] = 0;
  136. }
  137.  
  138. }
  139.  
  140. for(int s=n-1;s>0;s--) // dla kazdego wiersza
  141. {
  142. for(int i=0;i<s ;i++) //dla każdego wyższego wiersza
  143. {
  144. for(int j=s;j<n + 1;j++) //dla każdego elementu
  145. //cout<<s<<' '<<i<<' '<<j<<'\n';
  146. if(j!= s)
  147. A[i][j] -= A[s][j] * A[i][s]/A[s][s];
  148. A[i][s] = 0;
  149. }
  150.  
  151. }
  152.  
  153.  
  154. for(int i=0;i<n;i++)
  155. out.push_back(A[i][n] / A[i][i]);
  156.  
  157. for(int i=0;i<n;i++)
  158. delete[] A[i];
  159. delete[] A;
  160. return out;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement