cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Gauss-Jordan elimination method for inverse matrix

SOLVED
Highlighted
Bedrock

Gauss-Jordan elimination method for inverse matrix

I try in Mathcad to build  Gauss-Jordan method for obtaining the inverse matrix but it looks quite difficult.

It is closed to Jordan elimination method, but on the right side we consider initially (in the augmented matrix) an unit matrix. Did anyone build this method in Mathcad?

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Gauss-Jordan elimination method for inverse matrix

Cool ! I see that it makes also Pivotation. The problem is solved !  I attached the file.

32 REPLIES 32

Re: Gauss-Jordan elimination method for inverse matrix

You may look up algorithms in "Numerical Recipes", I guess.

Writing an all purpose matrix-inverse-function and implementing it in Mathcad would require the knowledge of some "tricks" to make the algorithm as stable as possible, finding the appropriate pivot element, etc.

I am sure you can find a lot of references about this algorithm after a short internet research.

 

But what would be the benefit compared with Mathcads built-in algorithms, which usually are well tested, quite stable and much faster than anything implemented in Mathcads programming language?

 

You may be interested in this thread

https://community.ptc.com/t5/PTC-Mathcad/Gauss-Jordan-rref/td-p/273580

 

Re: Gauss-Jordan elimination method for inverse matrix

MAthcad fucntions are Black boxes. I want to apply just the numerical method Gauss-Jordan for the inverse of  a matrix, a method that is open, transparent, and verifiable.

Re: Gauss-Jordan elimination method for inverse matrix


@tubar wrote:

MAthcad fucntions are Black boxes. I want to apply just the numerical method Gauss-Jordan for the inverse of  a matrix, a method that is open, transparent, and verifiable.


So for the start you sure would need some good advice about how to implement a stable algorithm.
Many algorithm implemented natively in Mathcad were based on the ones found in the above mentioned "Numerical Recipes" -> http://www.numerical.recipes

In Prime some were exchanged for the KNITRO tool box (with no advantage as far as I can tell).

Next step would be to implement the algorithm in C or the like and turn it into a user DLL which can be used from within Mathcad.

Then you may have functions which are fully verifyable and transparent which hopefully are as fast and reliable as Mathcad's built-in at the high price of a lot of work.

 

Re: Gauss-Jordan elimination method for inverse matrix

 

OK, are interesting the desires of PTC about Mathcad, but I remain faithful to the classical numerical methods implemented as they by the user itself.

 

The representation of the Mathcad's logical algorithm is very closed to the General Flowchart (  the logical scheme) of the algorithm of solving the problem, that's why I like it to use this way instead of Built-in functions.

 

I have the C++ and Matlab codes for "Gauss-Jordan elimination method for inverse matrix" and I want also to obtain a representation of it in Mathcad:

 

// Gauss-Jordan elimination for finding the inverse matrix
  
#include <iostream>
#include <vector>
using namespace std;
  
// Function to Print matrix.
void PrintMatrix(float ar[][20], int n, int m)
{
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cout << ar[i][j] << "  ";
        }
        printf("\n");
    }
    return;
}
  
// Function to Print inverse matrix
void PrintInverse(float ar[][20], int n, int m)
{
    for (int i = 0; i < n; i++) {
        for (int j = n; j < m; j++) {
            printf("%.3f  ", ar[i][j]);
        }
        printf("\n");
    }
    return;
}
  
