Home Robotics C++ Physics II AP Physics B Electronics AP Java Astronomy Independent Study Summer Session Contests  About
                                                       

Linear Algebraic Equations

 

 

 

 

 

 

 

 

 

 

Summary

We will cover only Gauss Jordan and summarize the remainder

AX = b and Gauss Jordan Elimination

The data files      //note that you have to type them

Example 1

Notes
The main function is xgaussj and it uses gaussj along with the file matrx1.dat

Be sure to save the .dat file as a text file and check to see that it has a .dat extension

The name for the main program does not matter but it must be a source file

The name for the implementation file (also a source file) must be xgaussj

 

Input File (matrx1.dat - a text file)

 

MATRICES FOR INPUT TO TEST ROUTINES

Size of matrix (NxN), Number of solutions:

3 2

Matrix A:

1.0 0.0 0.0

0.0 2.0 0.0

0.0 0.0 3.0

Solution vectors:

1.0 0.0 0.0

1.0 1.0 1.0

Code for reading the data files  (in examples)

int j,k,l,m,n;
string dummy;             //string to store things
ifstream fp("matrx1.dat");     //open file and use fp alias

if (fp.fail())
    NR::nrerror("Data file matrx1.dat not found");     //in case file is not found
cout << fixed << setprecision(6);     //for formatting
getline(fp,dummy);     //reads the first line ("MATRICES FOR ...)
while (!fp.eof())

{
getline(fp,dummy);   //reads the second line ("Size of matrix...)
fp >> n >> m;               //reads the number of rows (n) and columns (m)
fp.get();
getline(fp,dummy);
Mat_DP a(n,n),u(n,n),b(n,m),t(n,m);
for (k=0;k<n;k++)
for (l=0;l<n;l++) fp >> a[k][l];
fp.get();
getline(fp,dummy);
for (l=0;l<m;l++)
for (k=0;k<n;k++) fp >> b[k][l];
fp.get();
getline(fp,dummy);
// save matrices for later testing of results
Mat_DP ai=a;
Mat_DP x=b;

 

Output

Notes

  The inverse is a matrix (listed as inverse of matrix a below) that, when multiplied by the original,

   results in the identity matrix (listed as a times a-inverse beow).

  The identity matrix is a matrix with 1s along the diagonal and 0s elsewhere.

  The code then checks to ensure that the original matrix multiplied by the solution vector

      (Ax) equals the original right hand side vector (b). Below this is listed as matrix*sol'n and is

      shown to be equal to the original vectors (listed as 0 and 1)

 

 

Example 2

 

Data File (matrix1.dat - a text file)

 

NEXT PROBLEM

Size of matrix (NxN), Number of solutions:

3 2

Matrix A:

1.0 2.0 3.0

2.0 2.0 3.0

3.0 3.0 3.0

Solution vectors:

1.0 1.0 1.0

1.0 2.0 3.0

 

Output

 

 

 

LU Decomposition Example 1

 

The main function is xludcmp.cpp - it uses ludcmp

 

MATRICES FOR INPUT TO TEST ROUTINES

Size of  matrix (NXN), Number of solutions;

3 2

Matrix A:

1.0 0.0 0.0

0.0 2.0 0.0

0.0 0.0 3.0

Solution vectors:

1.0 0.0 0.0

1.0 1.0 1.0

 

Note: You can either place the input file in the same project or modify the code as follows

            ifstream fp("c:\\matrx1.dat");        //location of file, note the \, first is escape character

 

Output

 

 

LU Decomposition Example 2

 

Program uses ludcmp.cpp, lubksb.cpp, and the driver xlubksb.cpp

 

Input file is matrix1.dat as above