让我们看看如何在MATLAB中求解线性方程组。这是我们将部署以执行任务的各种运算符:
- \运算符:
A \ B
是A到B的矩阵除法,与INV(A) * B
大致相同。如果A是NXN矩阵,而B是具有N个分量的列向量或具有多个此类列的矩阵,则X = A \ B
是方程A * X = B
。如果A缩放错误或接近奇数,则会打印警告消息。A\EYE(SIZE(A))
产生A\EYE(SIZE(A))
的倒数。 - linsolve运算符:
X = LINSOLVE(A, B)
使用线性分解的LU分解(当A为正方形时X = LINSOLVE(A, B)
求解线性系统A * X = B,使用列分解的QR分解求解线性系统A * X =B。如果A对于正方形矩阵不适定,而对于矩形矩阵秩不足,则会给出警告。
示例1:非齐次系统Ax = b,其中A是一个正方形并且是可逆的。在我们的示例中,我们将考虑以下方程式:
2x + y - z = 7
x -2y + 5z = -13
3x + 5y - 4z = 18
我们将这些方程式转换为矩阵A和b:
% declaring the matrices based on the equations
A = [2 1 -1; 1 -2 5; 3 5 -4]
b = [7; -13; 18]
输出 :
A =
2 1 -1
1 -2 5
3 5 -4
b =
7
-13
18
现在,我们将创建一个增强矩阵Ab。我们将比较Ab和A的等级,如果等级相等,则存在唯一解。
% creating augmented matrix
Ab = [A b]
% checking the ranks
if rank(A) == rank(Ab)
display("Unique solution exists")
else
display("Unique solution does not exist")
end
输出 :
Ab =
2 1 -1 7
1 -2 5 -13
3 5 -4 18
Unique solution exists
现在,我们可以使用3种方法找到该方程组的解:
- 常规方式:
inv(A) * b
- 使用中分程序:
A \ b
- 使用linsolve例程:
linsolve(A, b)
% conventional way of finding solution
x_inv = inv(A) * b
% using mid-divide routine of MATLAB
x_bslash = A \ b
% using linsolve routine of MATLAB
x_linsolve = linsolve(A, b)
输出 :
x_inv =
2.0000e+00
8.8818e-16
-3.0000e+00
x_bslash =
2.0000e+00
9.6892e-16
-3.0000e+00
x_linsolve =
2.0000e+00
9.6892e-16
-3.0000e+00
我们可以通过使用A * x - b
查找错误来验证解决方案的正确性。错误应为0。
% check for errors
Er1 = A * x_inv - b
Er2 = A * x_bslash - b
Er3 = A * x_linsolve - b
输出 :
Er1 =
-8.8818e-16
-3.5527e-15
0.0000e+00
Er2 =
-1.7764e-15
-1.7764e-15
0.0000e+00
Er3 =
-1.7764e-15
-1.7764e-15
0.0000e+00
由于所有错误都接近于0,因此可以说该解决方案是正确的。
示例2:非齐次系统Ax = b,其中A为正方形,并且不可逆。在我们的示例中,我们将考虑以下方程式:
2x + 4y + 6z = 7
3x -2y + 1z = 2
1x + 2y + 3z = 5
% declaring the matrices based on the equations
A = [2 4 6; 3 -2 1; 1 2 3]
b = [7; 2; 5]
% creating augmented matrix
Ab = [A b]
% checking the ranks
if rank(A) == rank(Ab)
display("Unique solution exists")
else
display("Unique solution does not exist")
end
% conventional way of finding solution
% gives warning and wrong answer.
x_inv = inv(A) * b
% using mid-divide routine of MATLAB
% this too gives warning and wrong answer.
x_bslash = A \ b
% check for errors
Er1 = A * x_inv - b
Er2 = A * x_bslash - b
输出 :
A =
2 4 6
3 -2 1
1 2 3
b =
7
2
5
Ab =
2 4 6 7
3 -2 1 2
1 2 3 5
Unique solution does not exist
warning: matrix singular to machine precision
warning: called from
testing at line 17 column 7
x_inv =
Inf
Inf
Inf
warning: matrix singular to machine precision
warning: called from
testing at line 21 column 10
x_bslash =
-Inf
-Inf
Inf
Er1 =
Inf
NaN
Inf
Er2 =
NaN
NaN
NaN
示例3:非齐次系统Ax = b,其中A不是正方形。在我们的示例中,我们将考虑以下方程式:
2a + c - d + e = 2
a + c - d + e = 1
12a + 2b + 8c + 2e = 12
% declaring the matrices based on the equations
A = [2 0 1 -1 1; 1 0 1 -1 1; 12 2 8 0 2]
b = [2; 1; 12]
% creating augmented matrix
Ab = [A b]
% checking the ranks
if rank(A) == rank(Ab)
display("Solution exists")
else
display("Solution does not exist")
end
% checking for unique solution
if rank(A) == 5
display("Unique solution exists")
else
display("Unique solution does not exist")
end
输出 :
A =
2 0 1 -1 1
1 0 1 -1 1
12 2 8 0 2
b =
2
1
12
Ab =
2 0 1 -1 1 2
1 0 1 -1 1 1
12 2 8 0 2 12
Solution exists
Unique solution does not exist
示例4:齐次系统Ax = 0,其中A是一个正方形,并且是可逆的。在我们的示例中,我们将考虑以下方程式:
6x + 2y + 3z = 0
4x - y + 2z = 0
2x + y + 5z = 0
% declaring the matrices based on the equations
A = [6 2 3; 4 -1 2; 2 1 5]
b = [0; 0; 0]
% checking for unique solution
if rank(A) == 3
display("Unique solution exists")
else
display("Unique solution does not exist")
endif
% trivial solution
x = A \ b
% getting a null set.
% this is obvious as A is invertible.
% so its null space contains only zero vector
x = null(A)
输出 :
A =
6 2 3
4 -1 2
2 1 5
b =
0
0
0
Unique solution exists
x =
0
0
0
x = [](3x0)
示例5:齐次系统Ax = 0,其中A是正方形,并且不可逆。在我们的示例中,我们将考虑以下方程式:
1x + 2y + 3z = 0
4x + 5y + 6z = 0
7x + 8y + 9z = 0
% declaring the matrices based on the equations
A = [1 2 3; 4 5 6; 7 8 9]
b = [0; 0; 0]
% checking for unique solution
if rank(A) == 3
display("Unique solution exists")
else
display("Unique solution does not exist")
endif
% trivial solution with warning
x = A \ b
% this will return a set containing
% only one basis vector of null space of A
% the null space of A is spanned by this vector
% hence this vector or its scalar multiple
% is the solution of the given homogeneous system
x = null(A)
% finding the errors
Err = A*x - b
输出 :
A =
1 2 3
4 5 6
7 8 9
b =
0
0
0
Unique solution does not exist
warning: matrix singular to machine precision, rcond = 1.54198e-18
warning: called from
testing at line 13 column 3
x =
0
0
0
x =
0.40825
-0.81650
0.40825
Err =
-1.3323e-15
-4.4409e-16
4.4409e-16