# ill-conditioned matrix 'Matrix is close to singular or badly scaled'

in the equation of A*q=b, A is a NxN matrix in which the numbers can be up to 10^56 and the minimum is 1. the condition number of the matrix can be as large as 3.16e+064.

The SVD, QR and LUP have been used to deal with the matrix.

however, when computing using matlab, there are still warnings such as 'Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 3.164458e-065. '

why?

Simply put, the range of the values in your matrix span many, many orders of magnitude.

In double-precision arithmetic, we can only add numbers together that are within about 16 orders of magnitude of each other, otherwise the smaller number is "too small" and gets lost in the noise.

Many algorithms such as QR and SVD will run happily along with a matrix such as yours, but the results might be inaccurate. Let's just use a simple example to see what might happen.

Let $A = \begin{pmatrix} 5\times 10^{60} & 14 \\ 5\times 10^{60} & 140\end{pmatrix}$.

Obviously this matrix is non-singular. But what happens when we compute the inverse? Recall that the inverse would be:

$$A^{-1} = \frac{1}{\left(5\times 10^{60}\right)\cdot 140-14\cdot\left(5\times 10^{60}\right)}\begin{pmatrix} 140 & -14 \\ -5\times 10^{60} & 5\times 10^{60}\end{pmatrix}.$$

The denominator in the factor is very large, about $6.3\times 10^{62}$. Now, when you multiply that through, you get

$$A^{-1} = \begin{pmatrix} \frac{140}{6.3\times 10^{62}} & -\frac{14}{6.3\times 10^{62}} \\ -\frac{5\times 10^{60}}{6.3\times 10^{62}} & \frac{5\times 10^{60}}{6.3\times 10^{62}}\end{pmatrix}.$$

The numbers in the bottom row are equal to about $0.00794$. The numbers in the top row are practically zero.

So when you compute $A^{-1}A$, what you get is an issue of finite storage. The (1,1) element of $A^{-1}$ is tiny, and the (1,1) element of $A$ is huge. The computer can't store them both when it tries to compute them, so it rounds the tiny one down to zero.

So when you (naively) multiply $A^{-1}A$ on the computer, you end up getting:

$$A^{-1}A = \begin{pmatrix} 0 & 0 \\ 0 & 1\end{pmatrix}.$$

Now, if you actually punch these into MATLAB, you'll get the right answer. But that's because MATLAB has many built-in utilities for handling these scenarios. However, these utilities do not universally work on every algorithm, and the larger your matrix and the more complicated the algorithm, the less likely we are to be able to take shortcuts to avoid precision issues.

MATLAB checks this by computing the reciprocal of the condition number, aka RCOND. Usually, if RCOND is very small, you have a badly scaled matrix, and you should not trust your results, because some quirky hacks went into trying to deal with those scaling issues, and because those hacks were used, the quality of your results is not to be guaranteed.