Question about A\b
8 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
John D'Errico
el 9 de Dic. de 2017
Editada: John D'Errico
el 9 de Dic. de 2017
It looks like everyone in that same class will be asking this virtually identical question.
Respuestas (1)
Matt J
el 8 de Dic. de 2017
Editada: Matt J
el 8 de Dic. de 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 comentarios
Matt J
el 9 de Dic. de 2017
Editada: Matt J
el 9 de Dic. de 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".
Ver también
Categorías
Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!