Выделить код
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
class Program
{ static void Main(string[] args)
{ double[,] Arr = { {8.3, 4.12, 4.1 }, {-0.23, 6.39, 2.73}, {-0.035, 0.065, 1.895} };
double[] Arr1 = { -9.15, 16.785, -9.6525 }; double e = 0.01; double[] arr1; Show(Arr); Arr = Sys(Arr, Arr1, out arr1); Show(Arr); Show1(arr1); Console.WriteLine(); Approximation(Arr, arr1, e, 1); Console.WriteLine(); Console.ReadKey(); } private static double[,] Sys(double[,] matrix, double[] arr1, out double[] arr2) { double[,] result = new double[matrix.GetLength(0), matrix.GetLength(1)]; arr2 = new double[arr1.GetLength(0)]; for (int i = 0; i < matrix.GetLength(0); i++) { arr2[i] = arr1[i] / matrix[i, i]; for (int j = 0; j < matrix.GetLength(1); j++) { if (i != j) result[i, j] = -(matrix[i, j] / matrix[i, i]); else result[i, j] = 0; } } return result; } private static void Approximation(double[,] matrix, double[] b, double e, int n) { int count = 0; double[] x = new double[b.GetLength(0)]; double[] x0 = new double[b.GetLength(0)]; double sum = 0; for (int root = 0; root < b.GetLength(0); root++) { count = 0; for (int i = 0; i < b.GetLength(0); i++) { x0[i] = b[i]; } for (int i = 0; i < matrix.GetLength(0); i++) { for (int j = 0; j < 3; j++) { sum = matrix[i, j] * x0[j] + sum; } x[i] = b[i] + sum; sum = 0; } while (Math.Abs(x[root] - x0[root]) >= e) { for (int k = 0; k < matrix.GetLength(0); k++) { x0[k] = x[k]; } for (int i = 0; i < matrix.GetLength(0); i++) { for (int j = 0; j < 3; j++) { sum = matrix[i, j] * x0[j] + sum; } x[i] = b[i] + sum; sum = 0; } Console.WriteLine(x[root]); count++; } Console.Write("x[" + root + "]="); Console.WriteLine(x[root]); Console.WriteLine(count); Console.WriteLine(); } } private static void Show(double[,] matrix) { for (int i = 0; i < matrix.GetLength(0); i++) { for (int j = 0; j < matrix.GetLength(1); j++) { Console.Write("\t"); Console.Write(matrix[i, j]); } Console.WriteLine(); } } private static void Show1(double[] matrix) { for (int i = 0; i < matrix.GetLength(0); i++) { Console.Write("\t"); Console.WriteLine(matrix[i]); } } }
0
21.03.2018, 01:28
Do'stlaringiz bilan baham: |