The last fragment remains at the iteration stage:

double A [2][3] = {{ -5, -1, -4}, { -4, 4, -5}}; //матрица с коэффициентами при двух неизвестных double epsilon = 0.001; double x[3], y[3]; // массивы для значений x и y x[0] = 0, y[0] = 0; i = 0; do { x[i + 1] = (A[i][i + 2] - A[i][i + 1] * y[i]) / A[i][i]; y[i + 1] = (A[i + 1][i + 2] - A[i + 1][i] * x[i + 1]) / A[i + 1][i + 1]; cout << "Решение на " << i << "-ой " << "итерации - " << "x = " << round(x[i + 1] * 10000) / 10000 << ", y =" << round( y[i + 1] * 10000) / 10000 << "\n"; i++; } while (abs(x[i + 1]) < epsilon && i < 3); 

The values ​​on the first iteration correctly calculates x [1] = -0.8, y [1] = -2.05, and the subsequent iterations displays with an error. I understand that you need to correctly assign the previous value of x and y as follows.

  • The values ​​on the first iteration correctly calculates x [1] = -0.8, y [1] = -2.05, and the subsequent iterations displays with an error. I understand that you need to correctly assign the previous value of x and y as follows. - user6448834
  • All important points on the issue should be included in the body of the question itself, and not in the comments. - αλεχολυτ
  • You see, you have written such nonsense that correcting it is the same as grinding a plane out of a metal bar with a file. It must be rewritten . Already with i==1 you refer to an element, for example, A[2][3] - well, and where is it with you? And with i==2 everything is even worse ... And all this in no way corresponds to the stated method. - Harry
  • @Harry, thank you very much, I figured out with your help. I'm still completely new - user6448834

1 answer 1

In fact, if you have not for the general case, but for two variables, it is enough like this:

 double A [2][3] = {{ -5, -1, -4}, { -4, 4, -5}}; //матрица с коэффициентами при двух неизвестных const double epsilon = 0.0001; int main() { double x = 0, y = 0; double norm = 2*epsilon; for(int i = 1;norm >= epsilon;++i) { double x1 = (A[0][2]-A[0][1]*y)/A[0][0]; double y1 = (A[1][2]-A[1][0]*x1)/A[1][1]; norm = abs(x1-x)+abs(y1-y); x = x1; y = y1; cout << "Iteration " << i << ": " << "x = " << setw(8) << x << " y = " << setw(8) << y << endl; } } 

Conclusion:

 Iteration 1: x = 0.8 y = -0.45 Iteration 2: x = 0.89 y = -0.36 Iteration 3: x = 0.872 y = -0.378 Iteration 4: x = 0.8756 y = -0.3744 Iteration 5: x = 0.87488 y = -0.37512 Iteration 6: x = 0.875024 y = -0.374976 Iteration 7: x = 0.874995 y = -0.375005 
  • @alexolut 32, ok, excuse me, not so long ago here, before I asked everything on cyber forum - user6448834
  • you are my savior ...)) now I finally understand what the error is)) - user6448834
  • Well, then mark the answer as accepted :) - Harry