Any idea why I'm ALWAYS getting a nonsingular matrix warning?

  • Thread starter Thread starter Jamin2112
  • Start date Start date
  • Tags Tags
    Idea Matrix
Click For Summary
SUMMARY

The discussion centers on the issue of receiving a nonsingular matrix warning when executing a MATLAB script that involves random and Hilbert matrices. The user encounters warnings related to matrix conditioning, particularly when using the 'inv' function and the 'rref' function. The Hilbert matrix is identified as poorly conditioned, with its true condition number being significantly worse than MATLAB's estimates. The suggestion to initialize the random number generator is provided as a potential solution to mitigate the warnings.

PREREQUISITES
  • Understanding of MATLAB scripting and functions
  • Familiarity with matrix operations, specifically 'inv' and 'rref'
  • Knowledge of matrix conditioning and its implications
  • Basic concepts of random number generation in MATLAB
NEXT STEPS
  • Learn about MATLAB's 'rand' function and its impact on matrix generation
  • Research the properties of Hilbert matrices and their condition numbers
  • Explore techniques for improving matrix conditioning in numerical computations
  • Investigate the use of the 'rng' function in MATLAB for random number initialization
USEFUL FOR

Mathematics students, MATLAB users, data scientists, and anyone involved in numerical analysis or linear algebra who seeks to understand matrix conditioning and error handling in MATLAB.

Jamin2112
Messages
973
Reaction score
12
Every time I run this, I'm getting the error that my random matrix is poorly conditioned. It's weird because I don't get the same error if I just create one in the command window.

My script:

clc
clear all

% part 1

x = rand(20, 1);
save('x.mat', 'x');
a = rand(20, 20);
save('a.mat', 'a');
y = a*x;
c = [a, y];
x1 = diag(rref(c));
x2 = inv(a) * y;
x3 = a\y;
ex1 = norm(x - x1);
ex2 = norm(x - x2);
ex3 = norm(x - x3);
fprintf('Using random matrix: \n \n')
fprintf('Error using rref function: %f \n', ex1)
fprintf('Error using inv function: %f \n', ex2)
fprintf('Error using backslash operator: %f \n', ex3)

clear a y c x1 x2 x3 ex1 ex2 ex3

% part 2
a = hilb(20);
y = a*x;
c = [a, y];
x1 = diag(rref(c));
x2 = inv(a) * y;
x3 = a\y;
ex1 = norm(x - x1);
ex2 = norm(x - x2);
ex3 = norm(x - x3);
fprintf('\n Using hilbert matrix: \n \n')
fprintf('Error using rref function: %f \n', ex1)
fprintf('Error using inv functoin: %f \n', ex2)
fprintf('Error using backslash operator: %f \n', ex3)

Output:

Using random matrix:

Error using rref function: 2.722469
Error using inv function: 0.000000
Error using backslash operator: 0.000000
Warning: Matrix is close to singular or badly scaled. Results
may be inaccurate. RCOND = 1.155429e-19.
> In hw6 at 30
Warning: Matrix is close to singular or badly scaled. Results
may be inaccurate. RCOND = 1.155429e-19.
> In hw6 at 31

Using hilbert matrix:

Error using rref function: 2.745181
Error using inv functoin: 134.434224
Error using backslash operator: 103.192355
 
Physics news on Phys.org
No idea what it is all about, but... have you tried to initialize random numbers generator?
 
The warnings refer to lines 30 and 31 which are inverting the hilbert matrix, not the random matrix.

The Hilbert matrix is badly conditioned. In fact the estimates in the your error messages are about 10^10 times too big compared with its true condition number (i.e. it is really a lot worse than Matlab said). http://en.wikipedia.org/wiki/Hilbert_matrix
 

Similar threads

Replies
2
Views
2K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 3 ·
Replies
3
Views
3K
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
5K
Replies
1
Views
2K