using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Jakobi { class Method { public double [,] _Amatrix = new double[,] { { 2, 2, 10 }, { 10, 1, 1 }, { 2, 10, 1 } }; public double [] _Bmatrix = new double[] { 14, 12, 13 }; public double[] _B; public double[] _C; public double[] _A; public double E = 0.01; public void prepare() { for (int i = _Amatrix.GetLength(0) - 1; i >= 0; i--) { for (int j = _Amatrix.GetLength(1) - 1; j >= 0; j--) { double sum = 0; if (_Amatrix[i, i] != _Amatrix[i, j]) { sum += _Amatrix[i, j]; } if (_Amatrix[i, i] < sum) { for (int k = _Amatrix.GetLength(1) - 1; k >= 0; k--) { if (_Amatrix[i, i] < _Amatrix[k, i]) { double[] res = new double[_Amatrix.GetLength(0)]; for (int s = 0; s < _Amatrix.GetLength(0); s++) { res[s] = _Amatrix[k, s]; _Amatrix[k, s] = _Amatrix[i, s]; _Amatrix[i, s] = res[s]; res[s] = _Bmatrix[k]; _Bmatrix[k] = _Bmatrix[i]; _Bmatrix[i] = res[s]; } } } } } } } public void getB() { _B = new double[_Amatrix.GetLength(0)]; for (int i = 0; i < _Bmatrix.Length; i++) { _B[i] = _Bmatrix[i]; } } public void zeroing() { for (int i = 0; i < _Amatrix.GetLength(0); i++) { double data = _Amatrix[i, i]; for (int j = 0; j < _Amatrix.GetLength(1); j++) { if (_Amatrix[i, j] == _Amatrix[i, i]) { _Amatrix[i, j] = 0; } else { _Amatrix[i, j] /= -data; } } _Bmatrix[i] /= data; } } public void Iteration() { _C = new double[_Amatrix.GetLength(0)]; _A = new double[_Amatrix.GetLength(0)]; for (int i = 0; i < _Bmatrix.Length; i++) { _A[i] = _Bmatrix[i]; } for (int i = 0; i < _Amatrix.GetLength(0); i++) { double sum = 0; for (int j = 0; j < _Amatrix.GetLength(1); j++) { sum += _Amatrix[i, j] * _Bmatrix[j]; } _C[i] = sum + _B[i]; } for(int i = 0; i < _Bmatrix.Length; i++) { _Bmatrix[i] = _C[i]; } } public void showAmatrix() { Console.WriteLine(); for(int i = 0; i < _Amatrix.GetLength(0); i++) { for(int j =0 ; j < _Amatrix.GetLength(1); j++) { Console.Write("{0}\t", _Amatrix[i, j]); } Console.WriteLine("|\t{0}", _Bmatrix[i]); } Console.WriteLine(); } public double convergence() { return Math.Abs(_A[_Bmatrix.Length - 1] - _C[_Bmatrix.Length - 1]); } public void showC() { Console.WriteLine("C:"); for (int i = 0; i < _C.Length; i++) { Console.Write("{0}\t", _C[i]); } Console.WriteLine(); } public void showB() { Console.WriteLine("B:"); for (int i = 0; i < _B.Length; i++) { Console.Write("{0}\t", _B[i]); } Console.WriteLine(); } public void showA() { Console.WriteLine("A:"); for (int i = 0; i < _A.Length; i++) { Console.Write("{0}\t", _A[i]); } Console.WriteLine(); } } class Program { static void Main(string[] args) { Method method = new Method(); method.showAmatrix(); method.prepare(); method.showAmatrix(); method.zeroing(); method.showAmatrix(); method.getB(); int k = 1; Console.WriteLine("{0} итерация", k); method.Iteration(); method.showAmatrix(); k++; Console.WriteLine("----------------------------------------"); while (method.convergence() > method.E) { Console.WriteLine("{0} итерация", k); method.Iteration(); method.showAmatrix(); Console.WriteLine("{0:f5}", method.convergence()); Console.WriteLine("----------------------------------------"); k++; } Console.ReadKey(); } } }