MATLAB中的行最簡形矩陣(rref)
行最簡形矩陣(rref)是一個簡化矩陣,用於求解線性方程組。MATLAB提供了一些內建函式來查詢給定矩陣的行最簡形矩陣。閱讀本文以瞭解使用MATLAB查詢給定矩陣的行最簡形矩陣的方法。在此之前,讓我們先概述一下rref矩陣及其屬性。
什麼是行最簡形矩陣?
線上性代數中,行最簡形矩陣,也稱為rref矩陣,是一個特殊的簡化矩陣,用於求解線性方程組。rref矩陣在其他一些數學應用中也很有用,因為它具有一些獨特的屬性。
下面解釋了行最簡形矩陣的一些關鍵屬性:
在rref矩陣的每一行中,最左邊的非零元素稱為主元元素,它等於1。
在rref矩陣中,主元元素(1)上方和下方的所有元素都為零。
在rref矩陣中,所有元素都為零的行將是底行。
在rref矩陣中,每一列包含一個主元元素(1),其他所有元素都等於0。
在rref矩陣中,行的順序是這樣的:對於任意兩行非零行,下行的主元元素(1)位於上行主元元素(1)的右側。
讓我們來看一個例子,瞭解如何找到給定矩陣的行最簡形矩陣。
考慮下面給出的矩陣A:
$$\mathrm{A \: = \: \begin{bmatrix}1 & 5 & 3 \\ 5 & 6 & 2 \\ 9 & 8 & 5 \end{bmatrix}}$$
以下是查詢A的行最簡形矩陣的分步過程。
步驟1 - 在第一列中獲得主元元素(1)。這可以透過將第一行乘以1來實現,如下所示:
$$\mathrm{\begin{bmatrix}1 \: \times 1 & 5 \: \times 1 & 3 \: \times 1 \\ 5 & 6 & 2 \\ 9 & 8 & 5 \end{bmatrix} \: \rightarrow \begin{bmatrix}1 & 5 & 3 \\ 5 & 6 & 2 \\ 9 & 8 & 5 \end{bmatrix}}$$
步驟2 - 減少第一列中主元1下方的元素。為了實現這一點,我們將第一行乘以-5並將結果新增到第二行。然後,我們將第一行乘以-9並將結果新增到第三行,如下所示:
將第一行乘以(-5) -
$$\mathrm{\begin{bmatrix}1 \: \times (-5) & 5 \: \times (-5) & 3 \: \times (-5) \\ 5 & 6 & 2 \\ 9 & 8 & 5 \end{bmatrix} \: \rightarrow \begin{bmatrix}-5 & -25 & -15 \\ 5 & 6 & 2 \\ 9 & 8 & 5 \end{bmatrix}}$$
將第一行加到第二行 -
$$\mathrm{\begin{bmatrix}-5 & -25 & -15 \\ 5 \: + (-5) & 6 \: + (-25) & 2 \: + (-15) \\ 9 & 8 & 5 \end{bmatrix} \rightarrow \begin{bmatrix}1 & 5 & 3 \\ 0 & -19 & -13 \\ 9 & 8 & 5 \end{bmatrix}}$$
同樣,將第一行乘以-9並將結果新增到第三行,我們得到:
$$\mathrm{\begin{bmatrix}1 & 5 & 3 \\ 0 & -19 & -13 \\ 0 & -37 & -22 \end{bmatrix}}$$
步驟3 - 將步驟(2)中獲得的矩陣的第二列的第二個元素設為主元1。我們可以透過將第二行除以(-19)來實現這一點,如下所示:
$$\mathrm{\begin{bmatrix}1 & 5 & 3 \\ \frac{0}{-19} & -\frac{19}{-19} & -\frac{13}{-19} \\ 0 & -37 & -22 \end{bmatrix} \rightarrow \begin{bmatrix}1 & 5 & 3 \\ 0 & 1 & \frac{13}{19} \\ 0 & -37 & -22 \end{bmatrix}}$$
步驟4 - 減少步驟(3)中獲得的矩陣中第二列中主元1上方和下方的元素。
為了使上面的元素為0,我們將第二行乘以(-5)並將結果新增到第一行。
為了使下面的元素為0,我們將第二行乘以37並將結果新增到第三行。
因此,我們得到以下結果矩陣:
$$\mathrm{\begin{bmatrix}1 & 5 & 3 \\ 0 & 1 & \frac{13}{19} \\ 0 & -37 & -22 \end{bmatrix} \rightarrow \begin{bmatrix}1 & 0 & -\frac{8}{19} \\ 0 & 1 & \frac{13}{19} \\ 0 & 0 & \frac{63}{19} \end{bmatrix}}$$
步驟5 - 在步驟(4)中獲得的矩陣的第三列中獲得主元1。我們透過將第三行除以(19/63)來實現這一點,即:
$$\mathrm{\begin{bmatrix}1 & 0 & -\frac{8}{19} \\ 0 & 1 & \frac{13}{19} \\ 0 & 0 & \frac{63}{19} \end{bmatrix} \rightarrow \begin{bmatrix}1 & 0 & -\frac{8}{19} \\ 0 & 1 & \frac{13}{19} \\ 0 & 0 & 1 \end{bmatrix}}$$
步驟6 - 將第三列中主元1上方的元素簡化為零。
為此,我們將第三行乘以(19/8)並將結果新增到第一行,並將第三行乘以(-19/13)並將結果新增到第二行。我們得到以下結果矩陣。
$$\mathrm{\begin{bmatrix}1 & 0 & -\frac{8}{19} \\ 0 & 1 & \frac{13}{19} \\ 0 & 0 & 1 \end{bmatrix} \rightarrow \begin{bmatrix}1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{bmatrix}}$$
這就是矩陣A的行最簡形矩陣。
這就是關於行最簡形矩陣(rref)基礎知識的全部內容。現在讓我們討論如何使用MATLAB查詢給定矩陣的rref矩陣。
如何在MATLAB中查詢行最簡形矩陣(rref)?
MATLAB提供了一個內建函式“rref”,我們可以用它來獲得給定矩陣的行最簡形矩陣(rref)。
“rref”函式可以具有以下語法格式:
R = rref(A);
R = rref(A, tol);
[R, p] = rref(A);
讓我們藉助MATLAB程式設計中的示例詳細討論這些函式。
查詢行最簡形矩陣
如上所述,在MATLAB中,我們可以使用“rref”函式來查詢給定矩陣A的行最簡形矩陣R。
以下是使用MATLAB查詢rref矩陣的步驟:
步驟1 - 提供輸入矩陣A。
步驟2 - 使用“rref”函式查詢矩陣A的rref矩陣。
步驟3 - 使用“disp”函式顯示結果。
示例1
讓我們來看一個例子來理解這個過程。
% MATLAB code to find rref matrix
% Create a sample matrix
A = [1, 5, 3; 5, 6, 2; 9, 8, 5];
% Obtain the reduced row echelon form matrix
R = rref(A);
% Display the original and rref matrices
disp('Original Matrix A:');
disp(A);
disp('Reduced Row Echelon Form Matrix R:');
disp(R);
輸出
它將產生以下輸出:
Original Matrix A:
1 5 3
5 6 2
9 8 5
Reduced Row Echelon Form Matrix R:
1 0 0
0 1 0
0 0 1
查詢具有指定容差的行最簡形矩陣
在MATLAB中,我們可以使用具有指定容差的“rref”函式來確定矩陣的秩。此函式的程式碼實現與之前的相同。
示例2
以下示例顯示了查詢具有指定容差的rref矩陣的過程。
% MATLAB code to find rref matrix with specified tolerance
% Create a sample matrix
A = [2, 9, 3; 4, 6, 1; 9, 7, 5];
% Specify a tolerance value desired
tol = 1e-7;
% Obtain the rref matrix with tolerance
R = rref(A, tol);
% Display the original matrix and rref matrix
disp('Original Matrix A:');
disp(A);
disp('Reduced Row Echelon Form Matrix R:');
disp(R);
輸出
它將產生以下輸出:
Original Matrix A: 2 9 3 4 6 1 9 7 5 Reduced Row Echelon Form Matrix R: 1 0 0 0 1 0 0 0 1
查詢行最簡形矩陣以及主元列
在MATLAB中,我們還可以使用“rref”函式來查詢行最簡形矩陣和一個包含rref矩陣的主元列索引的向量。
示例3
以下示例顯示了使用MATLAB查詢rref矩陣和主元列的程式碼實現。
% MATLAB code to find rref matrix and pivot columns
% Create a sample matrix
A = [3, 1, 5; 2, 6, 2; 7, 1, 4];
% Find the rref matrix and pivot columns
[R, p] = rref(A); % R = rref matrix, p = pivot columns
% Display the original matrix, rref matrix, and pivot columns
disp('Original Matrix A:');
disp(A);
disp('Reduced Row Echelon Form Matrix R:');
disp(R);
disp('Pivot Columns p:');
disp(p);
輸出
它將產生以下輸出:
Original Matrix A: 3 1 5 2 6 2 7 1 4 Reduced Row Echelon Form Matrix R: 1 0 0 0 1 0 0 0 1 Pivot Columns p: 1 2 3
現在讓我們討論如何使用行最簡形矩陣在MATLAB中求解線性方程組。
使用MATLAB中的行最簡形矩陣求解方程組
為了理解如何使用rref矩陣求解線性方程組,讓我們來看一個例子。
假設我們有一個具有3個方程和3個未知數的線性方程組,如下所示。
$$\mathrm{a \: + \: 3b \: + \: 5c \: = \: 10}$$
$$\mathrm{5a \: + \: 4b \: + \: c \: = \: 2}$$
$$\mathrm{7a \: + \: 5b \: + \: 10c \: = \: 7}$$
首先,我們為給定的方程組建立一個增廣矩陣,如下所示。
A = [1 3 5; 5 4 1; 7 5 10]; % Left-hand side array of coefficients b = [10; 2; 7]; % Right-hand side vector
增廣矩陣將是:
augmented_mat = [A, b];
然後,我們將找到這個增廣矩陣的rref矩陣並求解它以找到未知數的值。
示例4
讓我們藉助MATLAB程式設計中的一個例子來理解這個過程。
% MATLAB code to solve linear equations using rref matrix
% Provide the matrix of LHS coefficients
A = [1 3 5; 5 4 1; 7 5 10];
% Provide the vector of RHS constants
b = [10; 2; 7];
% Create the augmented matrix for the given system
augmented_mat = [A, b];
% Obtain the reduced row echelon form matrix of augmented matrix
R = rref(augmented_mat);
% Use the rref matrix to find the solution of equations
S = R(:, end);
% Display the augmented matrix, rref matrix, and solution matrix
disp('Augmented Matrix:');
disp(augmented_mat);
disp('Reduced Row Echelon Form Matrix:');
disp(R);
disp('Solution Matrix:');
disp(S);
輸出
它將產生以下輸出:
Augmented Matrix:
1 3 5 10
5 4 1 2
7 5 10 7
Reduced Row Echelon Form Matrix:
1.0000 0 0 -2.0275
0 1.0000 0 2.8624
0 0 1.0000 0.6881
Solution Matrix:
-2.0275
2.8624
0.6881
此示例演示瞭如何在MATLAB中使用行最簡形矩陣求解給定的線性方程組。
結論
總而言之,行最簡形矩陣(rref)是矩陣的一種簡化形式,用於查詢矩陣的秩或求解線性方程組。MATLAB提供了一個內建函式“rref”,我們可以用它來查詢給定矩陣的rref矩陣。
在本文中,我解釋了使用MATLAB中的“rref”函式查詢行最簡形矩陣的方法。我還解釋了使用rref矩陣求解線性方程組的過程。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP