Matrix inverse with LU decomposition in C++

In summary, the problem is to find the inverse of a 3x3 matrix using LU Decomposition with C++ command, with the numbers designated. In my case, my numbers for the matrix are '306 410780'
  • #1
KWKWII
4
0

Homework Statement


the problem is to find the inverse of a 3x3 matrix using LU Decomposition with C++ command, with the numbers designated. in my case, my numbers for the matrix are
'306
410
780'

Code:
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <math.h>
using namespace std;

int main(void)
{
    int e, i, j, k, y;
    float A[3][4] = {{3,0,6,1},{4,1,0,2},{7,8,0,3}};
    float x[3][3], c, sum;
    float L[3][4] = {{1,0,0,0},{0,1,0,0},{0,0,1,0}};
        for(j=0; j<=2; j++)
        {
            for(i=0; i<=2; i++)
            {
                if(i>j)
                {
                    c=A[i][j]/A[j][j];
                    L[i][j] = c;
                    for(k=0; k<=2; k++)
                    {
                        A[i][k]=A[i][k]-c*A[j][k];
                    }
                }
            }
        }
             
        for(y=0; y<=2; y++)
        {
            if(y==0)
            {
                    L[0][3] = 1;
                L[1][3] = 0;
                L[2][3] = 0;
            }
            else if (y==1)
            {
                    L[0][3] = 0;
                L[1][3] = 1;
                L[2][3] = 0;
            }
            else if (y==2)
            {
                    L[0][3] = 0;
                L[1][3] = 0;
                L[2][3] = 1;
            }
            A[0][3]=L[0][3]/L[0][0];
            for(i=1; i<=2; i++)
            {
                sum=0;
                for(j=i-1; j>=0; j--)
                {
                    sum=sum+L[i][j]*A[j][3];
                }
                A[i][3]=(L[i][3]-sum)/L[i][i];
            }
                x[2][y]=A[2][3]/A[2][2];
                for(i=1; i>=0; i--)
                {
                    sum=0;
                    for(j=i+1; j<=2; j++)
                    {
                        sum=sum+A[i][j]*x[j][y];
                    }
                    x[i][y]=(A[i][3] - sum)/A[i][i];
                }
        }
    cout<<"Matriks X"<<endl;
    for(int i=0;i<=2;i++)
    {
        for(int j=0;j<=2;j++)
        {
            cout<< x[i][j]<<"  ";
        }
    cout<<endl;
    }
    return(0);
}

Homework Equations

The Attempt at a Solution



the result from the program will show most numbers correct, except the numbers on X11 and X21

it's supposed to be
A11 = A21 = 0
but it became
A11 = -3.97364e-08
A21 = 1.19209e-07

with loop, i can find out the numbers from each matrix, and all of them are correct

only the calculations from the X matrix seems wrong
need help in making A11 and A21 become 0
 
Physics news on Phys.org
  • #2
Is this the same as the one you posted yesterday? If so, do not repost a problem just because no one has yet responded.

KWKWII said:

Homework Statement


the problem is to find the inverse of a 3x3 matrix using LU Decomposition with C++ command, with the numbers designated. in my case, my numbers for the matrix are
'306
410
780'

Code:
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <math.h>
using namespace std;

int main(void)
{
    int e, i, j, k, y;
    float A[3][4] = {{3,0,6,1},{4,1,0,2},{7,8,0,3}};
    float x[3][3], c, sum;
    float L[3][4] = {{1,0,0,0},{0,1,0,0},{0,0,1,0}};
        for(j=0; j<=2; j++)
        {
            for(i=0; i<=2; i++)
            {
                if(i>j)
                {
                    c=A[i][j]/A[j][j];
                    L[i][j] = c;
                    for(k=0; k<=2; k++)
                    {
                        A[i][k]=A[i][k]-c*A[j][k];
                    }
                }
            }
        }
            
        for(y=0; y<=2; y++)
        {
            if(y==0)
            {
                    L[0][3] = 1;
                L[1][3] = 0;
                L[2][3] = 0;
            }
            else if (y==1)
            {
                    L[0][3] = 0;
                L[1][3] = 1;
                L[2][3] = 0;
            }
            else if (y==2)
            {
                    L[0][3] = 0;
                L[1][3] = 0;
                L[2][3] = 1;
            }
            A[0][3]=L[0][3]/L[0][0];
            for(i=1; i<=2; i++)
            {
                sum=0;
                for(j=i-1; j>=0; j--)
                {
                    sum=sum+L[i][j]*A[j][3];
                }
                A[i][3]=(L[i][3]-sum)/L[i][i];
            }
                x[2][y]=A[2][3]/A[2][2];
                for(i=1; i>=0; i--)
                {
                    sum=0;
                    for(j=i+1; j<=2; j++)
                    {
                        sum=sum+A[i][j]*x[j][y];
                    }
                    x[i][y]=(A[i][3] - sum)/A[i][i];
                }
        }
    cout<<"Matriks X"<<endl;
    for(int i=0;i<=2;i++)
    {
        for(int j=0;j<=2;j++)
        {
            cout<< x[i][j]<<"  ";
        }
    cout<<endl;
    }
    return(0);
}

Homework Equations

The Attempt at a Solution



the result from the program will show most numbers correct, except the numbers on X11 and X21

it's supposed to be
A11 = A21 = 0
but it became
A11 = -3.97364e-08
A21 = 1.19209e-07

with loop, i can find out the numbers from each matrix, and all of them are correct

only the calculations from the X matrix seems wrong
need help in making A11 and A21 become 0
All of your matrix variables are type float, which can hold only about 6 or 7 decimal places. You will have a lot more precision if you make them double.
 
  • #3
Mark44 said:
do not repost a problem just because no one has yet responded

it is a repost, but i think i need to because when i re-read the guidelines, i must use proper English
which, i think, is not proper on my last post
so i tried to simplify my words on the new post (here)

and by using double for all matrix variables, it indeed became more precise, but also became worse
after changing float -> double
A11 = 7.40149e-17
A21 = -2.22045e-16
 
  • #4
KWKWII said:
it is a repost, but i think i need to because when i re-read the guidelines, i must use proper English
which, i think, is not proper on my last post
so i tried to simplify my words on the new post (here)
Your English was fine in the other post. If you need to edit a post, and you're no longer able to do it, report the post using the Report button, and a mentor will take care of it.
KWKWII said:
and by using double for all matrix variables, it indeed became more precise, but also became worse
after changing float -> double
A11 = 7.40149e-17
A21 = -2.22045e-16
Those aren't worse. They're much closer to 0 than the results when you used float variables.

Floating point arithmetic on computers is inherently imprecise due to roundoff and truncation errors.
 
  • #5
Mark44 said:
If you need to edit a post, and you're no longer able to do it, report the post using the Report button, and a mentor will take care of it

oh, i thought it wasn't fine...
i see, i'll do so next time

and also
although it is closer to 0, it is not 0
and the actual answer (done manually) is supposed to be 0
tried changing into integer, but it's even worse than using float or double
because the solution become numbers without decimals
is it also because of the rounding and truncation errors?

and i also notice that there's a minus on A21 and no minus on A11
a weird change considering the previous one is reversed
 
  • #7
Mark44 said:
using the precision function

Code:
#include <iomanip>

cout<< setprecision(3) << fixed << x[i][j]<<"  ";

(kind of different, but it works the same
i set it to only spell out 3 digits behind . )

using that, the result now becomes

A11 = -0.000
A21 = 0.000

well, although it's still weird for 0 to have (-) signs
but at least better for it shows the (seemingly) same amount as the actual answer

thanks
 
  • #8
The setprecision function is just truncating (or possibly rounding - I don't know) the fractional parts of numbers like 0.000000587 and -0.000342, and displaying only the first three decimal places.
 

Related to Matrix inverse with LU decomposition in C++

1. What is LU decomposition in C++?

LU decomposition, also known as LU factorization, is a method used in linear algebra to factorize a matrix into the product of a lower triangular matrix (L) and an upper triangular matrix (U). This technique is commonly used in numerical analysis and can make solving systems of linear equations more efficient.

2. How is LU decomposition used in finding the inverse of a matrix?

To find the inverse of a matrix using LU decomposition, the matrix is first decomposed into L and U matrices. Then, the inverse can be calculated using the formula (U^-1)(L^-1), where each of the inverse matrices can be easily found since they are triangular matrices. This method is more efficient than using traditional methods like Gaussian elimination.

3. What are the advantages of using LU decomposition in finding the inverse of a matrix?

One advantage of using LU decomposition is that it can be used to solve systems of linear equations more efficiently than other methods. Additionally, once the L and U matrices are calculated, finding the inverse of a matrix using LU decomposition is relatively straightforward and does not require complex calculations.

4. Is LU decomposition used in other programming languages besides C++?

Yes, LU decomposition is a commonly used method in linear algebra and is available in many programming languages, including C++, Python, MATLAB, and more. The specific syntax may differ between languages, but the underlying concept remains the same.

5. Are there any limitations to using LU decomposition for finding the inverse of a matrix?

One limitation of using LU decomposition is that it can only be used for square, non-singular matrices. This means that the matrix must have the same number of rows and columns, and its determinant must be non-zero. Additionally, LU decomposition may not be the most efficient method for finding the inverse of very large matrices.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
787
  • Engineering and Comp Sci Homework Help
Replies
19
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
Back
Top