Implementation Code of Gaussian elimination method for Inverse Matrix in MATLAB

Source: Internet
Author: User

Function qiuni = inv_get ()
N = length ();
M = eye (N );
% To get the upper triangle moment?
For I = 1: N
Max = a (I, I );
A = I;
For J = I + 1: N
If (ABS (A (J, I)> ABS (max) % find the maximum value
Max = a (J, I );
A = J;
End
End
For M = 1: N
Temp1 = a (I, m); % switch the row where the maximum value is located and the current row
A (I, m) = a (A, M );
A (a, m) = temp1;
Temp2 = m (I, m );
M (I, m) = M (A, M );
M (a, m) = temp2;
End
For k = I + 1: N
Jiaquan = a (K, I)/A (I, I); % the upper triangle is obtained by weighting other rows.
For n = 1: N
A (k, n) = a (k, n)-jiaquan * a (I, N );
M (k, n) = M (k, n)-jiaquan * m (I, n );
End
End

End
Temp3 =;
% To obtain the Unit Matrix
For S = N:-1:1
Xishu1 = a (S, S );
For P = s: N
A (P, S) = A (P, S)/xishu1;
End
For q = 1: N
M (S, q) = M (S, q)/xishu1;
End
For W = S-1:-1:1
Xishu = a (W, S );
A (W, S) = 0;
For t = 1: N
M (W, t) = m (W, t)-xishu * m (S, T );
End
End
End
Temp2 =;
Qiuni = m;

C language programs and comments

# Include <stdio. h>

# Include <math. h>

Long int const n = 1000; // The maximum number of defined matrices is 1000.

Int N; // n indicates the number of rows and columns in the matrix.

Double juzhen [N] [N]; // defines a 1000-level matrix.

Double Danwei [N] [N]; // defines a matrix of units.

Bool zhaozuidazhi (INT s) // defines a Boolean function that selects the largest element from row s to row N as the principal component.

{

Int I, j,;

Double mas, temp; // temp is an intermediate variable for row exchange.

Mas = FABS (juzhen [s] [s]); // mathematical function: FABS function: Calculate the absolute value of floating point juzhen [s] [s]

// Calculate juzhen [s] [s]. When juzhen [s] [s] is not negative, juzhen [s] [s] is returned. otherwise, return-juzhen [s] [s].

A = s;

For (I = S + 1; I <n; I ++)

{

If (MAS <FABS (juzhen [I] [s])

{

Mas = FABS (juzhen [I] [s]);

A = I;

}

}

If (MAS = 0)

Return false;

// Exchange two rows

For (j = 0; j <n; j ++)

{

Temp = juzhen [s] [J];

Juzhen [s] [J] = juzhen [a] [J];

Juzhen [a] [J] = temp;

Temp = Danwei [s] [J];

Danwei [s] [J] = Danwei [a] [J];

Danwei [a] [J] = temp;

}

Return true;

}

Void jisuan (INT s) // decode Calculation

{

Int I, J;

Double mas = juzhen [s] [s], R;

For (I = S + 1; I <n; I ++)

{

R = juzhen [I] [s]/MAS;

For (j = 0; j <n; j ++)

{// Use the elimination element to calculate the square matrix to make it an upper triangle matrix. The final result is to multiply the elements on the primary diagonal.

Juzhen [I] [J] = juzhen [I] [J]-juzhen [s] [J] * R;

Danwei [I] [J] = Danwei [I] [J]-Danwei [s] [J] * R;

}

}

}

Void HH (int s)

{

Int I, J;

Double MAS;

Mas = juzhen [s] [s];

For (I = s; I <n; I ++)

Juzhen [s] [I] = juzhen [s] [I]/MAS;

For (I = 0; I <n; I ++)

Danwei [s] [I] = Danwei [s] [I]/MAS;

For (I = s-1; I> = 0; I --)

{

Mas = juzhen [I] [s];

Juzhen [I] [s] = 0;

For (j = 0; j <n; j ++)

Danwei [I] [J] = Danwei [I] [J]-Mas * Danwei [s] [J];

}

}

Int main ()

{

Int I, J;

ZL: printf ("level of the first input matrix, and then press enter to enter the original matrix: \ n ");

Scanf ("% d", & N );

For (I = 0; I <n; I ++)

{

For (j = 0; j <n; j ++)

{

If (I = J) Danwei [I] [J] = 1;

Else Danwei [I] [J] = 0;

Scanf ("% lf", & juzhen [I] [J]); // function parameters in C language are passed values rather than references,

// Generally, parameters cannot be modified and the reason why scanf can modify parameters is transmitted to scanf.

// That parameter is a pointer. scanf modifies the content pointed to by the pointer. So,

// Get the X address and pass it back. & Obtains the address of juzhen [I] [J]. % lf indicates that juzhen [I] [J] is a double precision floating point type.

}

}

// Convert the original matrix into an upper Triangle Matrix

For (I = 0; I <n-1; I ++)

{

// Select the maximum absolute value of each time as the [I], that is, the principal element.

If (zhaozuidazhi (I) // function call

{

Jisuan (I); // function call

 

}

Else

{

Printf ("This matrix is irreversible"); // The Gaussian elimination method cannot convert the original matrix into a triangle format,

// It indicates that the original matrix is an irreversible matrix,

Return 0;

}

}

// Convert the original matrix into a matrix of units

For (I = n-1; I> = 0; I --)

{

HH (I );

}

Printf ("\ n original matrix's inverse matrix: \ n"); // output the inverse matrix statement

For (I = 0; I <n; I ++)

{

For (j = 0; j <n; j ++)

Printf ("%. 2lf", Danwei [I] [J]); // %. 2lf represents in printf () in the following format

// Output a long double number: the integer part outputs all the decimals and outputs two digits,

// If there is no decimal number, the output is two zeros. If there are less than two digits, the following zeros are added. If the number is greater than two digits, the result is truncated to two digits.

// Two decimal places are displayed in the test result.

Printf ("\ n ");

}

Goto ZL;

}

Implementation Code of Gaussian elimination method for Inverse Matrix in MATLAB

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.