Main Content

本页翻译不是最新的。点击此处可查看最新英文版本。

mldivide, \

求解关于 x 的线性方程组 Ax = B

说明

示例

x = A\B 对线性方程组 A*x = B 求解。矩阵 AB 必须具有相同的行数。如果 A 未正确缩放或接近奇异值,MATLAB® 将会显示警告信息,但还是会执行计算。

  • 如果 A 是标量,那么 A\B 等于 A.\B

  • 如果 An×n 方阵,Bn 行矩阵,那么 x = A\B 是方程 A*x = B 的解(如果存在解的话)。

  • 如果 A 是矩形 m×n 矩阵,且 m ~= nBm 行矩阵,那么 A\B 返回方程组 A*x= B 的最小二乘解。

x = mldivide(A,B) 是执行 x = A\B 这一操作的替代方法,但很少使用。它可以启用类的运算符重载。

示例

全部折叠

对简单的线性方程组 A*x = B 求解。

A = magic(3);
B = [15; 15; 15];
x = A\B
x = 3×1

    1.0000
    1.0000
    1.0000

对包含奇异矩阵 A 的线性方程组 A*x = b 求解。

A = magic(4);
b = [34; 34; 34; 34];
x = A\b
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND =  1.306145e-17.
x = 4×1

    1.5000
    2.5000
   -0.5000
    0.5000

如果 rcond0eps 之间,MATLAB® 会发出接近奇异的警告,但还会继续计算。如果处理的是病态矩阵,尽管残差 (b-A*x) 相对较小,还是有可能出现不可靠解。在此特例中,残差的范数是零,尽管 rcond 很小,仍能求得精确解。

rcond 等于 0 时,会出现奇异警告。

A = [1 0; 0 0];
b = [1; 1];
x = A\b
Warning: Matrix is singular to working precision.
x = 2×1

     1
   Inf

这种情况下,除以零会导致计算涉及 Inf 和/或 NaN,使得计算结果不可靠。

对线性方程组 A*x = b 求解。

A = [1 2 0; 0 4 3];
b = [8; 18];
x = A\b
x = 3×1

         0
    4.0000
    0.6667

使用稀疏矩阵对简单的线性方程组求解。

考虑以下矩阵方程 A*x = B

A = sparse([0 2 0 1 0; 4 -1 -1 0 0; 0 0 0 3 -6; -2 0 0 0 2; 0 0 4 2 0]);
B = sparse([8; -1; -18; 8; 20]);
x = A\B
x = 
   (1,1)       1.0000
   (2,1)       2.0000
   (3,1)       3.0000
   (4,1)       4.0000
   (5,1)       5.0000

输入参数

全部折叠

操作数,指定为向量、满矩阵或稀疏矩阵。AB 必须具有相同的行数。

  • 如果 AB 具有整数数据类型,则另一个输入必须为标量。整数数据类型的操作数不能为复数。

数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char
复数支持:

输出参数

全部折叠

解,以向量、满矩阵或稀疏矩阵返回。如果 Am×n 矩阵,Bm×p 矩阵,那么 xn×p 矩阵,包括 p==1 的情况。

如果 A 具有满存储,则 x 也是完全的。如果 A 是稀疏矩阵,则 xB 具有相同的存储。

提示

  • 运算符 /\ 通过以下对应关系而相互关联:B/A = (A'\B')'

  • 如果 A 是方阵,则 A\B 约等于 inv(A)*B,但 MATLAB 会用不同的更为稳健的方式处理 A\B,而且效果更好。

  • 如果 A 的秩小于 A 中的列数,则 x = A\B 不一定是最小范数解。您可以使用 x = lsqminnorm(A,B)x = pinv(A)*B 来计算最小范数最小二乘解。

  • 使用 decomposition 对象多次高效地求解具有不同右侧的线性方程组。decomposition 对象非常适合求解需要重复求解的问题,因为系数矩阵的分解不需要执行多次。

算法

全部折叠

mldivide 在求解线性方程组方面的多用性源于其能够在调度适当的求解器时利用问题中的对称性。此方法旨在最大限度减少计算时间。该函数首先区分(也称为“稠密”)和稀疏输入数组。

针对完全输入的算法

下面的流程图显示当输入 AB完全时的算法路径。

The properties of full input matrices determine which algorithm mldivide uses to solve the linear system

针对稀疏输入的算法

如果 A 是满矩阵,B 是稀疏矩阵,则 mldivideB 转换为满矩阵并使用完整算法路径(如上)计算具有满存储的解。如果 A 是稀疏矩阵,则解 x 的存储与 B 的解相同,并且 mldivide 遵循稀疏输入的算法路径,如下所示。

The properties of sparse input matrices determine which algorithm mldivide uses to solve the linear system

参考

[1] Gilbert, John R., and Tim Peierls. “Sparse Partial Pivoting in Time Proportional to Arithmetic Operations.” SIAM Journal on Scientific and Statistical Computing 9, no. 5 (September 1988): 862–874. https://doi.org/10.1137/0909058.

[2] Anderson, E., ed. LAPACK Users’ Guide. 3rd ed. Software, Environments, Tools. Philadelphia: Society for Industrial and Applied Mathematics, 1999. https://doi.org/10.1137/1.9780898719604.

[3] Davis, Timothy A. "Algorithm 832: UMFPACK V4.3 – an unsymmetric-pattern multifrontal method." ACM Transactions on Mathematical Software 30, no. 2 (June 2004): 196–199. https://doi.org/10.1145/992200.992206.

[4] Duff, Iain S. “MA57---a Code for the Solution of Sparse Symmetric Definite and Indefinite Systems.” ACM Transactions on Mathematical Software 30, no. 2 (June 2004): 118–144. https://doi.org/10.1145/992200.992202.

[5] Davis, Timothy A., John R. Gilbert, Stefan I. Larimore, and Esmond G. Ng. “Algorithm 836: COLAMD, a Column Approximate Minimum Degree Ordering Algorithm.” ACM Transactions on Mathematical Software 30, no. 3 (September 2004): 377–380. https://doi.org/10.1145/1024074.1024080.

[6] Amestoy, Patrick R., Timothy A. Davis, and Iain S. Duff. “Algorithm 837: AMD, an Approximate Minimum Degree Ordering Algorithm.” ACM Transactions on Mathematical Software 30, no. 3 (September 2004): 381–388. https://doi.org/10.1145/1024074.1024081.

[7] Chen, Yanqing, Timothy A. Davis, William W. Hager, and Sivasankaran Rajamanickam. “Algorithm 887: CHOLMOD, Supernodal Sparse Cholesky Factorization and Update/Downdate.” ACM Transactions on Mathematical Software 35, no. 3 (October 2008): 1–14. https://doi.org/10.1145/1391989.1391995.

[8] Davis, Timothy A. “Algorithm 915, SuiteSparseQR: Multifrontal Multithreaded Rank-Revealing Sparse QR Factorization.” ACM Transactions on Mathematical Software 38, no. 1 (November 2011): 1–22. https://doi.org/10.1145/2049662.2049670.

扩展功能

版本历史记录

在 R2006a 之前推出

全部展开