// Function to perform the inverse operation on the matrix.
void InverseOfMatrix(float matrix[][20], int order)
{
    // Matrix Declaration.
  
    float temp;
  
    // PrintMatrix function to print the element
    // of the matrix.
    printf("=== Matrix ===\n");
    PrintMatrix(matrix, order, order);
  
    // Create the augmented matrix
    // Add the identity matrix
    // of order at the end of orignal matrix.
    for (int i = 0; i < order; i++) {
  
        for (int j = 0; j < 2 * order; j++) {
  
            // Add '1' at the diagonal places of
            // the matrix to create a identity matirx
            if (j == (i + order))
                matrix[i][j] = 1;
        }
    }
// Interchange the row of matrix,
    // interchanging of row will start from the last row
    for (int i = order - 1; i > 0; i--) {
  
        if (matrix[i - 1][0] < matrix[i][0])
            for (int j = 0; j < 2 * order; j++) {
  
                // Swapping of the row, if above
                // condition satisfied.
                temp = matrix[i][j];
                matrix[i][j] = matrix[i - 1][j];
                matrix[i - 1][j] = temp;
            }
    }
  
    // Print matrix after interchange operations.
    printf("\n=== Augmented Matrix ===\n");
    PrintMatrix(matrix, order, order * 2);
  
    // Replace a row by sum of itself and a
    // constant multiple of another row of the matrix
    for (int i = 0; i < order; i++) {
  
        for (int j = 0; j < 2 * order; j++) {
  
            if (j != i) {
  
                temp = matrix[j][i] / matrix[i][i];
                for (int k = 0; k < 2 * order; k++) {
  
                    matrix[j][k] -= matrix[i][k] * temp;
                }
            }
        }
    }
  
    // Multiply each row by a nonzero integer.
    // Divide row element by the diagonal element
    for (int i = 0; i < order; i++) {
  
        temp = matrix[i][i];
        for (int j = 0; j < 2 * order; j++) {
  
            matrix[i][j] = matrix[i][j] / temp;
        }
    }
  
    // print the resultant Inverse matrix.
    printf("\n=== Inverse Matrix ===\n");
    PrintInverse(matrix, order, 2 * order);
  
    return;
}
// Driver code
int main()
{
    int order;
  
    // Order of the matrix
    // The matrix must be a square a matrix
    order = 3;
    float matrix[20][20] = { { 5, 7, 9 },
                             { 4, 3, 8 },
                             { 7, 5, 6 },
                             { 0 } };
  
    // Get the inverse of matrix
    InverseOfMatrix(matrix, order);
  
    return 0;
}

 

Re: Gauss-Jordan elimination method for inverse matrix

If you use real Mathcad (not Prime but Mathcad 15) then you find information on how to write Mathcad DLLs if you go to "Help" and chose "Developer's Reference" .

Since I have little interest in Prime I am not sure where you could find the appropriate information for this version.

As far as I recall there are some threads in this forum dealing with DLLs for Prime.

Re: Gauss-Jordan elimination method for inverse matrix

I dont care DLLs.  I just want to build a Mathcad algorithm for Gauss-Jordan elimination method to obtain the inverse matrix.

Re: Gauss-Jordan elimination method for inverse matrix


@tubar wrote:

I dont care DLLs.  I just want to build a Mathcad algorithm for Gauss-Jordan elimination method to obtain the inverse matrix.


Oh, I see. So you don't trust Mathcad's built-in algorithms but you trust Mathcad's built-in programming language, which is a black box, too ;-)

Implementing such a basic algorithm in a slow, interpreted language, as the Mathcad programming language at worksheet level is, is IMHO maybe a good exercise for students to do but nothing you would use for real, I guess.

 

Anyway - just go ahead and give it a try. If you get stuck you may come back and post your worksheet here to get help.
Maybe the link to the forum thread I posted above can be helpful (even though the matrix is only brought to echelon form there.

Re: Gauss-Jordan elimination method for inverse matrix

 

I want to build the Algorithm in the Mathcad interface. Already, in this Forum are presented some Jordan elimination algorithms (applied to obtain the solution) but this time I want to find the inverse matrix.

 

Unlike the other methods, the Gauss-Jordan elimination bring you to the exact/analytic solution (as you can see in the attached image file from the first message).

Re: Gauss-Jordan elimination method for inverse matrix


@tubar wrote:

 

I want to build the Algorithm in the Mathcad interface. Already, in this Forum are presented some Jordan elimination algorithms (applied to obtain the solution) but this time I want to find the inverse matrix.

 

Unlike the other methods, the Gauss-Jordan elimination bring you to the exact/analytic solution (as you can see in the attached image file from the first message).


Not sure what you mean by "exact/analytic".

Does Mathcads symbolics not qualify for "exact" and "analytic" in your eyes?
B.png

And the C-program you posted is only providing a numeric solution, not a symbolic, "exact" one. So whats the benefit?

 

But of course you are free to give it a try and come back for help with programming in Mathcad, if needed.