MATLAB - 高斯消元法



高斯消元法(也稱為Gaussian Elimination)是一種求解線性方程組的方法。它涉及使用一系列運算將方程組的增廣矩陣轉換為行階梯形,然後進行回代以找到解。

讓我們首先了解什麼是增廣矩陣和行階梯形矩陣。

增廣矩陣是線性代數中用於求解線性方程組的矩陣。它將方程組的係數矩陣和常數矩陣組合成一個矩陣。增廣矩陣通常透過將常數列(方程的右邊)附加到係數列來編寫。

例如,考慮線性方程組:

$$\mathrm{2x \: + \: 3y \: = \: 5}$$

$$\mathrm{4x \: + \: 6y \: = \: 10}$$

係數矩陣 (A) 和常數矩陣 (b) 為:

$$\mathrm{A \: = \: \begin{pmatrix} 2 & 3 \\ 4 & 6 \end{pmatrix} \: , \: b \: = \: \left(\begin{array}{c}5\\ 10\end{array}\right)}$$

增廣矩陣 (A|b) 是透過將 b 附加到 A 形成的:

$$\mathrm{\begin{pmatrix} 2 & 3 & | & 5 \\ 4 & 6 & | & 10\end{pmatrix}}$$

**行階梯形 (REF)** 是線性代數中使用的矩陣的一種特定形式,用於簡化求解線性方程組的過程。如果矩陣滿足以下條件,則該矩陣為行階梯形:

  • 所有非零行都在所有零行的上方。
  • 每個非零行的首個非零元素(也稱為主元)為 1,並且位於其上方行的首個非零元素的右邊。
  • 首個非零元素在其列中是唯一的非零元素。

這是一個 3×4 行階梯形矩陣的示例:

$$\mathrm{\begin{pmatrix} 1 & 2 & 3 & 4 \\ 0 & 1 & 5 & 6 \\ 0 & 0 & 1 & 7\end{pmatrix}}$$

在這個例子中:

  • 第一個首個非零元素是 1,它出現在第一列。
  • 第二個首個非零元素也是 1,它出現在第一個首個非零元素的右邊,在第二行和第二列。
  • 第三個首個非零元素是 1,它出現在第二個首個非零元素的右邊,在第三行和第三列。
  • 所有首個非零元素下方的元素都是零。
  • 行的順序排列,使得任何零行(如果存在)都在底部(在這個特定示例中沒有零行)。

高斯消元法的步驟

考慮線性方程組:

$$\mathrm{\begin{cases} 2x \: + \: 3y \: = \: 8\\ 4x \: + \: 9y \: = \: 20\end{cases}}$$

我們可以將其寫成增廣矩陣形式:

$$\mathrm{\begin{bmatrix} 2 & 3 & | & 8 \\ 4 & 9 & | & 20\end{bmatrix}}$$

基於上述等式,高斯消元法的步驟如下:

1. 形成增廣矩陣:

$$\mathrm{\begin{bmatrix} 2 & 3 & | & 8 \\ 4 & 9 & | & 20\end{bmatrix}}$$

2. 將第一行的首個非零元素化為 1(如果它還不是 1):

將第一行除以 2:

$$\mathrm{\begin{bmatrix} 1 & \frac{3}{2} & | & 4 \\ 4 & 9 & | & 20\end{bmatrix}}$$

消去第二行中的 x 項:

從第二行中減去第一行的 4 倍:

$$\mathrm{R2 \: = \: R2 \: - \: 4 \: \times \: R1}$$

$$\mathrm{\begin{bmatrix} 1 & \frac{3}{2} & | & 4 \\ 0 & 3 & | & 4\end{bmatrix}}$$

將第二行的首個非零元素化為 1(如果它還不是 1):

將第二行除以 3:

$$\mathrm{\begin{bmatrix} 1 & \frac{3}{2} & | & 4 \\ 0 & 1 & | & \frac{4}{3}\end{bmatrix}}$$

消去第一行中的 y 項:

從第一行中減去第二行的 3/2 倍。

$$\mathrm{R1 \: = \: R1 \: - \: \frac{3}{2} \: \times \: R2}$$

$$\mathrm{\begin{bmatrix} 1 & 0 & | & \frac{10}{3} \\ 0 & 1 & | & \frac{4}{3}\end{bmatrix}}$$

寫出解

從最終的增廣矩陣中,我們可以直接看到解。

$$\mathrm{x \: = \: \frac{10}{3} \: , \: y \: = \: \frac{4}{3}}$$

因此,方程組的解為。

$$\mathrm{x \: = \: \frac{10}{3} \: , \: y \: = \: \frac{4}{3}}$$

現在我們知道了高斯消元法的步驟,讓我們嘗試在matlab中做同樣的事情。

MATLAB 中的高斯消元法

我們將使用的方程是。

$$\mathrm{\begin{cases} 2x \: + \: 3y \: = \: 8\\ 4x \: + \: 9y \: = \: 20\end{cases}}$$

**步驟 1** - 定義係數矩陣 A 和右端向量 b

A = [2 3; 4 9];
b = [8; 20];

**步驟 2** - 用向量 b 增廣矩陣 A。

Ab = [A b];

**步驟 3** - 執行行運算以將 A 轉換為上三角矩陣。

MATLAB 有一個名為 rref(簡化行階梯形)的函式,它簡化了此過程。但是,如果您想手動執行這些步驟,您可以執行以下操作:

% Forward elimination
n = size(Ab, 1);
for i = 1:n-1
    for j = i+1:n
        factor = Ab(j,i) / Ab(i,i);
        Ab(j,:) = Ab(j,:) - factor * Ab(i,:);
    end
end

**步驟 4** - 向後代入以求解變數。

% Initialize the solution vector
x = zeros(n,1);

% Backward substitution
x(n) = Ab(n,end) / Ab(n,n);
for i = n-1:-1:1
    x(i) = (Ab(i,end) - Ab(i,i+1:n) * x(i+1:n)) / Ab(i,i);
end

**步驟 5** - 顯示結果

disp('The solution is:');
disp(x);

完整的matlab程式碼如下。

% Define the coefficient matrix A and the right-hand side vector b
A = [2 3; 4 9];
b = [8; 20];

% Augment the matrix A with the vector b
Ab = [A b];

% Forward elimination
n = size(Ab, 1);
for i = 1:n-1
    for j = i+1:n
        factor = Ab(j,i) / Ab(i,i);
        Ab(j,:) = Ab(j,:) - factor * Ab(i,:);
    end
end

% Initialize the solution vector
x = zeros(n,1);

% Backward substitution
x(n) = Ab(n,end) / Ab(n,n);
for i = n-1:-1:1
    x(i) = (Ab(i,end) - Ab(i,i+1:n) * x(i+1:n)) / Ab(i,i);
end

% Display the result
disp('The solution is:');
disp(x);

當代碼在matlab命令視窗中執行時,我們得到的輸出是

The solution is:
    2.0000
    1.3333

使用 rref(簡化行階梯形)

您可以使用MATLAB內建的rref()函式來達到與下面所示相同的結果。

% Define the coefficient matrix A and the right-hand side vector b
A = [2 3; 4 9];
b = [8; 20];

% Augment the matrix A with the vector b
Ab = [A b];

% Use rref to get the Reduced Row Echelon Form
R = rref(Ab);

% Extract the solutions
x = R(:,end);

% Display the result
disp('The solution is:');
disp(x);

執行後的輸出為:

The solution is:
    2.0000
    1.3333
廣告