Question about A\b
8 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi everyone,
I really like A\b (just 3 letters and it can do magic). However, I run into this problem.
>> A = [1 1; 1 1]
A =
1 1
1 1
>> b = [1; 1]
b =
1
1
>> A\b
Warning: Matrix is singular to working precision.
(Type "warning off MATLAB:singularMatrix" to suppress this warning.)
ans =
NaN
NaN
I understand that A is not invertible. However there is a solution and I kind of expect MATLAB will return [1; 0].
Or maybe I have the wrong expectation.
3 commentaires
John D'Errico
le 9 Déc 2017
Modifié(e) : John D'Errico
le 9 Déc 2017
It looks like everyone in that same class will be asking this virtually identical question.
Réponses (1)
Matt J
le 8 Déc 2017
Modifié(e) : Matt J
le 8 Déc 2017
It's really all a question of which linear solver you use. Each has its own idea of which of the infinite solutions to choose from. One alternative is,
>> pinv(A)*b
ans =
0.5000
0.5000
Another is,
>> lscov(A,b)
Warning: A is rank deficient to within machine precision.
> In lscov (line 200)
ans =
1.0000
0
3 commentaires
Matt J
le 9 Déc 2017
Modifié(e) : Matt J
le 9 Déc 2017
You may have found a corner case, but LSCOV should always return the solution with a maximum number of zeros. From the documentation:
x = lscov(A,B) returns the ordinary least squares solution to the linear system of equations A*x = B, i.e., x is the n-by-1 vector that minimizes the sum of squared errors (B - A*x)'*(B - A*x), where A is m-by-n, and B is m-by-1. B can also be an m-by-k matrix, and lscov returns one solution for each column of B. When rank(A) < n, lscov sets the maximum possible number of elements of x to zero to obtain a "basic solution".
Voir également
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!