Skip to main content
LESSON

Gaussian elimination

For a system of linear equations, such as: Now we want to solve this system of linear equations, what we can do is multiply one of the equations by a number, subtract the other equation, eliminate one variable, repeat the above steps and eliminate the other variable. This method of solving a system of equations has been learned in junior high school. It is called "elimination". For example, for the above system of equations, (1) + (2), eliminate the variables and get the new equation . Then multiply the new equation by 5, and then subtract equation (3) to get the equation containing only x_3. Solved. Go back and find the solution. this

For a system of linear equations, for example:
eft egin{align} &x_1-2x_2+x_3&=0ag{1} &2x_2-8x_3&=8ag{2} &5x_1uaduad-5x_3&=10ag{3} nd{align} ight.
Now we want to solve this linear system of equations. What we can do is multiply one of the equations by a number, subtract from the other equation, cancel one variable, repeat the above steps and cancel the other variable. This method of solving a system of equations has been learned in junior high school. It is called "elimination".
For example, for the above system of equations, (1) + (2), eliminate the variablesx_2, get the new equationx_1-7x_3=8. Then multiply the new equation by 5, and then subtract the formula (3) to get the formula containing only x_3-30x_3=30. Solvedx_3=-1. Back againx_3Solvedx_1=1,x_2=0
This process is very simple, but think about what we did during the elimination process:

  1. Row doubling transformation (multiply a row by a number and add it to another row)
  2. Multiply transformation (multiply a row by a non-zero number)
  3. Row exchange (swapping the positions of two rows): In our opinion, we do not use this transformation, but you can indeed exchange the positions of any two expressions during the elimination process, which is extremely important in matrix operations.

We call the above three transformationselementary row transformation, it can be seen that the elementary row transformation does not affect the solution set of the system of equations.


Next, we extract the coefficients of the system of equations and put them in the first box according to their original positions, and put the constants on the right side of the system of equations in the second box.
egin{array}{cc} nderbrace{ egin{bmatrix} 1&-2&1 0&2&-8 5&0&-5 nd{bmatrix}}{系数矩阵}& nderbrace{ egin{bmatrix} 1&-2&1&0 0&2&-8&8 5&0&-5&10 nd{bmatrix}}{增广矩阵} nd{array}
We call this list of numbers plus a box a matrix. For solving systems of linear equations we prefer the augmented matrix form. Consider if we treat each row of the matrix as an equation, and we perform elementary row transformations on these rows. Depending on the number and method of elementary row transformations we do, we will get many new matrices, and these matrices must have the same solution set as the original matrix.
Therefore, we say that if one matrix becomes another matrix after several elementary row transformations, we say that the two matrices are row equivalent. Row-equivalent matrices have exactly the same set of solutions.


In this way, elimination of the system of equations is converted into elimination of the matrix. In dealing with extremely large systems of equations and solution existence and uniqueness issues, the matrix elimination method is far more convenient than the traditional solution of systems of equations.

Systematic solution to systems of equations—Gaussian elimination method

eft egin{align} 3x_2-6x_3+6x_4+4x_5&=-5 3x_1-7x_2+8x_3-5x_4+8x_5&=9 3x_1-9x_2+12x_3-9x_4+6x_5&=15 nd{align} ight.o
nderbrace{ egin{bmatrix} 0&3&-6&6&4&-5 3&-7&8&-5&8&9 3&-9&12&-9&6&15 nd{bmatrix}}_{增广矩阵}mplies
Convert the augmented matrix into the form of a ladder matrix through elementary row transformation:
nderbrace{egin{bmatrix} box{3}&-9&12&-9&6&15 0&box{2}&-4&4&2&-6 0&0&0&0&box{1}&4 nd{bmatrix}}_{阶梯型矩阵(REF)}mplies

