When should I use x = A\b and when should I use x = inv(A)*b ?

290 views (last 30 days)
When should I use x = A\b and when should I use x = inv(A)*b?
Why should I use x = A\b rather than x= inv(A)*b?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
For a system of equations expressed as
A*x = b;
generally, you should use x = inv(A)*b only if the system is of full rank and is well-conditioned. The inverse of A exists only for well defined full-rank matrices and thus cannot be used for over- or under-defined systems. You should use x=A\b for an over- or under-defined system, or one which is badly conditioned.
For a well defined system, both the methods will yield the same result. The following example illustrates the above:
rand('state',0)
A = round(10*rand(4,4));
b = round(20*rand(4,1));
The A and b define a system of equations A*x = b. The RANK function
rank(A)
ans =
4
shows that A is a well defined system. The COND function
cond(A)
ans =
11.1361
shows that A is fairly well-conditioned. Thus to obtain the solution of the system A*x = b, both the INV function and the backslash operator can be used.
x1 = inv(A)*b
x1 =
-1.4378
-0.0922
2.2467
1.8038
x2 = A\b
x2 =
-1.4378
-0.0922
2.2467
1.8038
Even though both of the above procedures yield similar results for a well-defined and well-conditioned system, we recommend that you use the '\' operator to solve a system of linear equations, as it will handle both well-conditioned and ill-conditioned matrices robustly and can handle over- or under-determined systems. INV requires the coefficient matrix to be square.
rand('state', 0)
A = [round(10*rand(3,4))]; % an over determined system
b = round(10*rand(3,1));
For this example, you cannot use x = inv(A)*b because A is not square. In this case you will have to use x = A\b.
x = A\b
x =
1.2400
0.5022
-1.1822
0
More information on the \ operator can be found by typing HELP SLASH at the MATLAB command prompt. More information on kinds of systems is given below.
A system of linear equations can be expressed as one of the following:
(a) Well-defined systems: The system where the number of unknown variables are the same as the number of linearly independent equations (LIE) in the system.
(b) Under-defined systems: The system where the number of unknown variables exceeds the number of LIE relating these variables.
(c) Over-defined systems The system where the number of unknown variables are less then the number of LIE relating these variables.
Mathematically, if A*x = b, then the definition of the system can be obtained as follows:
If A is a square matrix and the rank of A is equal to the length of the matrix, then the matrix is well defined. If A is of size m x n and m>n, then the system is underdefined. If m<n, the system is overdefined.

More Answers (0)

Categories

Find more on Linear Algebra in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!