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

  • Thread starter Thread starter Jamin2112
  • Start date Start date
  • Tags Tags
    Idea Matrix
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
2 replies · 2K views
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