1

I wrote the code according to the algorithm, but the result is incorrect. According to the algorithm, we must indicate the dimension of the matrix and manually fill in the main matrix A and vector B. We need to generate an LU matrix. It is generated, but with the wrong numbers. And in the end we have to get the vector X with solutions. And this is in windowed mode. https://i.stack.imgur.com/UzXHg.jpg

int N = 1; // matrix dimension
double R = 0;
typedef double Matrix [6][6];
typedef double Vec [6];
.
.
.
void Decomp (Matrix A, int N, int &Change)
{
int i, j, k ;
double R, L, U;
Change = 1;
R = Math::Abs(A[1][1]);
for(j=2; j<=N; j++)
    if (Math::Abs(A[j][1])>= R)
    {
        Change = j;
    R = Math::Abs(A[j][1]);
    }
    if (R<= 1E-7) 
    {
      MessageBox::Show("The system is degenerate");
    }
    if (k!=1)
    {
     for(i=1; i<=N; i++)
         { 
              R = A[Change][i];
              A[Change][i] = A[1][i];
               A[1][i] = R;
          }
    }
     for(i=2; i<=N; i++)
A[1][i] = A[1][i]/A[1][1];
for(i=2; i<=N; i++)
{
  for(k=i; k<=N; k++);
  {
    R = 0;
    for ( j=1; j<=(i-1); j++)
    R = R + A[k][j] * A[j][i];
    A[k][i] = A[k][i] - R;
  }
  if (A[i][i]<= 1E-7) 
    {
      MessageBox::Show("The system is degenerate[enter image description here][1]");
    }
  for(k = i+1; k<=N; k++)
  {
    R = 0;
    for (j=1; j<=(i-1); j++)
      R = R + A[i][j] * A[j][k];
    A[i][k] = (A[i][k] - R) / A[i][i];
  }
}
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
{
C_matrix_dgv->Rows[i]->Cells[j] -> Value = Convert::ToString(A[i+1][j+1]);
}
    }

void Solve (Matrix A, Vec b, Vec x, int Change, int N)
{
int i = 0,j = 0;
double R;
if (Change!=1)
{
  R = b[Change];
  b[Change] = b[1];
  b[1] = R;
}
b[1] = b[1]/A[1][1];
for(i=2; i<=N; i++)
{
  R = 0;
  for( j=1; j<=(i-1); j++)
    R = R + A[i][j] * b[j];
  b[i] = (b[i] - R) / A[i][i];
}
x[N] = b[N];
for( i=1; i<=(N-1); i++)
{
  R = 0;
  for(j = (N+1-i); j<=N; j++)
    R = R + A[N - i][j] * x[j];
  x[N - i] = b[N - i] - R;
}
}
Jeka X
  • 19
  • 3

1 Answers1

1
int N = 1; // matrix dimension

If you use this in the rest of the code you cannot get correct results. The dimension of the matrix is 6x6. Use a std::array or std::vector so that you dont need to keep the size in a seperate variable.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • 1
    I am new to programming, so can you throw off a piece of code or better all the code so that it works correctly, please? – Jeka X Nov 29 '19 at 18:52