Palson

Gauss

Mar 12th, 2019
51
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public static class MetodaGaussa
  2. {
  3. public static int ObliczanieMetodaGaussa(double[,] A, double[] B, double[] X)
  4. {
  5.  
  6.  
  7.  
  8. //Wybór Elementu Głównego
  9. double T, ZS, MA;
  10. int k, i, j, N;
  11. N = A.GetLength(0) - 1;
  12. for (i = 1; i <= N; i++)
  13. {
  14. T = Math.Abs(A[i, i]); k = i;
  15. for (j = i + 1; j <= N; j++)
  16. {
  17. MA = Math.Abs(A[j, i]);
  18. if (MA > T)
  19. {
  20. T = MA;
  21. k = j;
  22. }
  23. }
  24. ZS = B[k];
  25. B[k] = B[i];
  26. B[i] = ZS;
  27. for (j = N; j >= 1; j--)
  28. {
  29. ZS = A[k, j];
  30. A[k, j] = A[i, j];
  31. A[i, j] = ZS;
  32. }
  33.  
  34. }
  35.  
  36. //Przekształcenie Macierzy
  37. double m;
  38. for (i = 1; i <= N; i++)
  39. {
  40. for (j = i + 1; j <= N; j++)
  41. {
  42. m = A[j, i] / A[i, i];
  43. B[j] -= B[i] * m;
  44. for (k = 1; k <= N; k++)
  45. A[j, k] -= A[i, k] * m;
  46. }
  47. }
  48. //Metoda Eliminacji Wstecznej
  49. double Z;
  50. for (i = N; i >= 1; i--)
  51. {
  52. Z = B[i];
  53. for (j = i + 1; j <= N; j++)
  54. Z -= A[i, j] * X[j];
  55. X[i] = Z / A[i, i];
  56.  
  57. }
  58.  
  59. for (j = 1; j <= B.GetLength(0) - 1; j++)
  60. {
  61.  
  62. }
  63.  
  64. return 0;
  65.  
  66. }
  67.  
  68.  
  69. public static void ObliczanieMetodaGaussa(Complex[,] AZ, Complex[] BZ, Complex[] XZ)
  70. {
  71. //Wybór Elementu Głównego
  72. Complex T, ZS, MA;
  73. int k, i, j, N;
  74. N = AZ.GetLength(0) - 1;
  75. for (i = 1; i <= N; i++)
  76. {
  77. T = Complex.Abs(AZ[i, i]); k = i;
  78. for (j = i + 1; j <= N; j++)
  79. {
  80. MA = AZ[j, i];
  81. if (Complex.Abs(MA) > Complex.Abs(T))
  82. {
  83. T = MA;
  84. k = j;
  85. }
  86. }
  87. ZS = BZ[k];
  88. BZ[k] = BZ[i];
  89. BZ[i] = ZS;
  90. for (j = N; j >= 1; j--)
  91. {
  92. ZS = AZ[k, j];
  93. AZ[k, j] = AZ[i, j];
  94. AZ[i, j] = ZS;
  95. }
  96.  
  97. }
  98.  
  99. //Przekształcenie Macierzy
  100. Complex m;
  101. for (i = 1; i <= N; i++)
  102. {
  103. for (j = i + 1; j <= N; j++)
  104. {
  105. m = AZ[j, i] / AZ[i, i];
  106. BZ[j] -= BZ[i] * m;
  107. for (k = 1; k <= N; k++)
  108. AZ[j, k] -= AZ[i, k] * m;
  109. }
  110. }
  111. //Metoda Eliminacji Wstecznej
  112. Complex ZZ;
  113. for (i = N; i >= 1; i--)
  114. {
  115. ZZ = BZ[i];
  116. for (j = i + 1; j <= N; j++)
  117. ZZ -= AZ[i, j] * XZ[j];
  118. XZ[i] = ZZ / AZ[i, i];
  119.  
  120. }
  121.  
  122. for (j = 1; j <= BZ.GetLength(0) - 1; j++)
  123. {
  124.  
  125. }
  126.  
  127.  
  128.  
  129.  
  130. }
  131.  
  132.  
  133. }
RAW Paste Data