My achievements of the program, but it does not work correctly. Tell me, please, what is wrong in the algorithm and in general?

void __fastcall TForm1::Button2Click(TObject* Sender) { x = StrToFloat(Edit1->Text); y = StrToFloat(Edit2->Text); z = StrToFloat(Edit3->Text); esp = StrToFloat(Edit4->Text); double a[n][n], a1[n][n], b[n], det, invdet, f[n], K[n]; //a[3][3]-матрица Якоби; //a1[3][3]-обратная матрица; b[3]-произведение а1[3] на f[3]; f[3]-массив функций; K[3]-массив переменных double xk, yk, zk, xn, yn, zn, max; //изменение значения переменных при k=0,1,2.. int k = 0; //кол-во итераций //Матрица Якоби { a[0][0] = (-2 * x - 1); a[0][1] = (2 * z); a[0][2] = (2 * y); a[1][0] = (-3 * z); a[1][1] = (2 * y - 1); a[1][2] = (-3 * x); a[2][0] = (-2 * y); a[2][1] = (-2 * x); a[2][2] = (-2 * z - 1); //Определитель det = a[0][0] * a[1][1] * a[2][2] + a[0][1] * a[1][2] * a[2][0] + a[0][2] * a[1][0] * a[2][1] - a[0][2] * a[1][1] * a[2][0] - a[0][0] * a[1][2] * a[2][1] - a[0][1] * a[1][0] * a[2][2]; invdet = 1 / det; //Обратная матрица for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { a1[0][0] = a[1][1] * a[2][2] - a[1][2] * a[2][1] * invdet; a1[0][1] = -a[1][0] * a[2][2] + a[2][0] * a[1][2] * invdet; a1[0][2] = a[1][0] * a[2][1] - a[1][1] * a[2][0] * invdet; a1[1][0] = -a[0][1] * a[2][2] + a[0][2] * a[2][1] * invdet; a1[1][1] = a[0][0] * a[2][2] - a[0][2] * a[2][0] * invdet; a1[1][2] = -a[0][0] * a[2][1] + a[0][1] * a[2][0] * invdet; a1[2][0] = a[0][1] * a[1][2] - a[0][1] * a[1][1] * invdet; a1[2][1] = -a[0][0] * a[1][2] + a[0][2] * a[2][0] * invdet; a1[2][2] = a[0][0] * a[1][1] - a[0][1] * a[1][0] * invdet; } } //Массив функций for (j = 0; j < n; j++) { f[0] = f1(x, y, z); f[1] = f2(x, y, z); f[2] = f3(x, y, z); } //Массив переменных for (j = 0; j < n; j++) { K[0] = x; K[1] = y; K[2] = z; } do { for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { b[0] = a[0][0] * f[0] + a[0][1] * f[1] + a[0][2] * f[2]; b[1] = a[1][0] * f[0] + a[1][1] * f[1] + a[1][2] * f[2]; b[2] = a[2][0] * f[0] + a[2][1] * f[1] + a[2][2] * f[2]; } } xk = K[0] - b[0]; yk = K[1] - b[1]; zk = K[2] - b[2]; xn = xk - x; yn = yk - y; zn = zk - z; { if (abs(xn) > abs(yn)) { if (abs(xn) > abs(zn)) { max = xn; } else { max = zn; } } else { if (abs(yn) > abs(zn)) { max = yn; } else { max = zn; } } } x = xk; y = yk; z = zk; } while (abs(max) >= esp); k++; Edit5->Text = FloatToStr(xk); Edit6->Text = FloatToStr(yk); Edit7->Text = FloatToStr(zk); Edit8->Text = FloatToStr(k); } } 
  • Formatting needs to be corrected, and then 2 level closing braces look straight out. It would be nice to take out the logic in a separate function in order not to mix logic and data input-output (Edit5-> Text = FloatToStr (xk)) - Schullz
  • ok, this is understandable, thanks to me b to understand where the error is in the algorithm ( - user209898
  • one
    @emrvs, if you want to get help, it would be good not to force others to break their eyes, at least. And to describe the problem in more detail, and not limited to the phrase, она неправильно работает . Which dataset? What result is expected / obtained? Etc. - αλεχολυτ

2 answers 2

To begin with, it is not clear why you are 9 times (I think, you have n defined above as const int n = 3 , so?) Repeat the same calculations?

 //Обратная матрица for (i=0; i<n; i++){ for (j=0; j<n; j++){ a1[0][0]= a[1][1]*a[2][2]-a[1][2]*a[2][1]*invdet; a1[0][1]=-a[1][0]*a[2][2]+a[2][0]*a[1][2]*invdet; a1[0][2]= a[1][0]*a[2][1]-a[1][1]*a[2][0]*invdet; a1[1][0]=-a[0][1]*a[2][2]+a[0][2]*a[2][1]*invdet; a1[1][1]= a[0][0]*a[2][2]-a[0][2]*a[2][0]*invdet; a1[1][2]=-a[0][0]*a[2][1]+a[0][1]*a[2][0]*invdet; a1[2][0]= a[0][1]*a[1][2]-a[0][1]*a[1][1]*invdet; a1[2][1]=-a[0][0]*a[1][2]+a[0][2]*a[2][0]*invdet; a1[2][2]= a[0][0]*a[1][1]-a[0][1]*a[1][0]*invdet; } } 

Yes, and besides the wrong? Is the inverse matrix calculated by such formulas? At least you forgot about brackets; I did not check the correctness of the indexes.

Well, and then I didn’t check it yet - once the calculations do not go right away, it’s clear that no matter what happens next, there’s no sense in it ...

  • It would be highly desirable to ask you either to give the variables human names or to accompany the code with comments. Otherwise, what you have written looks quite on scholarsk and can not be parsed. It would be nice to bring the functions, the roots of which you hope to find. - BuilderC
  1. The determinant is calculated normally.
  2. invdet must be multiplied by the bracket with an algebraic complement (Harry is right).
  3. There are errors in algebraic additions, should be:
    a[2][0] = ( a[0][1]*a[1][2]-a[1][1]*a[0][2])*invdet;
    a[2][1] = (-a[0][0]*a[1][2]+a[1][0]*a[0][2])*invdet;

  4. If you need help on the functions, you should describe the source data and the algorithm itself (give a name and a clear reference).