Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Warning: Matrix is singular to working precision

Subject: Warning: Matrix is singular to working precision

From: saneman

Date: 30 May, 2008 09:20:36

Message: 1 of 11

I am working on rather large matrices and now and then I get the warning:

Warning: Matrix is singular to working precision

when I take the inverse of a matrix. Why does some matrices give a singular
result when inverting them?

Subject: Warning: Matrix is singular to working precision

From: Sebastiaan

Date: 30 May, 2008 09:55:04

Message: 2 of 11

This has a computational origin. When numbers in the matrix
are close to the machine precision (eps), the number of
independent rows from the computers' point of view is less
than the number of independent rows from the mathematical
point of view.

A simple example. Take the following matrix:
A = [1 0.705;
     1.41 1];
b = [1;
     sqrt(2)];

Basically, we put sqrt(2) in 2 decimals precision in the
matrix. Solving A\b gives
[0.5;
 0.708]

However, putting
c = [1;
     1.41];
and then A\c gives:
[1;
 0]

as solution. So, rounding off can be bad.

Now back to inverting a matrix. Say this is your real matrix:
A = [1 0
     0 1e-3];
While this is a matrix with rank(2) (2 independent rows),
the matrix is badly scaled, since the last element is close
to 0. cond(A) gives a condition of 1000.

Now try inv(A) on this for decreasing the last element, e.g.
1e-6, 1e-12, 1e-16, 1e-20. The last element becomes close
and closer to 0, until it becomes indistinguishable from 0.
Then inv(A) starts to complain (or, warn you).

You should read something about rounding off errors when
working with limited precision.

Subject: Warning: Matrix is singular to working precision

From: saneman

Date: 30 May, 2008 10:11:04

Message: 3 of 11


"Sebastiaan " <s.breedveld@erasmusmc.REMOVE.BOO.BOO.nl> skrev i en
meddelelse news:g1oito$8b0$1@fred.mathworks.com...
> This has a computational origin. When numbers in the matrix
> are close to the machine precision (eps), the number of
> independent rows from the computers' point of view is less
> than the number of independent rows from the mathematical
> point of view.
>
> A simple example. Take the following matrix:
> A = [1 0.705;
> 1.41 1];
> b = [1;
> sqrt(2)];
>
> Basically, we put sqrt(2) in 2 decimals precision in the
> matrix. Solving A\b gives
> [0.5;
> 0.708]
>
> However, putting
> c = [1;
> 1.41];
> and then A\c gives:
> [1;
> 0]
>
> as solution. So, rounding off can be bad.
>
> Now back to inverting a matrix. Say this is your real matrix:
> A = [1 0
> 0 1e-3];
> While this is a matrix with rank(2) (2 independent rows),
> the matrix is badly scaled, since the last element is close
> to 0. cond(A) gives a condition of 1000.
>
> Now try inv(A) on this for decreasing the last element, e.g.
> 1e-6, 1e-12, 1e-16, 1e-20. The last element becomes close
> and closer to 0, until it becomes indistinguishable from 0.
> Then inv(A) starts to complain (or, warn you).
>
> You should read something about rounding off errors when
> working with limited precision.
>

I have read that you can use pinv (pseudo-inverse) for these kind of
matrices.

Subject: Warning: Matrix is singular to working precision

From: Sebastiaan

Date: 30 May, 2008 10:53:02

Message: 4 of 11

> I have read that you can use pinv (pseudo-inverse) for
these kind of
> matrices.
>
>

Alternatively, when you want to use and inverted matrix to
solve a linear system, it is often better (and faster, in
decomposition and in solving) to use a decomposition, e.g.:
[L,U] = lu(A);

and then use backsolve: Ax = b, or LUx = b and:
x = U\(L\b);

Subject: Warning: Matrix is singular to working precision

From: carlos lopez

Date: 30 May, 2008 13:27:02

Message: 5 of 11

"saneman" <asd@ad.com> wrote in message
> I have read that you can use pinv (pseudo-inverse) for
these kind of
> matrices.
Hello Saneman:
If you are shure that your matrix is invertible maybe you
could do better trying to understand why it is reported as
singular. I can provide a cue: Is there any row/column which
entries are sensibly larger than the other columns? if so,
you might try to change your variables in order to scale the
elements to similar size, solve the system and recover the
original variable. Like this:
A=[1e-10 1e6; 3e-10 -5e7];b=[1 ;1];
x=A\b;
Warning: Matrix is close to singular or badly scaled.
         Results may be inaccurate. RCOND = 1.698113e-017.
If you just change variables to y=[1e-10*x(1); 1e6*x(2)],
your system of equations will look as
Ascaled=[1 1; 3 -50];
y=Ascaled\b;%solve for dummy unknwons
x=[1e10*y(1);1e-6*y(2)];%recover the original unknowns

residual=A*x-b %check that solution is fine
residual =
  1.0e-015 *
    0.2220
   -0.2220
which are ok (indeed equal to eps).
This is a very broad topic; unfortunately I am not an
expert, but i believe that "preconditioning" could be the
keyword here.
Hope this helps
Regards
Carlos

Subject: Warning: Matrix is singular to working precision

From: saneman

Date: 30 May, 2008 16:11:22

Message: 6 of 11


