MATLAB - 行簡化階梯形矩陣



行簡化階梯形矩陣 (RREF) 是線性代數中使用的矩陣的一種特定形式,用於求解線性方程組、進行秩計算以及分析線性變換。矩陣的 RREF 是唯一的,並且可以透過一系列初等行運算獲得:行交換、行縮放以及將一行乘以一個數加到另一行。

RREF 的定義

如果矩陣滿足以下條件,則該矩陣處於行簡化階梯形。

  • 主元為 1 - 每一非零行的首個非零元素為 1。
  • 主元上方和下方為 0 - 每個主元 1 是其所在列中唯一的非零元素。
  • 行排序 - 任何一行的主元都位於其上一行主元的右邊。
  • 零行 - 任何僅包含零的列都位於矩陣的底部。

讓我們考慮一個簡單的例子

假設我們有一個矩陣 A:

$$\mathrm{A \: = \: \begin{bmatrix} 1 & 2 & 1 \\ 2 & 4 & -3 \\ 3 & 6 & 4 \end{bmatrix}}$$

要找到它的 RREF,請執行以下步驟。

步驟 1 - 將第一行轉換為具有主元 1(如有必要,除以主係數)。

R1 = [1 2 1].

步驟 2 - 透過從後續行中減去第一行的倍數來消除第一列中主元 1 下方的元素。

$$\mathrm{R2\:=\:\begin{bmatrix}2&4&-3\end{bmatrix}\:-2\:\times\:\begin{bmatrix}1&2&1\end{bmatrix}\:=\:\begin{bmatrix}0&0&-5\end{bmatrix}}$$

$$\mathrm{R3\:=\:\begin{bmatrix}3&6&4\end{bmatrix}\:-3\:\times\:\begin{bmatrix}1&2&1\end{bmatrix}\:=\:\begin{bmatrix}0&0&1\end{bmatrix}} $$

步驟 3 - 將新行轉換為具有主元 1。

R3 = [ 0 0 1 ]

步驟 4 - 透過減去第三行的適當倍數來消除第三列中主元 1 上方的元素。

$$\mathrm{R1\:=\:\begin{bmatrix}1&2&1\end{bmatrix}\:-1\:\times\:\begin{bmatrix}0&0&1\end{bmatrix}\:=\:\begin{bmatrix}1&2&0\end{bmatrix}}$$

$$\mathrm{R2\:=\:\begin{bmatrix}0&0&-5\end{bmatrix}\:-(-5)\:\times\:\begin{bmatrix}0&0&1\end{bmatrix}\:=\:\begin{bmatrix}0&0&0\end{bmatrix}}$$

得到的 RREF 矩陣為:

$$\mathrm{R \: = \: \begin{bmatrix} 1 & 2 & 0 \\ 0 & 0 & 0 \\ 0 & 0 & 1 \end{bmatrix}}$$

使用 Matlab 計算行簡化階梯形矩陣

在 MATLAB 中,可以使用內建函式 rref() 來計算矩陣的行簡化階梯形矩陣 (RREF)。RREF 是經過行約簡達到簡化形式的矩陣,這使得求解線性方程組更加容易。

語法

R = rref(A)
R = rref(A,tol)
[R,p] = rref(A)

語法解釋

R = rref(A) 使用高斯-約旦消元法給出矩陣 A 的簡化版本,該方法包括一個步驟,透過在需要時交換行來確保計算更穩定。

R = rref(A, tol) 允許您設定容差級別 (tol),這有助於演算法確定哪些列太小而不足以被認為重要。

行簡化階梯形矩陣 Matlab 示例

以下是一些演示如何有效使用此函式的示例:

示例 1

考慮以下線性方程組。

2x+4y = 10;
4x+9y = 19;

對於上述方程,我們

定義增廣矩陣

A = [2 4 10; 4 9 19];

此矩陣表示方程組,其中前兩列表示 x 和 y 的係數,最後一列表示方程右側的常數。

使用 rref() 簡化矩陣

R = rref(A);

rref() 函式將高斯-約旦消元法應用於矩陣 A。它透過使對角元素為 1 並使這些列中的所有其他元素為 0 來簡化矩陣。

如果需要,演算法將交換行以確保數值穩定性。這確保了在處理小數或大數時,計算不會出現舍入誤差或不穩定性。

disp('The Reduced Row Echelon Form is:');
disp(R);

完整的程式碼如下所示:

% Define the augmented matrix
A = [2 4 10; 4 9 19];

% Compute the Reduced Row Echelon Form
R = rref(A);

% Display the result
disp('The Reduced Row Echelon Form is:');
disp(R);

執行後,得到的輸出如下:

>> % Define the augmented matrix
A = [2 4 10; 4 9 19];

% Compute the Reduced Row Echelon Form
R = rref(A);

% Display the result
disp('The Reduced Row Echelon Form is:');
disp(R);

The Reduced Row Echelon Form is:
     1     0     7
     0     1    -1

>> 

該矩陣現在處於簡化形式,稱為行簡化階梯形矩陣 (RREF)。

線性方程的解為 x=7 和 y=-1

示例 2

考慮以下矩陣。

$$\mathrm{A \: = \: \begin{bmatrix} 1 & 2 & 3 \\ 2 & 4 & 6.0001 \\ 3 & 6 & 9 \end{bmatrix}}$$

此矩陣有一行幾乎是其他行的線性組合,除了一個很小的差異(6 對 6.0001)。使用容差引數將允許我們決定是否應該忽略或考慮該小差異。

定義矩陣

A = [1 2 3; 2 4 6.0001; 3 6 9];

此矩陣幾乎是秩虧的,因為第二行和第三行幾乎是第一行的線性組合。

使用具有指定容差的 rref()

tol = 0.01; % Set a higher tolerance
R_high_tol = rref(A, tol);

在這裡,我們使用 0.01 的容差,這意味著任何小於 0.01 的值都將被視為零。在這種情況下,6 和 6.0001 之間的微小差異將被忽略。

tol = 1e-6; % Set a lower tolerance
R_low_tol = rref(A, tol);

使用更小的容差 (1e-6),演算法將認為即使是微小的差異也很重要,因此 6 和 6.0001 之間的微小差異將被考慮在內。

最終程式碼如下:

% Define the matrix
A = [1 2 3; 2 4 6.0001; 3 6 9];

% Compute RREF with high tolerance
tol = 0.01;
R_high_tol = rref(A, tol);

% Compute RREF with low tolerance
tol = 1e-6;
R_low_tol = rref(A, tol);

% Display the results
disp('Reduced Row Echelon Form with high tolerance:');
disp(R_high_tol);

disp('Reduced Row Echelon Form with low tolerance:');
disp(R_low_tol);

執行後,得到的輸出為:

>> % Define the matrix
A = [1 2 3; 2 4 6.0001; 3 6 9];

% Compute RREF with high tolerance
tol = 0.01;
R_high_tol = rref(A, tol);

% Compute RREF with low tolerance
tol = 1e-6;
R_low_tol = rref(A, tol);

% Display the results
disp('Reduced Row Echelon Form with high tolerance:');
disp(R_high_tol);

disp('Reduced Row Echelon Form with low tolerance:');
disp(R_low_tol);

Reduced Row Echelon Form with high tolerance:
     1     2     3
     0     0     0
     0     0     0

Reduced Row Echelon Form with low tolerance:
     1     2     0
     0     0     1
     0     0     0

>> 
廣告
© . All rights reserved.