box{3},box{2},box{1}is the leading element of each row of the ladder matrix, which we callmain element, the column where the pivot is located is calledmain column, the column with no pivot is calledfree column. The variable corresponding to the main column is calledbasic variables, the variable corresponding to the free column is calledFree variables.
Next, we transform theThe element at the pivot position is set to 1, and other elements in the main column except the pivot are set to 0.. The resulting matrix is ​​called a reduced row echelon matrix.
nderbrace{egin{bmatrix} box{1}&0&-2&3&0&-24 0&box{1}&-2&2&0&-7 0&0&0&0&box{1}&4 nd{bmatrix}}_{简化阶梯型矩阵(RREF)}
With the simplified row echelon matrix we know:
eft egin{align} x_1-2x_3+3x_4&=-24 x2-2x_3+2x_4&=-7 x_5&=4 nd{align}ight.
Solution:
eft egin{align} &x_1=2x_3-3x_4-24 &x2=2x_3-2x_4-7 &x_3是自由变量 &x_4是自由变量 &x_5=4 nd{align}ight.
This solution is called an explicit expression of the solution. As long as the values of the two free variables are determined, the solution to the system of equations is determined. Because there are free variables, this equation has countless solutions.


Matrix equations and vector equations

For a system of equations:
eft egin{align} &x_1-2x_2+x_3&=0 &2x_2-8x_3&=8 &5x_1uaduad-5x_3&=10 nd{align} ight.
We have two other expressions:
matrix equation
nderbrace{ egin{align} nderbrace{ egin{bmatrix} 1&-2&1 0&2&-8 5&0&-5 nd{bmatrix}}{系数矩阵} & egin{bmatrix} x_1 x_2 x_3 nd{bmatrix}=egin{bmatrix} 0 8 10 nd{bmatrix} nd{align}}{AX=b}
Vector equation:
x_1egin{bmatrix} 105nd{bmatrix}+x_2egin{bmatrix} -220nd{bmatrix}+x_3egin{bmatrix} 1-8-5nd{bmatrix}=egin{bmatrix}0810nd{bmatrix}
It can be seen that the product of a matrix and a vector is a linear combination of the column vectors in A weighted by the elements in X.
Vector operations

  • Addition:egin{bmatrix}abcnd{bmatrix}+egin{bmatrix}defnd{bmatrix}=egin{bmatrix}a+db+ec+fnd{bmatrix}
  • Multiply numbers:kegin{bmatrix}abcnd{bmatrix}=egin{bmatrix}kakbkcnd{bmatrix}(k是常数)
    Linear properties of matrices
    A(u+v)=A(u)+A(v)
    A(cu)=cA(u)

Implicit expression of solution: parameter vector form

For the explicit solution of the equation:
eft egin{align} &x_1=2x_3-3x_4-24 &x2=2x_3-2x_4-7 &x_3是自由变量 &x_4是自由变量 &x_5=4 nd{align}ight.
We can write it as free variablex_3,x_4is a linear combination of weights
x=egin{bmatrix} x_1x_2x_3x_4x_5nd{bmatrix}=x_3egin{bmatrix} 22100nd{bmatrix}+x_4egin{bmatrix} -3-2010nd{bmatrix}+egin{bmatrix} -24-7004nd{bmatrix}

Example questions

poj1222

//Gaussian elimination
#include<iostream>
#include<cstring>
using namespace std; int a[31][31];//Represents 30 equation systems
int d[5][2] = {{0, 0}, {0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int res[5][6];//result void back () { for (int i = 29; i >= 0; i--) { res[i / 6][i % 6] = a[i][30]; }
} void gauss () { for (int i = 0; i < 30; i++) { //row swap int k = i; for (; k < 30; k++) { if (a[k][i]) break; } for (int j = 0; j <= 30; j++) {//att1: There is a unique solution according to the meaning of the question, so k will not >= 30 here. No need to judge anymore. swap(a[i][j], a[k][j]); } //Elimination--convert into unit matrix for (int j = 0; j < 30; j++) {//att1: starts from 0 if (i == j) continue;//att2: Don’t miss it if (a[j][i]) { for (int k = i; k <= 30; k++) { a[j][k] ^= a[i][k]; } } } } back();
} int main()
{ int t, cnt = 0; cin >> t; while (t--) { memset(a, 0, sizeof(a)); for (int i = 0; i < 30; i++) { cin >> a[i][30]; } for (int i = 0; i < 5; i++) { for (int j = 0; j < 6; j++) { int ti = i * 6 + j; for (int k = 0; k < 5; k++) { int tx = i + d[k][0]; int ty = j + d[k][1]; if (tx < 0 || tx > 4 || ty < 0 || ty > 5) continue; a[ti][tx * 6 + ty] = 1; } } } gauss(); cout << "PUZZLE #" << ++cnt << endl; for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { cout << res[i][j] << ' '; } cout << res[i][5] << endl; } } return 0;
}