"carlos lopez" <clv2clv_00000000_@adinet.com.uy> skrev i en meddelelse
news:g1ovb6$ies$1@fred.mathworks.com...
> "saneman" <asd@ad.com> wrote in message
>> I have read that you can use pinv (pseudo-inverse) for
> these kind of
>> matrices.
> Hello Saneman:
> If you are shure that your matrix is invertible maybe you
> could do better trying to understand why it is reported as
> singular. I can provide a cue: Is there any row/column which
> entries are sensibly larger than the other columns? if so,
> you might try to change your variables in order to scale the
> elements to similar size, solve the system and recover the
> original variable. Like this:
> A=[1e-10 1e6; 3e-10 -5e7];b=[1 ;1];
> x=A\b;
> Warning: Matrix is close to singular or badly scaled.
> Results may be inaccurate. RCOND = 1.698113e-017.
> If you just change variables to y=[1e-10*x(1); 1e6*x(2)],
> your system of equations will look as
> Ascaled=[1 1; 3 -50];
> y=Ascaled\b;%solve for dummy unknwons
> x=[1e10*y(1);1e-6*y(2)];%recover the original unknowns
>
> residual=A*x-b %check that solution is fine
> residual =
> 1.0e-015 *
> 0.2220
> -0.2220
> which are ok (indeed equal to eps).
> This is a very broad topic; unfortunately I am not an
> expert, but i believe that "preconditioning" could be the
> keyword here.


Be preconditioning do you mean som kind of normalization of the data? There
are some columns that only contains 0.

Subject: Warning: Matrix is singular to working precision

From: Greg Heath

Date: 30 May, 2008 16:12:17

Message: 7 of 11

On May 30, 5:20 am, "saneman" <a...@ad.com> wrote:
> I am working on rather large matrices and now and then I get the warning:
>
> Warning: Matrix is singular to working precision
>
> when I take the inverse of a matrix. Why does some matrices give a singular
> result when inverting them?

The real question is why are you trying to invert any matrix?
If you are trying to solve the linear system

                                    A*X = B,

the preferred solution is

                                     X = A\B

because BACKSLASH gives more stable solutions than INV when
A is nonsingular and yields a "basic" solution when A is singular
but B is in the range of A. Search the archives using

greg-heath basic-solution.

If the matrix A is ill-conditioned because it is badly scaled, you
can row and column scale it with invertible diagonal matrices R
and C, having elements that are integer powers of 2 so that

                 -2^p <= D = R*A*C <= 2^p

where the integer p is arbitrary but usually chosen to be zero.
Then the solution to

                       A*X = B
is

                      X = C*Y

where Y is the solution to

                      D*Y = R*B.

obtained from

                       Y = D\R*B.

Powers of two are used because the computer can represent
them exactly.

Hope this helps.

Greg

Subject: Warning: Matrix is singular to working precision

From: Roger Stafford

Date: 30 May, 2008 18:34:02

Message: 8 of 11

"saneman" <asd@ad.com> wrote in message <g1p8v5$a69$1@news.net.uni-
c.dk>...
> Be preconditioning do you mean som kind of normalization of the data? There
> are some columns that only contains 0.
------------
  If some of the columns are all zeros, that is a sure-fire indication that your
matrix is singular. An inverse cannot not exist!

Roger Stafford

Subject: Warning: Matrix is singular to working precision

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 30 May, 2008 18:49:38

Message: 9 of 11

In article <g1phaq$nrf$1@fred.mathworks.com>,
Roger Stafford <ellieandrogerxyzzy@mindspring.com.invalid> wrote:

> If some of the columns are all zeros, that is a sure-fire indication that your
>matrix is singular. An inverse cannot not exist!

Oh, in that situation, inverses -can- exist -- for sufficiently large
values of "zero" ;-)
--
Q: Why did the chicken cross the Mobius strip?

A: There were manifold reasons.

Subject: Warning: Matrix is singular to working precision

From: Roger Stafford

Date: 30 May, 2008 19:15:03

Message: 10 of 11

roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in message <g1pi82$392
$1@canopus.cc.umanitoba.ca>...
> In article <g1phaq$nrf$1@fred.mathworks.com>,
> Roger Stafford <ellieandrogerxyzzy@mindspring.com.invalid> wrote:
>
> > If some of the columns are all zeros, that is a sure-fire indication that your
> >matrix is singular. An inverse cannot not exist!
>
> Oh, in that situation, inverses -can- exist -- for sufficiently large
> values of "zero" ;-)
> --
> Q: Why did the chicken cross the Mobius strip?
> A: There were manifold reasons.
-----------
  Would those be plus zeros or minus zeros? :->

Roger Stafford

Subject: Warning: Matrix is singular to working precision

From: Steve Eddins

Date: 30 May, 2008 20:50:07

Message: 11 of 11

Roger Stafford wrote:
> roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in message <g1pi82$392
> $1@canopus.cc.umanitoba.ca>...
>> In article <g1phaq$nrf$1@fred.mathworks.com>,
>> Roger Stafford <ellieandrogerxyzzy@mindspring.com.invalid> wrote:
>>
>>> If some of the columns are all zeros, that is a sure-fire indication that your
>>> matrix is singular. An inverse cannot not exist!
>> Oh, in that situation, inverses -can- exist -- for sufficiently large
>> values of "zero" ;-)
>> --
>> Q: Why did the chicken cross the Mobius strip?
>> A: There were manifold reasons.
> -----------
> Would those be plus zeros or minus zeros? :->

Here's one of my favorite quotes - I once heard Sid Burrus give a
presentation in which he said something like "ten is sufficiently close
to infinity."

This statement actually made sense in context. But why take the fun out
of it by explaining the context?

:-)

---
Steve Eddins
http://blogs.mathworks.com/steve/

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us