Code Analyzer: INV is slow and inaccurate

前端 未结 3 1403
一向
一向 2021-01-23 09:16

When I try to calculate a matrix inverse using Matlab\'s inv() operation:

A = rand(10,10);
b = rand(10,1);

C = inv(A);
D = C*b;

I get the foll

3条回答
  •  醉酒成梦
    2021-01-23 10:02

    You should listen to Matlab and use the second option. inv(A)*b and A\b are computed with different algorithms under the hood, and \ is indeed more accurate.

    The documentation for inv states:

    In practice, it is seldom necessary to form the explicit inverse of a matrix. A frequent misuse of inv arises when solving the system of linear equations Ax = b. One way to solve this is with x = inv(A)*b. A better way, from both an execution time and numerical accuracy standpoint, is to use the matrix division operator x = A\b. This produces the solution using Gaussian elimination, without forming the inverse. See mldivide () for further information.

提交回复
热议问题