Advertisement
Guest User

Untitled

a guest
Oct 21st, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. class Seidel
  2. {
  3. int n;
  4. double[] Xk, Xk1, B, D, X0; // X0 - начальное приближение
  5. double[,] A, C;
  6. double eps;
  7.  
  8. public Seidel(double[,] A, double[] B)
  9. {
  10. n = B.Length;
  11. this.A = A;
  12. this.B = B;
  13. eps = 0.001;
  14. C = new double[n, n];
  15. Xk = new double[n];
  16. Xk1 = new double[n];
  17. D = new double[n];
  18. X0 = new double[n]; //{ 2, 2, -2, 2, 1 };
  19.  
  20. for (int i = 0; i < n; i++)
  21. {
  22. for(int j = 0; j < n; j++)
  23. {
  24. if (i != j)
  25. {
  26. C[i, j] = -A[i, j] / A[i, i];
  27. }
  28. }
  29. D[i] = B[i] / A[i, i];
  30. Xk[i] = X0[i]; // Начальное приближение всех значений равно нулю
  31. Xk1[i] = X0[i];
  32. }
  33. }
  34.  
  35. public void solve()
  36. {
  37. do
  38. {
  39. iterate(Xk, Xk1);
  40. }
  41. while (!converge(Xk1, Xk));
  42.  
  43. print(Xk);
  44. }
  45.  
  46. bool converge(double[] Xk, double[] Xk1)
  47. {
  48. double norm = 0;
  49. for (int i = 0; i < n; i++)
  50. {
  51. norm += (Xk1[i] - Xk[i]) * (Xk1[i] - Xk[i]);
  52. }
  53. if (Math.Sqrt(norm) >= eps)
  54. return false;
  55. return true;
  56. }
  57.  
  58. void iterate(double[] Xk, double[] Xk1)
  59. {
  60. for (int i = 0; i < n; i++)
  61. {
  62. Xk[i] = Xk1[i];
  63. }
  64. for (int i = 0; i < n; i++)
  65. {
  66. double var = 0;
  67. for (int j = 0; j < i; j++)
  68. var += A[i, j] * Xk1[j];
  69.  
  70. for (int j = i + 1; j < n; j++)
  71. var += A[i, j] * Xk[j];
  72.  
  73. Xk1[i] = (B[i] - var) / A[i, i];
  74. }
  75. }
  76.  
  77. public void print(double[,] matrix)
  78. {
  79. for (int i = 0; i < n; i++)
  80. {
  81. for (int j = 0; j < n; j++)
  82. {
  83. Console.Write("{0,7:f3} ", matrix[i, j]);
  84. }
  85. Console.WriteLine();
  86. }
  87. Console.WriteLine();
  88. }
  89.  
  90. public void print(double[] vector)
  91. {
  92. for (int i = 0; i < n; i++)
  93. {
  94. Console.WriteLine("{0,7:f3} ", vector[i]);
  95. }
  96. Console.WriteLine();
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement