fork download
  1. using System;
  2.  
  3. public class Test
  4. {
  5. public static void Main()
  6. {
  7. int N = 10;
  8. double p = 0.3 * N, t = 0.2 * N;
  9. int count = 0;
  10. double[,] A, B;
  11. A= new double[,]{
  12. {12.5,1.4,1.1+p,0.7,-7+t},
  13. {1.2,10,1.5,1.7+p,6-0.2*t},
  14. {1.1,2.5-p,11.2,1.3,10.3},
  15. {0.7,2,3.1,15,-5-t}};
  16. B = new double[4, 5];
  17. double[] x, b;
  18. x = new double[4];
  19. b = new double[4] { 0, 0, 0, 0 };
  20. Console.WriteLine("Startup matrix:");
  21. for (int i = 0; i < 4; i++)
  22. for (int j = 0; j < 5; j++)
  23. {
  24. Console.Write(A[i, j] + "\t");
  25. count++;
  26. if (count == 4) Console.Write("=");
  27. if (count == 5){Console.Write("\n");count = 0;}
  28. } //вывести на экран наш массив
  29. Console.WriteLine();
  30. double[,] IterationA, IterationB;
  31. IterationA = new double[4, 5];
  32. IterationB = new double[4, 5];
  33. int IterationNum = 0, Iteration = 1;
  34. double alfa, epsilon =-1;
  35. double iter, check=0; //||x(k+1)-x(k)||
  36.  
  37. Console.WriteLine("Iteration #" + Iteration);
  38. for (int i = 0; i < 4; i++) //ищем х1,2,3,4
  39. for (int j = 0; j < 5; j++)
  40. {
  41. if (Iteration == 1) B[i, j] = A[i, j]; //Первое получение значений массива А
  42. if (i == j) B[i, j] = 0;
  43. else
  44. {
  45. if (j < 3) B[i, j] = -B[i, j] / A[i, i]; //x[1..3]=-b[i]/a[i]
  46. else B[i, j] = B[i, j] / A[i, i]; //x[4](beta)=b[i]/a[i,i]
  47. }
  48. Console.Write(Math.Round(B[i, j], 4) + "\t");
  49. count++;
  50. if (count == 4) Console.Write("=");
  51. if (count == 5) { Console.Write("\n"); count = 0; }
  52. } //массив B
  53.  
  54. for (int i = 0; i < 4; i++) b[i] = B[i, 4]; //beta(1,2,3,4)
  55.  
  56. for (int i = 0; i < 4; i++)
  57. for (int j = 0; j < 5; j++)
  58. {
  59. if (j < 4) x[i] += B[i, j] * b[i];
  60. else x[i] += B[i, j];
  61. } Console.WriteLine(); //значения x
  62.  
  63. //alfa (делается 1 раз)
  64. double[] elem = new double[4]; //значения при х(k)
  65. for (int i = 0; i < 4; i++)
  66. for (int j = 0; j < 4; j++)
  67. {
  68. elem[i] += B[i, j];
  69. }
  70. alfa = elem[0]; //||a||
  71. for (int i = 1; i < 4; i++)
  72. if (Math.Abs(elem[i]) > alfa) alfa = Math.Abs(elem[i]);
  73. Console.WriteLine("a=" + alfa);
  74. //
  75.  
  76.  
  77. if (Iteration%2!=0)
  78. {
  79. IterationA = B;
  80. IterationNum = 1;
  81. }
  82. else
  83. {
  84. IterationB = B;
  85. IterationNum = 0;
  86. }
  87. //первая итерация
  88.  
  89. while (check > epsilon)
  90. {
  91. epsilon = 0.0001;
  92. Iteration++;
  93. if (Iteration>11)
  94. {
  95. Console.WriteLine("TOO MUCH ITERATIONS");
  96. break;
  97. }
  98. Console.WriteLine("Iteration #" + Iteration);
  99.  
  100. for (int i = 0; i < 4; i++) //ищем х1,2,3,4
  101. for (int j = 0; j < 5; j++)
  102. {
  103. if (Iteration == 1) B[i, j] = A[i, j]; //Первое получение значений массива А
  104. if (i == j) B[i, j] = 0;
  105. else{
  106. if (j < 3) B[i, j] = -B[i, j] / A[i, i]; //x[1..3]=-b[i]/a[i]
  107. else B[i, j] = B[i, j] / A[i, i]; //x[4](beta)=b[i]/a[i,i]
  108. }
  109. Console.Write(Math.Round(B[i, j], 4) + "\t");
  110. count++;
  111. if (count == 4) Console.Write("=");
  112. if (count == 5){Console.Write("\n"); count = 0;}
  113. } //массив B
  114.  
  115. for (int i = 0; i < 4; i++) b[i] = B[i, 4]; //beta(1,2,3,4)
  116.  
  117. for (int i = 0; i < 4; i++)
  118. for (int j = 0; j < 5; j++){
  119. if (j < 4) x[i] += B[i, j] * b[i];
  120. else x[i] += B[i, j];
  121. } Console.WriteLine(); //значения x
  122.  
  123. Console.WriteLine("Критичне місце");
  124. if (Iteration%2!=0){
  125. Console.WriteLine("Гілка A");
  126. IterationA = B;
  127. IterationNum = 1;
  128. }
  129. else{
  130. Console.WriteLine("Гілка B");
  131. IterationB = B;
  132. IterationNum = 0;
  133. }
  134.  
  135.  
  136. if (Iteration > 1)
  137. {
  138. double[] elem0 = new double[4]; //значения при х(k)
  139. double[] elem1 = new double[4]; //значения при х(k+1)
  140. for (int i = 0; i < 4; i++)
  141. for (int j = 0; j < 4; j++)
  142. {
  143. elem0[i] += IterationA[i, j];
  144. elem1[i] += IterationB[i, j];
  145. }
  146. for (int i = 0; i < 4; i++)
  147. for (int j = 0; j < 5; j++)
  148. {
  149. Console.Write(IterationA[i, j] + "\t");
  150. count++;
  151. if (count == 4) Console.Write("=");
  152. if (count == 5) { Console.Write("\n"); count = 0; }
  153. } //вывести на экран наш массив IterationA
  154. for (int i = 0; i < 4; i++)
  155. for (int j = 0; j < 5; j++)
  156. {
  157. Console.Write(IterationB[i, j] + "\t");
  158. count++;
  159. if (count == 4) Console.Write("=");
  160. if (count == 5) { Console.Write("\n"); count = 0; }
  161. } //вывести на экран наш массив IterationB
  162. iter = -10;
  163. for (int i = 0; i < 4; i++)
  164. if (Math.Abs(elem1[i] - elem0[i]) > iter)
  165. iter = Math.Abs(elem1[i] - elem0[i]);
  166. //
  167. check = iter;
  168. Console.WriteLine("alfa = {0},\n||x(k+1)-x(k)|| = {1}", alfa, iter);
  169. }
  170. for (int i = 0; i < 4; i++)
  171. Console.WriteLine("x[{0}] = {1}", i + 1, x[i]);
  172. Console.WriteLine();
  173. }
  174.  
  175. }
  176. }
Success #stdin #stdout 0.04s 24208KB
stdin
Standard input is empty
stdout
Startup matrix:
12.5	1.4	4.1	0.7	=-5	
1.2	10	1.5	4.7	=5.6	
1.1	-0.5	11.2	1.3	=10.3	
0.7	2	3.1	15	=-7	

Iteration #1
0	-0.112	-0.328	0.056	=-0.4	
-0.12	0	-0.15	0.47	=0.56	
-0.0982	0.0446	0	0.1161	=0.9196	
-0.0467	-0.1333	-0.2067	0	=-0.4667	

a=0.386666666666667
Iteration #2
0	0.009	0.0262	0.0045	=-0.032	
0.012	0	0.015	0.047	=0.056	
0.0088	-0.004	0	0.0104	=0.0821	
0.0031	0.0089	0.0138	0	=-0.0311	

Критичне місце
Гілка B
0	0.00896	0.02624	0.00448	=-0.032	
0.012	0	0.015	0.047	=0.056	
0.00876913265306123	-0.0039859693877551	0	0.0103635204081633	=0.0821109693877551	
0.00311111111111111	0.00888888888888889	0.0137777777777778	0	=-0.0311111111111111	
0	0.00896	0.02624	0.00448	=-0.032	
0.012	0	0.015	0.047	=0.056	
0.00876913265306123	-0.0039859693877551	0	0.0103635204081633	=0.0821109693877551	
0.00311111111111111	0.00888888888888889	0.0137777777777778	0	=-0.0311111111111111	
alfa = 0.386666666666667,
||x(k+1)-x(k)|| = 0
x[1] = -0.27966976
x[2] = 0.732144
x[3] = 1.06047521398148
x[4] = -0.318135308641975